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