]> TLD Linux GIT Repositories - tld-builder.git/blob - TLD_Builder/srpm_builder.py
- tagging no longer supported
[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 StringIO
8 import sys
9 import re
10 import shutil
11 import atexit
12 import tempfile
13
14 import gpg
15 import request
16 import log
17 import path
18 import util
19 import loop
20 import chroot
21 import ftp
22 import buildlogs
23 import notify
24 import status
25 import build
26 import report
27
28 from lock import lock
29 from bqueue import B_Queue
30 from config import config, init_conf
31
32 def pick_request(q):
33     def mycmp(r1, r2):
34         if r1.kind != 'group' or r2.kind != 'group':
35             raise Exception, "non-group requests"
36         pri_diff = cmp(r1.priority, r2.priority)
37         if pri_diff == 0:
38             return cmp(r1.time, r2.time)
39         else:
40             return pri_diff
41     q.requests.sort(mycmp)
42     ret = q.requests[0]
43     q.requests = q.requests[1:]
44     return ret
45
46 def store_binary_request(r):
47     new_b = []
48     for b in r.batches:
49         if not b.build_failed: new_b.append(b)
50     if new_b == []:
51         return
52     r.batches = new_b
53     # store new queue and max_req_no for binary builders
54     num = int(string.strip(open(path.max_req_no_file, "r").read())) + 1
55
56     r.no = num
57     q = B_Queue(path.req_queue_file)
58     q.lock(0)
59     q.read()
60     q.add(r)
61     q.write()
62     q.dump(path.queue_stats_file)
63     q.dump_html(path.queue_html_stats_file)
64     q.write_signed(path.req_queue_signed_file)
65     q.unlock()
66
67     (fdno, tmpfname) = tempfile.mkstemp(dir=os.path.dirname(path.max_req_no_file))
68     cnt_f = os.fdopen(fdno, "w")
69     cnt_f.seek(0)
70     cnt_f.write("%d\n" % num)
71     cnt_f.flush()
72     os.fsync(cnt_f.fileno())
73     cnt_f.close()
74     os.chmod(tmpfname, 0644)
75     os.rename(tmpfname, path.max_req_no_file)
76
77 def transfer_file(r, b):
78     local = path.srpms_dir + '/' + r.id + "/" + b.src_rpm
79     f = b.src_rpm_file
80     # export files from chroot
81     chroot.cp(f, outfile = local, rm = True)
82     os.chmod(local, 0644)
83     ftp.add(local)
84
85     if config.gen_upinfo and 'test-build' not in r.flags:
86         fname = path.srpms_dir + '/' + r.id + "/" + b.src_rpm + ".uploadinfo"
87         f = open(fname, "w")
88         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))
89         f.close()
90         ftp.add(fname, "uploadinfo")
91
92 def build_srpm(r, b):
93     if len(b.spec) <= len('.spec'):
94         # should not really get here
95         util.append_to(b.logfile, "error: No .spec given but build src.rpm wanted")
96         return "FAIL"
97
98     status.push("building %s" % b.spec)
99
100     b.src_rpm = ""
101     builder_opts = "-nu -nm --nodeps --http --define \'_tld_builder 1\'"
102     cmd = ("cd rpm/packages; nice -n %s ./builder %s -bs %s -r %s %s %s %s 2>&1" %
103              (config.nice, builder_opts, b.bconds_string(), b.branch,
104               b.kernel_string(), b.defines_string(), b.spec))
105     util.append_to(b.logfile, "request from: %s" % r.requester)
106     util.append_to(b.logfile, "started at: %s" % time.asctime())
107     util.append_to(b.logfile, "building SRPM using: %s\n" % cmd)
108     res = chroot.run(cmd, logfile = b.logfile)
109     util.append_to(b.logfile, "exit status %d" % res)
110     files = util.collect_files(b.logfile)
111     if len(files) > 0:
112         if len(files) > 1:
113             util.append_to(b.logfile, "error: More than one file produced: %s" % files)
114             res = "FAIL_TOOMANYFILES"
115         last = files[len(files) - 1]
116         b.src_rpm_file = last
117         b.src_rpm = os.path.basename(last)
118         r.chroot_files.extend(files)
119     else:
120         util.append_to(b.logfile, "error: No files produced.")
121         res = "FAIL"
122     if res == 0 and not "test-build" in r.flags:
123         util.append_to(b.logfile, "Writing pkgrev")
124             res = chroot.run("cd rpm/packages; ./builder -bs %s -r %s --pkgrev %s %s" % \
125                 (b.bconds_string(), b.branch, b.defines_string(), b.spec), logfile = b.logfile)
126     if res == 0:
127         transfer_file(r, b)
128
129     packagename = b.spec[:-5]
130     packagedir = "rpm/packages/%s" % packagename
131     chroot.run("rpm/packages/builder -m %s" % \
132             (b.spec,), logfile = b.logfile)
133     chroot.run("rm -rf %s" % packagedir, logfile = b.logfile)
134     status.pop()
135
136     if res:
137         res = "FAIL"
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, 0755)
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)