summaryrefslogtreecommitdiffstats
path: root/BuildManager/build.py
blob: 9279d1715c9a2fcce3b843af7e8150a6e58ef268 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
from BuildManager.fileutil import *
from BuildManager.package import *
from BuildManager import *
import thread
import subprocess
import sys, os
import time
import shutil
import glob

__all__ = ["PackageBuilder"]

GLOBAL_PKGLIST_LOCK = thread.allocate_lock()

STAGE_UNPACK = 0
STAGE_PREP = 1
STAGE_COMPILE = 2
STAGE_INSTALL = 3
STAGE_SOURCE = 4
STAGE_BINARY = 5
STAGE_ALL = 6

STAGE_DICT = {"unpack": STAGE_UNPACK,
              "prep": STAGE_PREP,
              "compile": STAGE_COMPILE,
              "install": STAGE_INSTALL,
              "source": STAGE_SOURCE,
              "binary": STAGE_BINARY,
              "all": STAGE_ALL}

class PackageBuilder:
    def __init__(self, opts):
        self.opts = opts
        self.stage = STAGE_DICT[opts.mode]

    def run(self):
        self.pkglist = PackageList()
        logger.info("creating package list")
        for filename in self.opts.args:
            pkg = Package(filename, self.opts.build_log)
            for ignore in self.opts.ignore:
                if ignore.match(pkg.name):
                    break
            else:
                self.pkglist.append(pkg)
        for dir in self.opts.filter_renew:
            filterpkglist(pkglist, dir, "not_has_ge")
        for dir in self.opts.filter_refresh:
            filterpkglist(pkglist, dir, "not_has_lt")
        self.pkgsleft = len(self.pkglist)
        if self.pkgsleft == 0:
            logger.info("no packages to process")
            return True
        elif self.pkgsleft > 1 and self.pkgsleft % 10 != 0:
            logger.info("package list has %d packages" % pkgsleft)
        self.pkglist_lock = thread.allocate_lock()
        self.failures = 0
        if self.opts.parallel != 1:
            logger.info("starting threads")
            for i in range(self.opts.parallel-1):
                thread.start_new_thread(self_processlist, ())
        self._processlist()
        while self.pkgsleft > 0:
            time.sleep(2)
        return not self.failures

    def _processlist(self):
        while 1:
            self.pkglist_lock.acquire()
            if not self.pkglist:
                self.pkglist_lock.release()
                return 1
            if self.pkgsleft % 10 == 0:
                logger.info("package list has %d packages" % self.pkgsleft)
            pkg = self.pkglist.pop(0)
            logger.info("processing package %s-%s-%s" %
                        (pkg.name, pkg.version, pkg.release))
            self.pkglist_lock.release()
            ret = buildpkg(pkg=pkg,
                           stage=self.stage,
                           unpack_dir=self.opts.unpack_dir,
                           passtrough=" ".join(self.opts.options),
                           show_log=self.opts.show_log,
                           dryrun=self.opts.dryrun)
            if ret:
                if pkg.type == "srpm":
                    if self.opts.move_succeeded_srpm:
                        move_file(pkg.file,
                                  self.opts.move_succeeded_srpm,
                                  dryrun=self.opts.dryrun)
                    elif self.opts.copy_succeeded_srpm:
                        copy_file(pkg.file,
                                  self.opts.copy_succeeded_srpm,
                                  dryrun=self.opts.dryrun)
                    elif self.opts.remove_succeeded_srpm:
                        logger.info("removing %s" % pkg.file)
                        if not self.opts.dryrun:
                            os.unlink(pkg.file)
                if self.opts.move_srpm:
                    dir = os.path.join(pkg.builddir, "SRPMS")
                    for file in glob.glob(os.path.join(dir, "*.src.rpm")):
                        move_file(os.path.join(dir, file),
                                  self.opts.move_srpm,
                                  dryrun=self.opts.dryrun)
                if self.opts.move_rpm:
                    dir = os.path.join(pkg.builddir, "RPMS")
                    for subdir in os.listdir(dir):
                        subdir = os.path.join(dir, subdir)
                        for file in os.listdir(subdir):
                            move_file(os.path.join(subdir, file),
                                      self.opts.move_rpm,
                                      dryrun=self.opts.dryrun)
                if self.opts.move_log:
                    move_file(pkg.log,
                              self.opts.move_log,
                              dryrun=self.dryrun)
                if self.opts.clean or self.opts.clean_on_success:
                    if pkg.builddir != "/":
                        logger.info("cleaning build directory")
                        if not self.opts.dryrun:
                            shutil.rmtree(pkg.builddir)
            else:
                self.failures += 1
                if pkg.type == "srpm":
                    if self.opts.move_failed_srpm:
                        move_file(pkg.file,
                                  self.opts.move_failed_srpm,
                                  dryrun=self.opts.dryrun)
                    elif self.opts.copy_failed_srpm:
                        copy_file(pkg.file,
                                  self.opts.copy_failed_srpm,
                                  dryrun=self.opts.dryrun)
                    elif self.opts.remove_failed_srpm:
                        logger.info("removing %s" % pkg.file)
                        if not self.opts.dryrun:
                            os.unlink(pkg.file)
                if self.opts.move_failed_log:
                    move_file(pkg.log,
                              self.opts.move_failed_log,
                              dryrun=self.opts.dryrun)
                if self.opts.clean:
                    if pkg.builddir != "/":
                        logger.info("cleaning build directory")
                        if not self.opts.dryrun:
                            shutil.rmtree(pkg.builddir)
            self.pkglist_lock.acquire()
            self.pkgsleft -= 1
            self.pkglist_lock.release()

def filterpkglist(pkglist, directory, rule):
    filterlist = PackageList()
    logger.info("creating package list filter for "+directory)
    for filename in os.listdir(directory):
        filename = os.path.join(directory, filename)
        if os.path.isfile(filename):
            filterlist.append(Package(filename))
    logger.info("filtering")
    if rule[:4] == "not_":
        filterfunc_tmp = getattr(filterlist, rule[4:])
        filterfunc = lambda x: not filterfunc_tmp(x)
    else:
        filterfunc = getattr(filterlist, rule)
    pkglist[:] = filter(filterfunc, pkglist)


def buildpkglist(pkglist, stage, unpack_dir, passtrough="",
                 show_log=0, dryrun=0):
    while 1:
        GLOBAL_PKGLIST_LOCK.acquire()
        if not pkglist:
            GLOBAL_PKGLIST_LOCK.release()
            return 1
        pkg = pkglist[0]
        del pkglist[0]
        GLOBAL_PKGLIST_LOCK.release()
        buildpkg(pkg, stage, unpack_dir, passtrough, show_log, dryrun)

def buildpkg(pkg, stage, unpack_dir, passtrough="", show_log=0, dryrun=0):
    stagestr = ["unpacking",
                "running prep stage",
                "running prep and compile stage",
                "running prep, compile, and install stages",
                "building source package",
                "building binary packages",
                "building source and binary packages"][stage]
    logger.info(stagestr)
    ret = 0
    if pkg.type == "srpm" and not (dryrun or pkg.unpack(unpack_dir)):
        logger.error("failed unpacking")
        return 0
    else:
        status = 0
        if stage != STAGE_UNPACK:
            stagechar = ["p","c","i","s","b","a"][stage-1]
            if not dryrun and os.path.isdir(pkg.builddir+"/BUILDROOT"):
                tmppath = " --define '_tmppath %s/BUILDROOT'" % pkg.builddir
            else:
                tmppath = ""
            cmd = "rpmbuild -b%s --define '_topdir %s'%s %s %s 2>&1" % \
                  (stagechar,pkg.builddir,tmppath,passtrough,pkg.spec)
            logger.debug("rpmbuild command: "+cmd)
            if not dryrun:
                log = open(pkg.log, "w")
                pop = subprocess.Popen(cmd, shell=True,
                        stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
                of = pop.stdout.fileno()
                while True:
                    status = pop.poll()
                    if status is not None:
                        break
                    data = os.read(of, 8192)
                    log.write(data)
                    log.flush()
                    if show_log:
                        sys.stdout.write(data)
                log.close()
        if status == 0:
            logger.info("succeeded!")
            ret = 1
        else:
            logger.error("failed!")
            ret = 0
    return ret

# vim:ts=4:sw=4