summaryrefslogtreecommitdiffstats
path: root/modules/sourceball.py
blob: 8364eda3ff4b3ac6d51f1159043878d295f67a95 (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
# Fedora Developer Shell
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Authors: Yaakov M. Nemoy <ynemoy@redhat.com>
#
from __future__ import with_statement

import tarfile

from os.path import abspath, split, basename
from shutil import copytree
from subprocess import Popen, PIPE

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

from modules.package import Package

class SourceBall(Package):
    _type = 'sourceball'
    def orig_dir(self, dir):
        return dir + '_orig'
    
    def source_dir(self, *args):
        if args[0] == 'orig':
            return self.orig_dir(self.cfg['source'])
        else:
            return self.cfg['source']

    def add_sourceball(self, sourceball_name, extract_dir=None):
        log.debug('addincg sourceball with code_dir ' + self.code_dir)
        with pwd(self.dir):
            try:
                sourceball_name = copy(sourceball_name,
                                       split(sourceball_name)[1])

                self.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)
                log.debug('config is of ' + str(self.cfg))
                self.cfg['source'] = extract_dir
                log.debug('cfg[\'source\'] is ' + self.cfg['source'])
                orig_extract_dir = self.orig_dir(extract_dir)
                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


__all__ = ['SourceBall']