]> TLD Linux GIT Repositories - tld-builder.git/blob - TLD_Builder/srpm_builder.py
- cleanup
[tld-builder.git] / TLD_Builder / srpm_builder.py
1 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
2
3 import email
4 import string
5 import time
6 import os
7 import sys
8 import re
9 import shutil
10 import atexit
11 import tempfile
12
13 import gpg
14 import request
15 import log
16 import path
17 import util
18 import loop
19 import chroot
20 import ftp
21 import buildlogs
22 import notify
23 import status
24 import build
25 import report
26
27 from lock import lock
28 from bqueue import B_Queue
29 from config import config, init_conf
30
31 def pick_request(q):
32     def mycmp(r1, r2):
33         if r1.kind != 'group' or r2.kind != 'group':
34             raise Exception("non-group requests")
35         pri_diff = util.cmp(r1.priority, r2.priority)
36         if pri_diff == 0:
37             return util.cmp(r1.time, r2.time)
38         else:
39             return pri_diff
40     q.requests.sort(key=util.cmp_to_key(mycmp))
41     ret = q.requests[0]
42     q.requests = q.requests[1:]
43     return ret
44
45 def store_binary_request(r):
46     new_b = []
47     for b in r.batches:
48         if not b.build_failed: new_b.append(b)
49     if new_b == []:
50         return
51     r.batches = new_b
52     # store new queue and max_req_no for binary builders
53     num = int(open(path.max_req_no_file, "r").read().strip()) + 1
54
55     r.no = num
56     q = B_Queue(path.req_queue_file)
57     q.lock(0)
58     q.read()
59     q.add(r)
60     q.write()
61     q.dump(path.queue_stats_file)
62     q.dump_html(path.queue_html_stats_file)
63     q.write_signed(path.req_queue_signed_file)
64     q.unlock()
65
66     (fdno, tmpfname) = tempfile.mkstemp(dir=os.path.dirname(path.max_req_no_file))
67     cnt_f = os.fdopen(fdno, "w")
68     cnt_f.seek(0)
69     cnt_f.write("%d\n" % num)
70     cnt_f.flush()
71     os.fsync(cnt_f.fileno())
72     cnt_f.close()
73     os.chmod(tmpfname, 0o0644)
74     os.rename(tmpfname, path.max_req_no_file)
75
76 def transfer_file(r, b):
77     local = path.srpms_dir + '/' + r.id + "/" + b.src_rpm
78     f = b.src_rpm_file
79     # export files from chroot
80     chroot.cp(f, outfile = local, rm = True)
81     os.chmod(local, 0o0644)
82     ftp.add(local)
83
84     if config.gen_upinfo and 'test-build' not in r.flags:
85         fname = path.srpms_dir + '/' + r.id + "/" + b.src_rpm + ".uploadinfo"
86         f = open(fname, "w")
87         f.write("info:build:%s:requester:%s\ninfo:build:%s:requester_email:%s\nfile:SRPMS:%s\nEND\n" % (b.gb_id, b.requester, b.gb_id, b.requester_email, b.src_rpm))
88         f.close()
89         ftp.add(fname, "uploadinfo")
90
91 def build_srpm(r, b):
92     if len(b.spec) <= len('.spec'):
93         # should not really get here
94         util.append_to(b.logfile, "error: No .spec given but build src.rpm wanted")
95         return "FAIL"
96
97     status.push("building %s" % b.spec)
98
99     b.src_rpm = ""
100     builder_opts = "-nu -nm --nodeps --http --define \'_tld_builder 1\'"
101     cmd = ("cd rpm/packages; nice -n %s ./builder %s -bs %s -r %s %s %s %s 2>&1" %
102              (config.nice, builder_opts, b.bconds_string(), b.branch,
103               b.kernel_string(), b.defines_string(), b.spec))
104     util.append_to(b.logfile, "request from: %s" % r.requester)
105     util.append_to(b.logfile, "started at: %s" % time.asctime())
106     util.append_to(b.logfile, "building SRPM using: %s\n" % cmd)
107     res = chroot.run(cmd, logfile = b.logfile)
108     util.append_to(b.logfile, "exit status %d" % res)
109     files = util.collect_files(b.logfile)
110     if len(files) > 0:
111         if len(files) > 1:
112             util.append_to(b.logfile, "error: More than one file produced: %s" % files)
113             res = "FAIL_TOOMANYFILES"
114         last = files[len(files) - 1]
115         b.src_rpm_file = last
116         b.src_rpm = os.path.basename(last)
117         r.chroot_files.extend(files)
118     else:
119         util.append_to(b.logfile, "error: No files produced.")
120         res = "FAIL"
121     if res == 0 and not "test-build" in r.flags:
122         util.append_to(b.logfile, "Writing package revision")
123         res = chroot.run("cd rpm/packages; ./builder -bs %s -r %s --pkgrev %s %s" % \
124             (b.bconds_string(), b.branch, b.defines_string(), b.spec), logfile = b.logfile)
125     if res == 0:
126         transfer_file(r, b)
127
128     packagename = b.spec[:-5]
129     packagedir = "rpm/packages/%s" % packagename
130     chroot.run("rpm/packages/builder -m %s" % \
131             (b.spec,), logfile = b.logfile)
132     chroot.run("rm -rf %s" % packagedir, logfile = b.logfile)
133     status.pop()
134
135     if res:
136         res = "FAIL"
137
138     return res
139
140 def handle_request(r):
141     os.mkdir(path.srpms_dir + '/' + r.id)
142     os.chmod(path.srpms_dir + '/' + r.id, 0o0755)
143     ftp.init(r)
144     buildlogs.init(r)
145     build.build_all(r, build_srpm)
146     report.send_report(r, is_src = True)
147     report.send_cia_report(r, is_src = True)
148     store_binary_request(r)
149     ftp.flush()
150     notify.send(r)
151
152 def main():
153     init_conf("src")
154     if lock("building-srpm", non_block = 1) == None:
155         return
156     while True:
157         status.push("srpm: processing queue")
158         q = B_Queue(path.queue_file)
159         if not q.lock(1):
160             status.pop()
161             return
162         q.read()
163         if q.requests == []:
164             q.unlock()
165             status.pop()
166             return
167         r = pick_request(q)
168         q.write()
169         q.unlock()
170         status.pop()
171         status.push("srpm: handling request from %s" % r.requester)
172         handle_request(r)
173         status.pop()
174
175 if __name__ == '__main__':
176     loop.run_loop(main)