summaryrefslogtreecommitdiffstats
path: root/modules/package.py
blob: 80729382fc6956cb4b9647381a639f42aeced165 (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
from __future__ import with_statement
import tarfile

from os import makedirs, getcwd, chdir
from shutil import copyfileobj, copytree
from os.path import abspath, join, split, splitext, basename
from configobj import ConfigObj


from base.module import Module
from base.base import log
from base.exceptions import ExecutionException
from base.util import pwd, copy

#TODO: Find universal library for parsing URLs and PATHs alike

class Package(Module):
    def __init__(self, name=None):
        if not name:
            log.debug(split(getcwd()))
            name = split(getcwd())[1]
            self.code_dir = getcwd()
            #detect name somehow
        else:
            try:
                cfg = ConfigObj('.devshell')
                if cfg['type'] == 'package':
                    self.code_dir = getcwd()
            except:
                self.code_dir = abspath(name) 
        with pwd(self.code_dir):
            self.pkg_cfg = ConfigObj('.devshell')
            if not self.pkg_cfg['type'] == 'package':
                raise ExecutionException('invalid package directory')
        self.name = name
    
    def create(self, name):
        if not self.name:
            makedirs(join(getcwd(), name))
    
    def add_spec(self, spec_file):
        log.debug('spec_file is %s' % spec_file)
        log.debug('spec_file_name is %s' % self.name + '.spec')
        with pwd(self.code_dir):
            try:
                copy(spec_file, self.name)
            except IOError, e:
                log.error(str(e))
                raise ExecutionException(e, 'spec-file could not be added')
    
    def add_sourceball(self, sourceball_name, extract_dir=None):
        with pwd(self.code_dir):
            try:
                copy(sourceball_name, split(sourceball_name)[1])
                sourceball_name = split(sourceball_name)[1]
                
                self.pkg_cfg['sourceball'] = sourceball_name 
                sourceball = tarfile.open(sourceball_name)
                if not extract_dir:
                    extract_dir = min([(x.name, x) for x in sourceball])[0]
                    extract_dir = basename(abspath(extract_dir))
                    log.debug('extract_dir is %s' % extract_dir)
                    self.pkg_cfg['source'] = extract_dir 
                orig_extract_dir = extract_dir + '_orig'
                sourceball.extractall()
                copytree(abspath(extract_dir), abspath(orig_extract_dir))
            except OSError, e:
                #TODO: Fill this in with something better
                #Chances are the _orig dir already exists
                raise ExecutionException(e, 'something went wrong')
                #TODO: figure out what exceptions TarFile will throw
    
    def close(self):
        self.pkg_cfg.write()
        
__all__ = ['Package']