summaryrefslogtreecommitdiffstats
path: root/modules/build.py
blob: cc61868a3eb51f8fe46d61c8ae69e0dbedca1678 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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

from shutil import copyfileobj
from os.path import join, abspath, isfile
from os import listdir, getcwd, walk
from subprocess import Popen

from base.base import log
from base.module import Module
from base.util import pwd, copy, symlink, move, log_file
from base.exceptions import ExecutionException
from base.vars import FEDORA_DIR
from base.profiles import dir_defines, join_defines, dist_defines, Profile

from modules.directory import Directory
from modules.dirfactory import DirFactory
from modules.package import Package
from modules.profile import Profile

class Build(Directory):
    _type = 'build'
    def setup_source(self, package):
        pkg = DirFactory(package)
        with pwd(pkg.dir):
            symlink(pkg.spec_file,
                    join(self.dir, 'SPECS', pkg.spec_file))
            sball = pkg.sourceball
            symlink(sball, join(self.dir, 'SOURCES', sball))
            patches = [f for f in listdir(getcwd()) if f.endswith('.patch')]
            for patch in patches:
                symlink(patch, join(self.dir, 'SOURCES', patch))

    def build_quick_rpm(self, package, profile=None):
        self.rpmbuild('-ba', package, profile)

    def build_source_rpm(self, package, profile=None):
        self.rpmbuild('-bs', package, profile)

    def rpmbuild(self, param, package, profile=None):
        pkg = DirFactory(package)
        if profile:
            defines = join_defines(profile.dist_defines,
                                   dir_defines(self.dir))
        else:
            defines = dir_defines(self.dir)
        log.debug('defines are ' + str(defines))
        log.debug('spec file is ' + pkg.spec_file)
        cmd = ['rpmbuild'] + defines + ['-v', param, pkg.spec_file]
        log.debug('cmd is ' + str(cmd))
        with pwd(pkg.dir):
            with log_file('rpmbuild.log') as rpm_out:
                with pwd(join(self.dir, 'SPECS')):
                    p = Popen(cmd, stdout=rpm_out, stderr=rpm_out)
                    log.info('building %s... please wait'
                             % pkg.spec_file)
                    p.wait()
                    log.debug(p.returncode)

    def fetch_rpms(self, target_dir):
        with pwd(self.dir):
            for path, dirs, files in walk('.'):
                for f in files:
                    if f.endswith('.rpm'):
                        #TODO: Gotta figure out what should be FEDORA_DIR
                        # Probably something involving "Project" or i dunno what
                        # it's something temporary for now, don't commi this
                        move(join(path, f), join(target_dir, f))

    def fetch_build(self, package):
        pkg = DirFactory(package)
        with pwd(self.dir):
            source = pkg.cfg['source']
            move(join('BUILD', source), join(pkg.dir, 'results'))

__all__ = ['Build']