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
20 __all__ = ['parse_request', 'parse_requests']
24 for n in e.childNodes:
25 if n.nodeType != Element.TEXT_NODE:
26 log.panic("xml: text expected in <%s>, got %d" % (e.nodeName, n.nodeType))
30 def attr(e, a, default = None):
32 return e.attributes[a].value
39 return xml.sax.saxutils.escape(s)
41 # return timestamp with timezone information
42 # so we could parse it in javascript
44 # as strftime %z is unofficial, and does not work, need to make it numeric ourselves
45 date = time.strftime("%a %b %d %Y %H:%M:%S", time.localtime(t))
46 # NOTE: the altzone is showing CURRENT timezone, not what the "t" reflects
47 # NOTE: when DST is off timezone gets it right, altzone not
49 tzoffset = time.altzone
51 tzoffset = time.timezone
52 tz = '%+05d' % (-tzoffset / 3600 * 100)
53 return date + ' ' + tz
55 # return date in iso8601 format
56 def iso8601(ts, timezone='UTC'):
57 tz = pytz.timezone(timezone)
58 dt = datetime.fromtimestamp(ts, tz)
62 return e.nodeType == Element.TEXT_NODE and string.strip(e.nodeValue) == ""
65 def __init__(self, e):
68 self.id = attr(e, "id")
69 self.no = int(attr(e, "no"))
71 self.time = time.time()
74 self.requester_email = ""
75 self.flags = string.split(attr(e, "flags", ""))
76 for c in e.childNodes:
77 if is_blank(c): continue
79 if c.nodeType != Element.ELEMENT_NODE:
80 log.panic("xml: evil group child %d" % c.nodeType)
81 if c.nodeName == "batch":
82 self.batches.append(Batch(c))
83 elif c.nodeName == "requester":
84 self.requester = text(c)
85 self.requester_email = attr(c, "email", "")
86 elif c.nodeName == "priority":
87 self.priority = int(text(c))
88 elif c.nodeName == "time":
89 self.time = int(text(c))
90 elif c.nodeName == "maxjobs":
91 self.max_jobs = int(text(c))
93 log.panic("xml: evil group child (%s)" % c.nodeName)
94 # note that we also check that group is sorted WRT deps
96 for b in self.batches:
99 for dep in b.depends_on:
102 if id(m[dep]) != id(b):
105 log.panic("xml: dependency not found in group")
107 if self.requester_email == "" and self.requester != "":
108 self.requester_email = acl.user(self.requester).mail_to()
111 f.write("group: %d (id=%s pri=%d)\n" % (self.no, self.id, self.priority))
112 f.write(" from: %s\n" % self.requester)
113 f.write(" flags: %s\n" % string.join(self.flags))
114 f.write(" time: %s\n" % time.asctime(time.localtime(self.time)))
115 for b in self.batches:
119 # return structure usable for json encoding
122 for b in self.batches:
123 batches.append(b.dump_json())
129 requester=self.requester,
130 priority=self.priority,
131 max_jobs=self.max_jobs,
136 def dump_html(self, f):
138 "<div id=\"%(no)d\" class=\"request %(flags)s\">\n"
139 "<a href=\"#%(no)d\">%(no)d</a>. "
140 "<time class=\"timeago\" title=\"%(datetime)s\" datetime=\"%(datetime)s\">%(time)s</time> "
141 "from <b class=requester>%(requester)s</b> "
142 "<small>%(id)s, prio=%(priority)d, jobs=%(max_jobs)d, %(flags)s</small>\n"
145 'id': '<a href="srpms/%(id)s">%(id)s</a>' % {'id': self.id},
146 'time': escape(tzdate(self.time)),
147 'datetime': escape(iso8601(self.time)),
148 'requester': escape(self.requester),
149 'priority': self.priority,
150 'max_jobs': self.max_jobs,
151 'flags': string.join(self.flags)
154 for b in self.batches:
155 b.dump_html(f, self.id)
159 def write_to(self, f):
161 <group id="%s" no="%d" flags="%s">
162 <requester email='%s'>%s</requester>
164 <priority>%d</priority>
165 <maxjobs>%d</maxjobs>\n""" % (self.id, self.no, string.join(self.flags),
166 escape(self.requester_email), escape(self.requester),
167 self.time, self.priority, self.max_jobs))
168 for b in self.batches:
170 f.write(" </group>\n\n")
174 for b in self.batches:
179 # transform php package name (52) to version (5.2)
180 def php_name_to_ver(v):
181 return '.'.join(list(v))
183 # transform php version (5.2) to package name (52)
184 def php_ver_to_name(v):
185 return v.replace('.', '')
190 def __init__(self, e):
191 self.bconds_with = []
192 self.bconds_without = []
194 self.builders_status = {}
195 self.builders_status_time = {}
196 self.builders_status_buildtime = {}
205 self.command_flags = []
208 self.b_id = attr(e, "id")
209 self.depends_on = string.split(attr(e, "depends-on"))
216 def get_topdir(self):
217 if not self.__topdir:
218 self.__topdir = tempfile.mkdtemp(prefix='B.', dir='/tmp')
221 def parse_xml(self, e):
222 for c in e.childNodes:
223 if is_blank(c): continue
225 if c.nodeType != Element.ELEMENT_NODE:
226 log.panic("xml: evil batch child %d" % c.nodeType)
227 if c.nodeName == "src-rpm":
228 self.src_rpm = text(c)
229 elif c.nodeName == "spec":
230 # normalize specname, specname is used as buildlog and we don't
231 # want to be exposed to directory traversal attacks
232 self.spec = text(c).split('/')[-1]
233 elif c.nodeName == "command":
234 self.spec = "COMMAND"
235 self.command = text(c).strip()
236 self.command_flags = string.split(attr(c, "flags", ""))
237 elif c.nodeName == "info":
239 elif c.nodeName == "kernel":
240 self.kernel = text(c)
241 elif c.nodeName == "define":
242 define = attr(c, "name")
243 self.defines[define] = text(c)
244 elif c.nodeName == "target":
245 self.target.append(text(c))
246 elif c.nodeName == "skip":
247 self.skip.append(text(c))
248 elif c.nodeName == "branch":
249 self.branch = text(c)
250 elif c.nodeName == "builder":
252 self.builders.append(key)
253 self.builders_status[key] = attr(c, "status", "?")
254 self.builders_status_time[key] = attr(c, "time", "0")
255 self.builders_status_buildtime[key] = "0" #attr(c, "buildtime", "0")
256 elif c.nodeName == "with":
257 self.bconds_with.append(text(c))
258 elif c.nodeName == "without":
259 self.bconds_without.append(text(c))
261 log.panic("xml: evil batch child (%s)" % c.nodeName)
263 def get_package_name(self):
264 if len(self.spec) <= 5:
266 return self.spec[:-5]
270 return tmpdir for this batch job building
272 # it's better to have TMPDIR and BUILD dir on same partition:
273 # + /usr/bin/bzip2 -dc /home/services/builder/rpm/packages/kernel/patch-2.6.27.61.bz2
274 # patch: **** Can't rename file /tmp/B.a1b1d3/poKWwRlp to drivers/scsi/hosts.c : No such file or directory
275 path = os.path.join(self.get_topdir(), 'BUILD', 'tmp')
280 for b in self.builders:
281 s = self.builders_status[b]
282 if not s.startswith("OK") and not s.startswith("SKIP") and not s.startswith("UNSUPP") and not s.startswith("FAIL"):
287 f.write(" batch: %s/%s\n" % (self.src_rpm, self.spec))
288 f.write(" info: %s\n" % self.info)
289 f.write(" kernel: %s\n" % self.kernel)
290 f.write(" defines: %s\n" % self.defines_string())
291 f.write(" target: %s\n" % self.target_string())
292 f.write(" branch: %s\n" % self.branch)
293 f.write(" bconds: %s\n" % self.bconds_string())
295 for b in self.builders:
296 builders.append("%s:%s" % (b, self.builders_status[b]))
297 f.write(" builders: %s\n" % string.join(builders))
299 def is_command(self):
300 return self.command != ""
302 # return structure usable for json encoding
305 command=self.command,
306 command_flags=self.command_flags,
310 package=self.spec[:-5],
311 src_rpm=self.src_rpm,
313 bconds_with=self.bconds_with,
314 bconds_without=self.bconds_without,
318 defines=self.defines,
320 builders=self.builders,
321 builders_status=self.builders_status,
322 builders_status_time=self.builders_status_time,
323 builders_status_buildtime=self.builders_status_buildtime,
326 def dump_html(self, f, rid):
328 if self.is_command():
329 desc = "SH: <pre>%s</pre> flags: [%s]" % (self.command, ' '.join(self.command_flags))
331 cmd = "/usr/bin/git ls-remote --heads git://git.tld-linux.org/packages/%s 1>/dev/null 2>&1" % (self.spec[:-5])
332 r = call(cmd, shell=True)
334 package_url = "http://git.tld-linux.org/?p=packages/%(package)s.git;a=blob;f=%(spec)s;hb=%(branch)s" % {
335 'spec': urllib.quote(self.spec),
336 'branch': urllib.quote(self.branch),
337 'package': urllib.quote(self.spec[:-5]),
340 package_url = "http://git.pld-linux.org/gitweb.cgi?p=packages/%(package)s.git;f=%(spec)s;h=%(branch)s;a=shortlog" % {
341 'spec': urllib.quote(self.spec),
342 'branch': urllib.quote(self.branch),
343 'package': urllib.quote(self.spec[:-5]),
345 desc = "%(src_rpm)s (<a href=\"%(package_url)s\">%(spec)s -r %(branch)s</a>%(rpmopts)s)" % {
346 'src_rpm': self.src_rpm,
348 'branch': self.branch,
349 'rpmopts': self.bconds_string() + self.kernel_string() + self.target_string() + self.defines_string(),
350 'package_url': package_url,
352 f.write("%s <small>[" % desc)
354 for b in self.builders:
355 s = self.builders_status[b]
356 if s.startswith("OK"):
358 elif s.startswith("FAIL"):
360 elif s.startswith("SKIP"):
362 elif s.startswith("UNSUPP"):
368 if (s.startswith("OK") or s.startswith("SKIP") or s.startswith("UNSUPP") or s.startswith("FAIL")) and len(self.spec) > 5:
369 if self.is_command():
372 bl_name = self.spec[:len(self.spec)-5]
373 lin_ar = b.replace('noauto-','')
374 path = "/%s/%s/%s,%s.bz2" % (lin_ar.replace('-','/'), s, bl_name, rid)
376 if s.startswith("OK"):
378 bld = lin_ar.split('-')
379 tree_name = '-'.join(bld[:-1])
380 tree_arch = '-'.join(bld[-1:])
381 link_pre = "<a href=\"http://buildlogs.tld-linux.org/index.php?dist=%s&arch=%s&name=%s&id=%s&action=download\">" \
382 % (urllib.quote(tree_name), urllib.quote(tree_arch), urllib.quote(bl_name), urllib.quote(rid))
388 return time.asctime(time.localtime(t))
392 tooltip = "last update: %(time)s\nbuild time: %(buildtime)s" % {
393 'time' : ftime(self.builders_status_time[b]),
394 'buildtime' : ftime(self.builders_status_buildtime[b]),
396 builders.append(link_pre +
397 "<font color='%(color)s'><b title=\"%(tooltip)s\">%(builder)s:%(status)s</b></font>" % {
401 'tooltip' : cgi.escape(tooltip, True),
404 f.write("%s]</small></li>\n" % string.join(builders))
406 def rpmbuild_opts(self):
408 return all rpmbuild options related to this build
410 rpmopts = self.bconds_string() + self.kernel_string() + self.target_string() + self.defines_string()
412 "--define '_topdir %s' " % self.get_topdir() + \
413 "--define '_specdir %{_topdir}' " \
414 "--define '_sourcedir %{_specdir}' " \
415 "--define '_rpmdir %{_topdir}/RPMS' " \
416 "--define '_builddir %{_topdir}/BUILD' "
417 return rpmdefs + rpmopts
419 def php_ignores(self, php_version):
420 # available php versions in distro
421 php_versions = ['7.2', '7.3', '7.4', '8.0']
423 # remove current php version
425 php_versions.remove(php_version)
427 log.notice("Attempt to remove inexistent key '%s' from %s" % (php_version, php_versions))
430 # map them to poldek ignores
433 for v in list(map(php_ver_to_name, php_versions)):
434 res.append("php%s-*" % v)
438 # build ignore package list
439 # currently only php ignore is filled based on build context
443 # add php version based ignores
444 if self.defines.has_key('php_suffix'):
445 # current version if -D php_suffix is present
446 php_version = php_name_to_ver(self.defines['php_suffix'])
448 php_version = self.DEFAULT_PHP
450 ignores.extend(self.php_ignores(php_version))
452 # return empty string if the list is empty
453 if len(ignores) == 0:
457 return "--ignore=%s" % s
459 return " ".join(list(map(add_ignore, ignores)))
461 def kernel_string(self):
463 if self.kernel != "":
464 r = " --define 'alt_kernel " + self.kernel + "'"
467 def target_string(self):
468 if len(self.target) > 0:
469 return " --target " + ",".join(self.target)
473 def bconds_string(self):
475 for b in self.bconds_with:
476 r = r + " --with " + b
477 for b in self.bconds_without:
478 r = r + " --without " + b
481 def defines_string(self):
483 for key,value in self.defines.items():
484 r += " --define '%s %s'" % (key, value)
487 def defines_xml(self):
489 for key,value in self.defines.items():
490 r += "<define name='%s'>%s</define>\n" % (escape(key), escape(value))
493 def default_target(self, arch):
494 self.target.append("%s-tld-linux" % arch)
496 def write_to(self, f):
498 <batch id='%s' depends-on='%s'>
499 <src-rpm>%s</src-rpm>
500 <command flags="%s">%s</command>
503 <info>%s</info>\n""" % (self.b_id,
504 string.join(list(map(lambda (b): b.b_id, self.depends_on))),
505 escape(self.src_rpm),
506 escape(' '.join(self.command_flags)), escape(self.command),
507 escape(self.spec), escape(self.branch), escape(self.info)))
508 if self.kernel != "":
509 f.write(" <kernel>%s</kernel>\n" % escape(self.kernel))
510 for b in self.bconds_with:
511 f.write(" <with>%s</with>\n" % escape(b))
512 for b in self.target:
513 f.write(" <target>%s</target>\n" % escape(b))
514 for b in self.bconds_without:
515 f.write(" <without>%s</without>\n" % escape(b))
517 f.write(" %s\n" % self.defines_xml())
518 for b in self.builders:
519 if self.builders_status_buildtime.has_key(b):
520 t = self.builders_status_buildtime[b]
523 f.write(" <builder status='%s' time='%s' buildtime='%s'>%s</builder>\n" % \
524 (escape(self.builders_status[b]), self.builders_status_time[b], t, escape(b)))
525 f.write(" </batch>\n")
527 def log_line(self, l):
529 if self.logfile != None:
530 util.append_to(self.logfile, l)
532 def expand_builders(batch, all_builders):
534 for bld in batch.builders:
536 for my_bld in all_builders:
537 if fnmatch.fnmatch(my_bld, bld):
546 def __init__(self, e):
548 self.kind = 'notification'
549 self.group_id = attr(e, "group-id")
550 self.builder = attr(e, "builder")
552 self.batches_buildtime = {}
553 for c in e.childNodes:
554 if is_blank(c): continue
555 if c.nodeType != Element.ELEMENT_NODE:
556 log.panic("xml: evil notification child %d" % c.nodeType)
557 if c.nodeName == "batch":
559 status = attr(c, "status")
560 buildtime = attr(c, "buildtime", "0")
561 if not status.startswith("OK") and not status.startswith("SKIP") and not status.startswith("UNSUPP") and not status.startswith("FAIL"):
562 log.panic("xml notification: bad status: %s" % status)
563 self.batches[id] = status
564 self.batches_buildtime[id] = buildtime
566 log.panic("xml: evil notification child (%s)" % c.nodeName)
568 # return structure usable for json encoding
572 builder=self.builder,
573 batches=self.batches,
574 batches_buildtime=self.batches_buildtime,
577 def apply_to(self, q):
579 if r.kind == "group":
581 if self.batches.has_key(b.b_id):
582 b.builders_status[self.builder] = self.batches[b.b_id]
583 b.builders_status_time[self.builder] = time.time()
584 b.builders_status_buildtime[self.builder] = "0" #self.batches_buildtime[b.b_id]
586 def build_request(e):
587 if e.nodeType != Element.ELEMENT_NODE:
588 log.panic("xml: evil request element")
589 if e.nodeName == "group":
591 elif e.nodeName == "notification":
592 return Notification(e)
593 elif e.nodeName == "command":
597 log.panic("xml: evil request [%s]" % e.nodeName)
599 def parse_request(f):
601 return build_request(d.documentElement)
603 def parse_requests(f):
606 for r in d.documentElement.childNodes:
607 if is_blank(r): continue
608 res.append(build_request(r))