]> TLD Linux GIT Repositories - tld-builder.git/blob - TLD_Builder/build.py
- more python 3.x fixes
[tld-builder.git] / TLD_Builder / build.py
1 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
2
3 import string
4 import os
5 import atexit
6
7 import notify
8 import path
9 import util
10 import chroot
11 import stopwatch
12 import report
13 import log
14 import buildlogs
15 import status
16 from config import config, init_conf
17
18
19
20 def run_command(batch):
21     # we want to keep "skip" in queue.html
22     command = batch.command
23
24     # rewrite special "skip:BUILD_ID into touch
25     if command[:5] == "skip:":
26         c = ""
27         for id in command[5:].split(','):
28             if os.path.isdir(path.srpms_dir + '/' + id):
29                 c = c + "echo skip:%s;\n" % (id)
30                 c = c + "touch %s/%s/skipme;\n" % (path.srpms_dir, id)
31             else:
32                 c = c + "echo %s is not valid build-id;\n" % (id)
33         command = c
34
35     if "no-chroot" in batch.command_flags:
36         # TODO: the append here by shell hack should be solved in python
37         c = "(%s) >> %s 2>&1" % (command, batch.logfile)
38         f = os.popen(c)
39         for l in f:
40             pass
41         r = f.close()
42         if r == None:
43             return 0
44         else:
45             return r
46     else:
47         user = "root"
48         if "as-builder" in batch.command_flags:
49             user = None
50         return chroot.run(command, logfile = batch.logfile, user = user)
51
52 def build_all(r, build_fnc):
53     status.email = r.requester_email
54     notify.begin(r)
55     tmp = path.build_dir + '/' + util.uuid() + "/"
56     r.tmp_dir = tmp
57     os.mkdir(tmp)
58     atexit.register(util.clean_tmp, tmp)
59
60     log.notice("started processing %s" % r.id)
61     r.chroot_files = []
62     r.some_ok = 0
63     for batch in r.batches:
64         can_build = 1
65         failed_dep = ""
66         for dep in batch.depends_on:
67             if dep.build_failed:
68                 can_build = 0
69                 failed_dep = dep.spec
70
71         if batch.is_command() and can_build:
72             batch.logfile = tmp + "command"
73             if config.builder in batch.builders:
74                 log.notice("running %s" % batch.command)
75                 stopwatch.start()
76                 batch.build_failed = run_command(batch)
77                 if batch.build_failed:
78                     log.notice("running %s FAILED" % batch.command)
79                     notify.add_batch(batch, "FAIL")
80                 else:
81                     r.some_ok = 1
82                     log.notice("running %s OK" % batch.command)
83                     notify.add_batch(batch, "OK")
84                 batch.build_time = stopwatch.stop()
85                 report.add_tld_builder_info(batch)
86                 buildlogs.add(batch.logfile, failed = batch.build_failed, id=r.id)
87             else:
88                 log.notice("not running command, not for me.")
89                 batch.build_failed = 0
90                 batch.log_line("queued command %s for other builders" % batch.command)
91                 r.some_ok = 1
92                 buildlogs.add(batch.logfile, failed = batch.build_failed, id=r.id)
93         elif can_build:
94             log.notice("building %s" % batch.spec)
95             stopwatch.start()
96             batch.logfile = tmp + batch.spec + ".log"
97             batch.gb_id=r.id
98             batch.requester=r.requester
99             batch.requester_email=r.requester_email
100             batch.build_failed = build_fnc(r, batch)
101             if batch.build_failed:
102                 log.notice("building %s FAILED (%s)" % (batch.spec, batch.build_failed))
103                 notify.add_batch(batch, batch.build_failed)
104             else:
105                 r.some_ok = 1
106                 log.notice("building %s OK" % (batch.spec))
107                 notify.add_batch(batch, "OK")
108             batch.build_time = stopwatch.stop()
109             report.add_tld_builder_info(batch)
110             buildlogs.add(batch.logfile, failed = batch.build_failed, id=r.id)
111         else:
112             batch.build_failed = 1
113             batch.skip_reason = "SKIPED [%s failed]" % failed_dep
114             batch.logfile = None
115             batch.build_time = ""
116             log.notice("building %s %s" % (batch.spec, batch.skip_reason))
117             notify.add_batch(batch, "SKIP")
118
119     buildlogs.flush()
120     chroot.run("rm -f %s" % r.chroot_files.join())