1 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
3 from xml.dom.minidom import *
6 import xml.sax.saxutils
15 from config import config
17 __all__ = ['parse_request', 'parse_requests']
21 for n in e.childNodes:
22 if n.nodeType != Element.TEXT_NODE:
23 log.panic("xml: text expected in <%s>, got %d" % (e.nodeName, n.nodeType))
27 def attr(e, a, default = None):
29 return e.attributes[a].value
36 return xml.sax.saxutils.escape(s)
38 # return timestamp with timezone information
39 # so we could parse it in javascript
41 # as strftime %z is unofficial, and does not work, need to make it numeric ourselves
42 # date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(t))
43 date = time.strftime("%a %b %d %Y %H:%M:%S", time.localtime(t))
44 # NOTE: the altzone is showing CURRENT timezone, not what the "t" reflects
45 # NOTE: when DST is off timezone gets it right, altzone not
47 tzoffset = time.altzone
49 tzoffset = time.timezone
50 tz = '%+05d' % (-tzoffset / 3600 * 100)
51 return date + ' ' + tz
54 return e.nodeType == Element.TEXT_NODE and string.strip(e.nodeValue) == ""
57 def __init__(self, e):
60 self.id = attr(e, "id")
61 self.no = int(attr(e, "no"))
63 self.time = time.time()
66 self.requester_email = ""
67 self.flags = string.split(attr(e, "flags", ""))
68 for c in e.childNodes:
69 if is_blank(c): continue
71 if c.nodeType != Element.ELEMENT_NODE:
72 log.panic("xml: evil group child %d" % c.nodeType)
73 if c.nodeName == "batch":
74 self.batches.append(Batch(c))
75 elif c.nodeName == "requester":
76 self.requester = text(c)
77 self.requester_email = attr(c, "email", "")
78 elif c.nodeName == "priority":
79 self.priority = int(text(c))
80 elif c.nodeName == "time":
81 self.time = int(text(c))
82 elif c.nodeName == "maxjobs":
83 self.max_jobs = int(text(c))
85 log.panic("xml: evil group child (%s)" % c.nodeName)
86 # note that we also check that group is sorted WRT deps
88 for b in self.batches:
91 for dep in b.depends_on:
94 if id(m[dep]) != id(b):
97 log.panic("xml: dependency not found in group")
99 if self.requester_email == "" and self.requester != "":
100 self.requester_email = acl.user(self.requester).mail_to()
103 f.write("group: %d (id=%s pri=%d)\n" % (self.no, self.id, self.priority))
104 f.write(" from: %s\n" % self.requester)
105 f.write(" flags: %s\n" % string.join(self.flags))
106 f.write(" time: %s\n" % time.asctime(time.localtime(self.time)))
107 for b in self.batches:
111 def dump_html(self, f):
113 "<div id=\"%(no)d\" class=\"request %(flags)s\">\n"
114 "<a href=\"#%(no)d\">%(no)d</a>. <span id=\"tz\">%(time)s</span> from <b class=requester>%(requester)s</b> "
115 "<small>%(id)s, prio=%(priority)d, jobs=%(max_jobs)d, %(flags)s</small>\n"
118 'id': '<a href="srpms/%(id)s">%(id)s</a>' % {'id': self.id},
119 'time': escape(tzdate(self.time)),
120 'requester': escape(self.requester),
121 'priority': self.priority,
122 'max_jobs': self.max_jobs,
123 'flags': string.join(self.flags)
126 for b in self.batches:
127 b.dump_html(f, self.id)
131 def write_to(self, f):
133 <group id="%s" no="%d" flags="%s">
134 <requester email='%s'>%s</requester>
136 <priority>%d</priority>
137 <maxjobs>%d</maxjobs>\n""" % (self.id, self.no, string.join(self.flags),
138 escape(self.requester_email), escape(self.requester),
139 self.time, self.priority, self.max_jobs))
140 for b in self.batches:
142 f.write(" </group>\n\n")
146 for b in self.batches:
152 def __init__(self, e):
153 self.bconds_with = []
154 self.bconds_without = []
156 self.builders_status = {}
157 self.builders_status_time = {}
158 self.builders_status_buildtime = {}
167 self.command_flags = []
170 self.b_id = attr(e, "id")
171 self.depends_on = string.split(attr(e, "depends-on"))
176 self._topdir = '/tmp/B.%s' % self.b_id
178 def parse_xml(self, e):
179 for c in e.childNodes:
180 if is_blank(c): continue
182 if c.nodeType != Element.ELEMENT_NODE:
183 log.panic("xml: evil batch child %d" % c.nodeType)
184 if c.nodeName == "src-rpm":
185 self.src_rpm = text(c)
186 elif c.nodeName == "spec":
187 # normalize specname, specname is used as buildlog and we don't
188 # want to be exposed to directory traversal attacks
189 self.spec = text(c).split('/')[-1]
190 elif c.nodeName == "command":
191 self.spec = "COMMAND"
192 self.command = text(c).strip()
193 self.command_flags = string.split(attr(c, "flags", ""))
194 elif c.nodeName == "info":
196 elif c.nodeName == "kernel":
197 self.kernel = text(c)
198 elif c.nodeName == "define":
199 define = attr(c, "name")
200 self.defines[define] = text(c)
201 elif c.nodeName == "target":
202 self.target.append(text(c))
203 elif c.nodeName == "skip":
204 self.skip.append(text(c))
205 elif c.nodeName == "branch":
206 self.branch = text(c)
207 elif c.nodeName == "builder":
209 self.builders.append(key)
210 self.builders_status[key] = attr(c, "status", "?")
211 self.builders_status_time[key] = attr(c, "time", "0")
212 self.builders_status_buildtime[key] = "0" #attr(c, "buildtime", "0")
213 elif c.nodeName == "with":
214 self.bconds_with.append(text(c))
215 elif c.nodeName == "without":
216 self.bconds_without.append(text(c))
218 log.panic("xml: evil batch child (%s)" % c.nodeName)
220 def get_package_name(self):
221 if len(self.spec) <= 5:
223 return self.spec[:-5]
227 return tmpdir for this batch job building
229 # it's better to have TMPDIR and BUILD dir on same partition:
230 # + /usr/bin/bzip2 -dc /home/services/builder/rpm/packages/kernel/patch-2.6.27.61.bz2
231 # patch: **** Can't rename file /tmp/B.a1b1d3/poKWwRlp to drivers/scsi/hosts.c : No such file or directory
232 path = os.path.join(self._topdir, 'BUILD', 'tmp')
237 for b in self.builders:
238 s = self.builders_status[b]
239 if not s.startswith("OK") and not s.startswith("SKIP") and not s.startswith("UNSUPP") and not s.startswith("FAIL"):
244 f.write(" batch: %s/%s\n" % (self.src_rpm, self.spec))
245 f.write(" info: %s\n" % self.info)
246 f.write(" kernel: %s\n" % self.kernel)
247 f.write(" defines: %s\n" % self.defines_string())
248 f.write(" target: %s\n" % self.target_string())
249 f.write(" branch: %s\n" % self.branch)
250 f.write(" bconds: %s\n" % self.bconds_string())
252 for b in self.builders:
253 builders.append("%s:%s" % (b, self.builders_status[b]))
254 f.write(" builders: %s\n" % string.join(builders))
256 def is_command(self):
257 return self.command != ""
259 def dump_html(self, f, rid):
261 if self.is_command():
262 desc = "SH: <pre>%s</pre> flags: [%s]" % (self.command, ' '.join(self.command_flags))
264 package_url = "http://git.pld-linux.org/gitweb.cgi?p=packages/%(package)s.git;f=%(spec)s;h=%(branch)s;a=shortlog" % {
265 'spec': urllib.quote(self.spec),
266 'branch': urllib.quote(self.branch),
267 'package': urllib.quote(self.spec[:-5]),
269 desc = "%(src_rpm)s (<a href=\"%(package_url)s\">%(spec)s -r %(branch)s</a>%(rpmopts)s)" % {
270 'src_rpm': self.src_rpm,
272 'branch': self.branch,
273 'rpmopts': self.bconds_string() + self.kernel_string() + self.target_string() + self.defines_string(),
274 'package_url': package_url,
276 f.write("%s <small>[" % desc)
278 for b in self.builders:
279 s = self.builders_status[b]
280 if s.startswith("OK"):
282 elif s.startswith("FAIL"):
284 elif s.startswith("SKIP"):
286 elif s.startswith("UNSUPP"):
292 if (s.startswith("OK") or s.startswith("SKIP") or s.startswith("UNSUPP") or s.startswith("FAIL")) and len(self.spec) > 5:
293 if self.is_command():
296 bl_name = self.spec[:len(self.spec)-5]
297 lin_ar = b.replace('noauto-','')
298 path = "/%s/%s/%s,%s.bz2" % (lin_ar.replace('-','/'), s, bl_name, rid)
300 if s.startswith("OK"):
302 bld = lin_ar.split('-')
303 tree_name = '-'.join(bld[:-1])
304 tree_arch = '-'.join(bld[-1:])
305 link_pre = "<a href=\"%s/index.php?dist=%s&arch=%s&ok=%d&name=%s&id=%s&action=tail\">" \
306 % (config.buildlogs, urllib.quote(tree_name), urllib.quote(tree_arch), is_ok, urllib.quote(bl_name), urllib.quote(rid))
312 return time.asctime(time.localtime(t))
316 tooltip = "last update: %(time)s\nbuild time: %(buildtime)s" % {
317 'time' : ftime(self.builders_status_time[b]),
318 'buildtime' : ftime(self.builders_status_buildtime[b]),
320 builders.append(link_pre +
321 "<font color='%(color)s'><b title=\"%(tooltip)s\">%(builder)s:%(status)s</b></font>" % {
325 'tooltip' : cgi.escape(tooltip, True),
328 f.write("%s]</small></li>\n" % string.join(builders))
330 def rpmbuild_opts(self):
332 return all rpmbuild options related to this build
334 rpmopts = self.bconds_string() + self.kernel_string() + self.target_string() + self.defines_string()
336 "--define '_topdir %s' " % self._topdir + \
337 "--define '_specdir %{_topdir}' " \
338 "--define '_sourcedir %{_specdir}' " \
339 "--define '_rpmdir %{_topdir}/RPMS' " \
340 "--define '_builddir %{_topdir}/BUILD' "
341 return rpmdefs + rpmopts
343 def php_ignores(self):
344 # transform php package name (52) to version (5.2)
345 def php_name_to_ver(v):
346 return '.'.join(list(v))
348 # transform php version (5.2) to package name (52)
349 def php_ver_to_name(v):
350 return v.replace('.', '')
352 # available php versions in distro
353 php_versions = ['5.2', '5.3', '5.4', '5.5', '5.6', '7.0']
355 # current version if -D php_suffix is present
356 php_version = php_name_to_ver(self.defines['php_suffix'])
358 # remove current php version
360 php_versions.remove(php_version)
362 log.notice("Attempt to remove inexistent key '%s' from %s" % (php_version, php_versions))
365 # map them to poldek ignores
368 for v in map(php_ver_to_name, php_versions):
369 res.append("php%s-*" % v)
373 # build ignore package list
374 # currently only php ignore is filled based on build context
378 # add php version based ignores
379 if self.defines.has_key('php_suffix'):
380 ignores.extend(self.php_ignores())
382 # return empty string if the list is empty
383 if len(ignores) == 0:
387 return "--ignore=%s" % s
389 return " ".join(map(add_ignore, ignores))
391 def kernel_string(self):
393 if self.kernel != "":
394 r = " --define 'alt_kernel " + self.kernel + "'"
397 def target_string(self):
398 if len(self.target) > 0:
399 return " --target " + ",".join(self.target)
403 def bconds_string(self):
405 for b in self.bconds_with:
406 r = r + " --with " + b
407 for b in self.bconds_without:
408 r = r + " --without " + b
411 def defines_string(self):
413 for key,value in self.defines.items():
414 r += " --define '%s %s'" % (key, value)
417 def defines_xml(self):
419 for key,value in self.defines.items():
420 r += "<define name='%s'>%s</define>\n" % (escape(key), escape(value))
423 def default_target(self, arch):
424 self.target.append("%s-pld-linux" % arch)
426 def write_to(self, f):
428 <batch id='%s' depends-on='%s'>
429 <src-rpm>%s</src-rpm>
430 <command flags="%s">%s</command>
433 <info>%s</info>\n""" % (self.b_id,
434 string.join(map(lambda (b): b.b_id, self.depends_on)),
435 escape(self.src_rpm),
436 escape(' '.join(self.command_flags)), escape(self.command),
437 escape(self.spec), escape(self.branch), escape(self.info)))
438 if self.kernel != "":
439 f.write(" <kernel>%s</kernel>\n" % escape(self.kernel))
440 for b in self.bconds_with:
441 f.write(" <with>%s</with>\n" % escape(b))
442 for b in self.target:
443 f.write(" <target>%s</target>\n" % escape(b))
444 for b in self.bconds_without:
445 f.write(" <without>%s</without>\n" % escape(b))
447 f.write(" %s\n" % self.defines_xml())
448 for b in self.builders:
449 if self.builders_status_buildtime.has_key(b):
450 t = self.builders_status_buildtime[b]
453 f.write(" <builder status='%s' time='%s' buildtime='%s'>%s</builder>\n" % \
454 (escape(self.builders_status[b]), self.builders_status_time[b], t, escape(b)))
455 f.write(" </batch>\n")
457 def log_line(self, l):
459 if self.logfile != None:
460 util.append_to(self.logfile, l)
462 def expand_builders(batch, all_builders):
464 for bld in batch.builders:
466 for my_bld in all_builders:
467 if fnmatch.fnmatch(my_bld, bld):
476 def __init__(self, e):
478 self.kind = 'notification'
479 self.group_id = attr(e, "group-id")
480 self.builder = attr(e, "builder")
482 self.batches_buildtime = {}
483 for c in e.childNodes:
484 if is_blank(c): continue
485 if c.nodeType != Element.ELEMENT_NODE:
486 log.panic("xml: evil notification child %d" % c.nodeType)
487 if c.nodeName == "batch":
489 status = attr(c, "status")
490 buildtime = attr(c, "buildtime", "0")
491 if not status.startswith("OK") and not status.startswith("SKIP") and not status.startswith("UNSUPP") and not status.startswith("FAIL"):
492 log.panic("xml notification: bad status: %s" % status)
493 self.batches[id] = status
494 self.batches_buildtime[id] = buildtime
496 log.panic("xml: evil notification child (%s)" % c.nodeName)
498 def apply_to(self, q):
500 if r.kind == "group":
502 if self.batches.has_key(b.b_id):
503 b.builders_status[self.builder] = self.batches[b.b_id]
504 b.builders_status_time[self.builder] = time.time()
505 b.builders_status_buildtime[self.builder] = "0" #self.batches_buildtime[b.b_id]
507 def build_request(e):
508 if e.nodeType != Element.ELEMENT_NODE:
509 log.panic("xml: evil request element")
510 if e.nodeName == "group":
512 elif e.nodeName == "notification":
513 return Notification(e)
514 elif e.nodeName == "command":
518 log.panic("xml: evil request [%s]" % e.nodeName)
520 def parse_request(f):
522 return build_request(d.documentElement)
524 def parse_requests(f):
527 for r in d.documentElement.childNodes:
528 if is_blank(r): continue
529 res.append(build_request(r))