aboutsummaryrefslogtreecommitdiff
path: root/cbc/environment.py
blob: a1c6b6e432289ac99b1c501c8d83edea27d6d244 (plain) (blame)
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
import os
from .exceptions import IncompleteEnv
from tempfile import TemporaryDirectory
import time


class Environment(object):
    def __init__(self, *args, **kwargs):
        self.environ = os.environ.copy()
        self.config = {}
        self.cbchome = None
        self.pwd = os.path.abspath(os.curdir)
        self.pkgdir = None
        
        if 'CBC_HOME' in kwargs:
            self.cbchome = kwargs['CBC_HOME']
        
        # I want the local user environment to override what is
        # passed to the class.
        if 'CBC_HOME' in self.environ:
            self.cbchome = self.environ['CBC_HOME']
        
        if self.cbchome is None:
            raise IncompleteEnv('CBC_HOME is undefined.')
        
        
        self.cbchome = os.path.abspath(self.cbchome)
        if not os.path.exists(self.cbchome):
            os.makedirs(self.cbchome)
        
    def _script_meta(self):
        self.config['script'] = {}
        self.config['script']['meta'] = self.join('meta.yaml')
        self.config['script']['build_linux'] = self.join('build.sh')
        self.config['script']['build_windows'] = self.join('bld.bat')
        
    def join(self, filename):
        return os.path.abspath(os.path.join(self.pkgdir, filename))
    
    def mkpkgdir(self, pkgname):
        pkgdir = os.path.join(self.cbchome, pkgname)
        
        if not pkgname:
            raise IncompleteEnv('Empty package name passed to {0}'.format(__name__))
        if not os.path.exists(pkgdir):
            os.mkdir(pkgdir)
            
        self.pkgdir = pkgdir
        self._script_meta()
        
    '''
    def local_temp(self):
        temp_prefix = os.path.basename(os.path.splitext(__name__)[0])
        return TemporaryDirectory(prefix=temp_prefix, dir=self.cbchome)        
    '''