aboutsummaryrefslogtreecommitdiff
blob: d25745bb412d21e679f23091a1da4b7cd66835a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- coding: utf-8 -*-

"""
    g_octave.overlay
    ~~~~~~~~~~~~~~~~
    
    This module implements a function to create an overlay to host the
    ebuilds generated by g-octave.
    
    :copyright: (c) 2009-2010 by Rafael Goncalves Martins
    :license: GPL-2, see LICENSE for more details.
"""

from __future__ import absolute_import

__all__ = ['create_overlay']

import os
import sys
import shutil
import portage.output

from .config import Config
from .compat import open

config = Config()
out = portage.output.EOutput()

def create_overlay(force=False, quiet=False):
    
    if force and os.path.exists(config.overlay):
        shutil.rmtree(config.overlay)
    
    if not os.path.exists(os.path.join(config.overlay, 'profiles', 'repo_name')):
        
        if not quiet:
            out.ebegin('Creating overlay: %s' % config.overlay)
        
        try:
            # creating dirs
            for _dir in ['profiles', 'eclass']:
                dir = os.path.join(config.overlay, _dir)
                if not os.path.exists(dir) or force:
                    os.makedirs(dir, 0o755)
            
            # creating files
            files = {
                os.path.join(config.overlay, 'profiles', 'repo_name'): 'g-octave',
                os.path.join(config.overlay, 'profiles', 'categories'): 'g-octave',
            }
            
            # symlinking g-octave eclass
            local_eclass = os.path.join(
                os.path.dirname(os.path.abspath(__file__)),
                '..', 'share', 'g-octave.eclass'
            )
            global_eclass = os.path.join(sys.prefix, 'share', 'g-octave', 'g-octave.eclass')
            overlay_eclass = os.path.join(config.overlay, 'eclass', 'g-octave.eclass')
            if os.path.exists(local_eclass):
                os.symlink(local_eclass, overlay_eclass)
            elif os.path.exists(global_eclass):
                os.symlink(global_eclass, overlay_eclass)
            else:
                if not quiet:
                    out.eend(1)
                sys.exit(1)
            for _file in files:
                if not os.path.exists(_file) or force:
                    with open(_file, 'w') as fp:
                        content = files[_file]
                        if hasattr(content, 'name'):
                            content = content.read()
                        fp.write(content)
        except:
            if not quiet:
                out.eend(1)
            sys.exit(1)
        else:
            if not quiet:
                out.eend(0)