1 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
3 from xml.dom.minidom import *
4 from datetime import datetime
7 import xml.sax.saxutils
18 from config import config
19 from subprocess import call
21 __all__ = ['parse_request', 'parse_requests']
25 for n in e.childNodes:
26 if n.nodeType != Element.TEXT_NODE:
27 log.panic("xml: text expected in <%s>, got %d" % (e.nodeName, n.nodeType))
31 def attr(e, a, default = None):
33 return e.attributes[a].value
40 return xml.sax.saxutils.escape(s)
42 # return date in iso8601 format
43 def iso8601(ts, timezone='UTC'):
44 tz = pytz.timezone(timezone)
45 dt = datetime.fromtimestamp(ts, tz)
49 return e.nodeType == Element.TEXT_NODE and string.strip(e.nodeValue) == ""
52 def __init__(self, e):
55 self.id = attr(e, "id")
56 self.no = int(attr(e, "no"))
58 self.time = time.time()
61 self.requester_email = ""
62 self.flags = string.split(attr(e, "flags", ""))
63 for c in e.childNodes:
64 if is_blank(c): continue
66 if c.nodeType != Element.ELEMENT_NODE:
67 log.panic("xml: evil group child %d" % c.nodeType)
68 if c.nodeName == "batch":
69 self.batches.append(Batch(c))
70 elif c.nodeName == "requester":
71 self.requester = text(c)
72 self.requester_email = attr(c, "email", "")
73 elif c.nodeName == "priority":
74 self.priority = int(text(c))
75 elif c.nodeName == "time":
76 self.time = int(text(c))
77 elif c.nodeName == "maxjobs":
78 self.max_jobs = int(text(c))
80 log.panic("xml: evil group child (%s)" % c.nodeName)
81 # note that we also check that group is sorted WRT deps
83 for b in self.batches:
86 for dep in b.depends_on:
89 if id(m[dep]) != id(b):
92 log.panic("xml: dependency not found in group")
94 if self.requester_email == "" and self.requester != "":
95 self.requester_email = acl.user(self.requester).mail_to()
98 f.write("group: %d (id=%s pri=%d)\n" % (self.no, self.id, self.priority))
99 f.write(" from: %s\n" % self.requester)
100 f.write(" flags: %s\n" % string.join(self.flags))
101 f.write(" time: %s\n" % time.asctime(time.localtime(self.time)))
102 for b in self.batches:
106 def dump_html(self, f):
108 "<div id=\"%(no)d\" class=\"request %(flags)s\">\n"
109 "<a href=\"#%(no)d\">%(no)d</a>. "
110 "<time class=\"timeago\" datetime=\"%(datetime)s\">%(time)s</time> "
111 "from <b class=requester>%(requester)s</b> "
112 "<small>%(id)s, prio=%(priority)d, jobs=%(max_jobs)d, %(flags)s</small>\n"
115 'id': '<a href="srpms/%(id)s">%(id)s</a>' % {'id': self.id},
116 'time': escape(time.strftime("%a %b %d %Y %H:%M:%S %z", time.localtime(self.time))),
117 'datetime': escape(iso8601(self.time)),
118 'requester': escape(self.requester),
119 'priority': self.priority,
120 'max_jobs': self.max_jobs,
121 'flags': string.join(self.flags)
124 for b in self.batches:
125 b.dump_html(f, self.id)
129 def write_to(self, f):
131 <group id="%s" no="%d" flags="%s">
132 <requester email='%s'>%s</requester>
134 <priority>%d</priority>
135 <maxjobs>%d</maxjobs>\n""" % (self.id, self.no, string.join(self.flags),
136 escape(self.requester_email), escape(self.requester),
137 self.time, self.priority, self.max_jobs))
138 for b in self.batches:
140 f.write(" </group>\n\n")
144 for b in self.batches:
150 def __init__(self, e):
151 self.bconds_with = []
152 self.bconds_without = []
154 self.builders_status = {}
155 self.builders_status_time = {}
156 self.builders_status_buildtime = {}
165 self.command_flags = []
168 self.b_id = attr(e, "id")
169 self.depends_on = string.split(attr(e, "depends-on"))
176 def get_topdir(self):
177 if not self.__topdir:
178 self.__topdir = tempfile.mkdtemp(prefix='B.', dir='/tmp')
181 def parse_xml(self, e):
182 for c in e.childNodes:
183 if is_blank(c): continue
185 if c.nodeType != Element.ELEMENT_NODE:
186 log.panic("xml: evil batch child %d" % c.nodeType)
187 if c.nodeName == "src-rpm":
188 self.src_rpm = text(c)
189 elif c.nodeName == "spec":
190 # normalize specname, specname is used as buildlog and we don't
191 # want to be exposed to directory traversal attacks
192 self.spec = text(c).split('/')[-1]
193 elif c.nodeName == "command":
194 self.spec = "COMMAND"
195 self.command = text(c).strip()
196 self.command_flags = string.split(attr(c, "flags", ""))
197 elif c.nodeName == "info":
199 elif c.nodeName == "kernel":
200 self.kernel = text(c)
201 elif c.nodeName == "define":
202 define = attr(c, "name")
203 self.defines[define] = text(c)
204 elif c.nodeName == "target":
205 self.target.append(text(c))
206 elif c.nodeName == "skip":
207 self.skip.append(text(c))
208 elif c.nodeName == "branch":
209 self.branch = text(c)
210 elif c.nodeName == "builder":
212 self.builders.append(key)
213 self.builders_status[key] = attr(c, "status", "?")
214 self.builders_status_time[key] = attr(c, "time", "0")
215 self.builders_status_buildtime[key] = "0" #attr(c, "buildtime", "0")
216 elif c.nodeName == "with":
217 self.bconds_with.append(text(c))
218 elif c.nodeName == "without":
219 self.bconds_without.append(text(c))
221 log.panic("xml: evil batch child (%s)" % c.nodeName)
223 def get_package_name(self):
224 if len(self.spec) <= 5:
226 return self.spec[:-5]
230 return tmpdir for this batch job building
232 # it's better to have TMPDIR and BUILD dir on same partition:
233 # + /usr/bin/bzip2 -dc /home/services/builder/rpm/packages/kernel/patch-2.6.27.61.bz2
234 # patch: **** Can't rename file /tmp/B.a1b1d3/poKWwRlp to drivers/scsi/hosts.c : No such file or directory
235 path = os.path.join(self.get_topdir(), 'BUILD', 'tmp')
240 for b in self.builders:
241 s = self.builders_status[b]
242 if not s.startswith("OK") and not s.startswith("SKIP") and not s.startswith("UNSUPP") and not s.startswith("FAIL"):
247 f.write(" batch: %s/%s\n" % (self.src_rpm, self.spec))
248 f.write(" info: %s\n" % self.info)
249 f.write(" kernel: %s\n" % self.kernel)
250 f.write(" defines: %s\n" % self.defines_string())
251 f.write(" target: %s\n" % self.target_string())
252 f.write(" branch: %s\n" % self.branch)
253 f.write(" bconds: %s\n" % self.bconds_string())
255 for b in self.builders:
256 builders.append("%s:%s" % (b, self.builders_status[b]))
257 f.write(" builders: %s\n" % string.join(builders))
259 def is_command(self):
260 return self.command != ""
262 def dump_html(self, f, rid):
264 if self.is_command():
265 desc = "SH: <pre>%s</pre> flags: [%s]" % (self.command, ' '.join(self.command_flags))
267 cmd = "/usr/bin/git ls-remote --heads git://git.tld-linux.org/packages/%s 1>/dev/null 2>&1" % (self.spec[:-5])
268 r = call(cmd, shell=True)
273 package_url = "http://git.%(dist)s-linux.org/?p=packages/%(package)s.git;a=blob;f=%(spec)s;hb=%(branch)s" % {
275 'spec': urllib.quote(self.spec),
276 'branch': urllib.quote(self.branch),
277 'package': urllib.quote(self.spec[:-5]),
279 desc = "%(src_rpm)s (<a href=\"%(package_url)s\">%(spec)s -r %(branch)s</a>%(rpmopts)s)" % {
280 'src_rpm': self.src_rpm,
282 'branch': self.branch,
283 'rpmopts': self.bconds_string() + self.kernel_string() + self.target_string() + self.defines_string(),
284 'package_url': package_url,
286 f.write("%s <small>[" % desc)
288 for b in self.builders:
289 s = self.builders_status[b]
290 if s.startswith("OK"):
292 elif s.startswith("FAIL"):
294 elif s.startswith("SKIP"):
296 elif s.startswith("UNSUPP"):
302 if (s.startswith("OK") or s.startswith("SKIP") or s.startswith("UNSUPP") or s.startswith("FAIL")) and len(self.spec) > 5:
303 if self.is_command():
306 bl_name = self.spec[:len(self.spec)-5]
307 lin_ar = b.replace('noauto-','')
308 path = "/%s/%s/%s,%s.bz2" % (lin_ar.replace('-','/'), s, bl_name, rid)
310 if s.startswith("OK"):
312 bld = lin_ar.split('-')
313 tree_name = '-'.join(bld[:-1])
314 tree_arch = '-'.join(bld[-1:])
315 link_pre = "<a href=\"http://buildlogs.tld-linux.org/index.php?dist=%s&arch=%s&name=%s&id=%s&action=download\">" \
316 % (urllib.quote(tree_name), urllib.quote(tree_arch), urllib.quote(bl_name), urllib.quote(rid))
322 return time.asctime(time.localtime(t))
326 tooltip = "last update: %(time)s\nbuild time: %(buildtime)s" % {
327 'time' : ftime(self.builders_status_time[b]),
328 'buildtime' : ftime(self.builders_status_buildtime[b]),
330 builders.append(link_pre +
331 "<font color='%(color)s'><b title=\"%(tooltip)s\">%(builder)s:%(status)s</b></font>" % {
335 'tooltip' : cgi.escape(tooltip, True),
338 f.write("%s]</small></li>\n" % string.join(builders))
340 def rpmbuild_opts(self):
342 return all rpmbuild options related to this build
344 rpmopts = self.bconds_string() + self.kernel_string() + self.target_string() + self.defines_string()
346 "--define '_topdir %s' " % self.get_topdir() + \
347 "--define '_specdir %{_topdir}' " \
348 "--define '_sourcedir %{_specdir}' " \
349 "--define '_rpmdir %{_topdir}/RPMS' " \
350 "--define '_builddir %{_topdir}/BUILD' "
351 return rpmdefs + rpmopts
353 def php_ignores(self):
354 # transform php package name (52) to version (5.2)
355 def php_name_to_ver(v):
356 return '.'.join(list(v))
358 # transform php version (5.2) to package name (52)
359 def php_ver_to_name(v):
360 return v.replace('.', '')
362 # available php versions in distro
363 php_versions = ['4', '5.2', '5.3', '5.4', '5.5', '5.6', '7.0']
365 # current version if -D php_suffix is present
366 php_version = php_name_to_ver(self.defines['php_suffix'])
368 # remove current php version
370 php_versions.remove(php_version)
372 log.notice("Attempt to remove inexistent key '%s' from %s" % (php_version, php_versions))
375 # map them to poldek ignores
378 for v in map(php_ver_to_name, php_versions):
379 res.append("php%s-*" % v)
383 # build ignore package list
384 # currently only php ignore is filled based on build context
388 # add php version based ignores
389 if self.defines.has_key('php_suffix'):
390 ignores.extend(self.php_ignores())
392 # return empty string if the list is empty
393 if len(ignores) == 0:
397 return "--ignore=%s" % s
399 return " ".join(map(add_ignore, ignores))
401 def kernel_string(self):
403 if self.kernel != "":
404 r = " --define 'alt_kernel " + self.kernel + "'"
407 def target_string(self):
408 if len(self.target) > 0:
409 return " --target " + ",".join(self.target)
413 def bconds_string(self):
415 for b in self.bconds_with:
416 r = r + " --with " + b
417 for b in self.bconds_without:
418 r = r + " --without " + b
421 def defines_string(self):
423 for key,value in self.defines.items():
424 r += " --define '%s %s'" % (key, value)
427 def defines_xml(self):
429 for key,value in self.defines.items():
430 r += "<define name='%s'>%s</define>\n" % (escape(key), escape(value))
433 def default_target(self, arch):
434 self.target.append("%s-tld-linux" % arch)
436 def write_to(self, f):
438 <batch id='%s' depends-on='%s'>
439 <src-rpm>%s</src-rpm>
440 <command flags="%s">%s</command>
443 <info>%s</info>\n""" % (self.b_id,
444 string.join(map(lambda (b): b.b_id, self.depends_on)),
445 escape(self.src_rpm),
446 escape(' '.join(self.command_flags)), escape(self.command),
447 escape(self.spec), escape(self.branch), escape(self.info)))
448 if self.kernel != "":
449 f.write(" <kernel>%s</kernel>\n" % escape(self.kernel))
450 for b in self.bconds_with:
451 f.write(" <with>%s</with>\n" % escape(b))
452 for b in self.target:
453 f.write(" <target>%s</target>\n" % escape(b))
454 for b in self.bconds_without:
455 f.write(" <without>%s</without>\n" % escape(b))
457 f.write(" %s\n" % self.defines_xml())
458 for b in self.builders:
459 if self.builders_status_buildtime.has_key(b):
460 t = self.builders_status_buildtime[b]
463 f.write(" <builder status='%s' time='%s' buildtime='%s'>%s</builder>\n" % \
464 (escape(self.builders_status[b]), self.builders_status_time[b], t, escape(b)))
465 f.write(" </batch>\n")
467 def log_line(self, l):
469 if self.logfile != None:
470 util.append_to(self.logfile, l)
472 def expand_builders(batch, all_builders):
474 for bld in batch.builders:
476 for my_bld in all_builders:
477 if fnmatch.fnmatch(my_bld, bld):
486 def __init__(self, e):
488 self.kind = 'notification'
489 self.group_id = attr(e, "group-id")
490 self.builder = attr(e, "builder")
492 self.batches_buildtime = {}
493 for c in e.childNodes:
494 if is_blank(c): continue
495 if c.nodeType != Element.ELEMENT_NODE:
496 log.panic("xml: evil notification child %d" % c.nodeType)
497 if c.nodeName == "batch":
499 status = attr(c, "status")
500 buildtime = attr(c, "buildtime", "0")
501 if not status.startswith("OK") and not status.startswith("SKIP") and not status.startswith("UNSUPP") and not status.startswith("FAIL"):
502 log.panic("xml notification: bad status: %s" % status)
503 self.batches[id] = status
504 self.batches_buildtime[id] = buildtime
506 log.panic("xml: evil notification child (%s)" % c.nodeName)
508 def apply_to(self, q):
510 if r.kind == "group":
512 if self.batches.has_key(b.b_id):
513 b.builders_status[self.builder] = self.batches[b.b_id]
514 b.builders_status_time[self.builder] = time.time()
515 b.builders_status_buildtime[self.builder] = "0" #self.batches_buildtime[b.b_id]
517 def build_request(e):
518 if e.nodeType != Element.ELEMENT_NODE:
519 log.panic("xml: evil request element")
520 if e.nodeName == "group":
522 elif e.nodeName == "notification":
523 return Notification(e)
524 elif e.nodeName == "command":
528 log.panic("xml: evil request [%s]" % e.nodeName)
530 def parse_request(f):
532 return build_request(d.documentElement)
534 def parse_requests(f):
537 for r in d.documentElement.childNodes:
538 if is_blank(r): continue
539 res.append(build_request(r))