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=\"%(flags)s\">\n"
114 "<a href=\"#%(no)d\">%(no)d</a>. <span id=\"tz\">%(time)s</span> from <b>%(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 = {}
166 self.command_flags = []
169 self.b_id = attr(e, "id")
170 self.depends_on = string.split(attr(e, "depends-on"))
172 for c in e.childNodes:
173 if is_blank(c): continue
175 if c.nodeType != Element.ELEMENT_NODE:
176 log.panic("xml: evil batch child %d" % c.nodeType)
177 if c.nodeName == "src-rpm":
178 self.src_rpm = text(c)
179 elif c.nodeName == "spec":
180 # normalize specname, specname is used as buildlog and we don't
181 # want to be exposed to directory traversal attacks
182 self.spec = text(c).split('/')[-1]
183 elif c.nodeName == "command":
184 self.spec = "COMMAND"
185 self.command = text(c).strip()
186 self.command_flags = string.split(attr(c, "flags", ""))
187 elif c.nodeName == "info":
189 elif c.nodeName == "kernel":
190 self.kernel = text(c)
191 elif c.nodeName == "target":
192 self.target.append(text(c))
193 elif c.nodeName == "skip":
194 self.skip.append(text(c))
195 elif c.nodeName == "branch":
196 self.branch = text(c)
197 elif c.nodeName == "builder":
199 self.builders.append(key)
200 self.builders_status[key] = attr(c, "status", "?")
201 self.builders_status_time[key] = attr(c, "time", "0")
202 self.builders_status_buildtime[key] = "0" #attr(c, "buildtime", "0")
203 elif c.nodeName == "with":
204 self.bconds_with.append(text(c))
205 elif c.nodeName == "without":
206 self.bconds_without.append(text(c))
208 log.panic("xml: evil batch child (%s)" % c.nodeName)
212 for b in self.builders:
213 s = self.builders_status[b]
214 if not s.startswith("OK") and not s.startswith("SKIP") and not s.startswith("UNSUPP") and not s.startswith("FAIL"):
219 f.write(" batch: %s/%s\n" % (self.src_rpm, self.spec))
220 f.write(" info: %s\n" % self.info)
221 f.write(" kernel: %s\n" % self.kernel)
222 f.write(" target: %s\n" % self.target_string())
223 f.write(" branch: %s\n" % self.branch)
224 f.write(" bconds: %s\n" % self.bconds_string())
226 for b in self.builders:
227 builders.append("%s:%s" % (b, self.builders_status[b]))
228 f.write(" builders: %s\n" % string.join(builders))
230 def is_command(self):
231 return self.command != ""
233 def dump_html(self, f, rid):
235 if self.is_command():
236 desc = "SH: <pre>%s</pre> flags: [%s]" % (self.command, ' '.join(self.command_flags))
238 package_url = "http://git.pld-linux.org/gitweb.cgi?p=packages/%(package)s.git;f=%(spec)s;h=%(branch)s;a=shortlog" % {
240 'branch': self.branch,
241 'package': self.spec[:-5],
243 desc = "%(src_rpm)s (<a href=\"%(package_url)s\">%(spec)s -r %(branch)s</a>%(bconds)s)" % {
244 'src_rpm': self.src_rpm,
246 'branch': self.branch,
247 'bconds': self.bconds_string() + self.kernel_string() + self.target_string(),
248 'package_url': package_url,
250 f.write("%s <small>[" % desc)
252 for b in self.builders:
253 s = self.builders_status[b]
254 if s.startswith("OK"):
256 elif s.startswith("FAIL"):
258 elif s.startswith("SKIP"):
260 elif s.startswith("UNSUPP"):
266 if (s.startswith("OK") or s.startswith("SKIP") or s.startswith("UNSUPP") or s.startswith("FAIL")) and len(self.spec) > 5:
267 if self.is_command():
270 bl_name = self.spec[:len(self.spec)-5]
271 lin_ar = b.replace('noauto-','')
272 path = "/%s/%s/%s,%s.bz2" % (lin_ar.replace('-','/'), s, bl_name, rid)
274 if s.startswith("OK"):
276 bld = lin_ar.split('-')
277 tree_name = '-'.join(bld[:-1])
278 tree_arch = '-'.join(bld[-1:])
279 link_pre = "<a href=\"http://buildlogs.pld-linux.org/index.php?dist=%s&arch=%s&ok=%d&name=%s&id=%s&action=tail\">" \
280 % (urllib.quote(tree_name), urllib.quote(tree_arch), is_ok, urllib.quote(bl_name), urllib.quote(rid))
286 return time.asctime(time.localtime(t))
290 tooltip = "last update: %(time)s\nbuild time: %(buildtime)s" % {
291 'time' : ftime(self.builders_status_time[b]),
292 'buildtime' : ftime(self.builders_status_buildtime[b]),
294 builders.append(link_pre +
295 "<font color='%(color)s'><b title=\"%(tooltip)s\">%(builder)s:%(status)s</b></font>" % {
299 'tooltip' : cgi.escape(tooltip, True),
302 f.write("%s]</small></li>\n" % string.join(builders))
304 def rpmbuild_opts(self):
306 return all rpmbuild options related to this build
308 bconds = self.bconds_string() + self.kernel_string() + self.target_string()
310 "--define '_topdir %(echo $HOME/rpm)' " \
311 "--define '_specdir %{_topdir}/packages/%{name}' " \
312 "--define '_sourcedir %{_specdir}' " \
313 "--define '_builddir %{_topdir}/BUILD/%{name}' "
314 return rpmdefs + bconds
316 def kernel_string(self):
318 if self.kernel != "":
319 r = " --define 'alt_kernel " + self.kernel + "'"
322 def target_string(self):
323 if len(self.target) > 0:
324 return " --target " + ",".join(self.target)
328 def bconds_string(self):
330 for b in self.bconds_with:
331 r = r + " --with " + b
332 for b in self.bconds_without:
333 r = r + " --without " + b
336 def default_target(self, arch):
337 self.target.append("%s-pld-linux" % arch)
339 def write_to(self, f):
341 <batch id='%s' depends-on='%s'>
342 <src-rpm>%s</src-rpm>
343 <command flags="%s">%s</command>
346 <info>%s</info>\n""" % (self.b_id,
347 string.join(map(lambda (b): b.b_id, self.depends_on)),
348 escape(self.src_rpm),
349 escape(' '.join(self.command_flags)), escape(self.command),
350 escape(self.spec), escape(self.branch), escape(self.info)))
351 if self.kernel != "":
352 f.write(" <kernel>%s</kernel>\n" % escape(self.kernel))
353 for b in self.bconds_with:
354 f.write(" <with>%s</with>\n" % escape(b))
355 for b in self.target:
356 f.write(" <target>%s</target>\n" % escape(b))
357 for b in self.bconds_without:
358 f.write(" <without>%s</without>\n" % escape(b))
359 for b in self.builders:
360 if self.builders_status_buildtime.has_key(b):
361 t = self.builders_status_buildtime[b]
364 f.write(" <builder status='%s' time='%s' buildtime='%s'>%s</builder>\n" % \
365 (escape(self.builders_status[b]), self.builders_status_time[b], t, escape(b)))
366 f.write(" </batch>\n")
368 def log_line(self, l):
370 if self.logfile != None:
371 util.append_to(self.logfile, l)
373 def expand_builders(batch, all_builders):
375 for bld in batch.builders:
377 for my_bld in all_builders:
378 if fnmatch.fnmatch(my_bld, bld):
387 def __init__(self, e):
389 self.kind = 'notification'
390 self.group_id = attr(e, "group-id")
391 self.builder = attr(e, "builder")
393 self.batches_buildtime = {}
394 for c in e.childNodes:
395 if is_blank(c): continue
396 if c.nodeType != Element.ELEMENT_NODE:
397 log.panic("xml: evil notification child %d" % c.nodeType)
398 if c.nodeName == "batch":
400 status = attr(c, "status")
401 buildtime = attr(c, "buildtime", "0")
402 if not status.startswith("OK") and not status.startswith("SKIP") and not status.startswith("UNSUPP") and not status.startswith("FAIL"):
403 log.panic("xml notification: bad status: %s" % status)
404 self.batches[id] = status
405 self.batches_buildtime[id] = buildtime
407 log.panic("xml: evil notification child (%s)" % c.nodeName)
409 def apply_to(self, q):
411 if r.kind == "group":
413 if self.batches.has_key(b.b_id):
414 b.builders_status[self.builder] = self.batches[b.b_id]
415 b.builders_status_time[self.builder] = time.time()
416 b.builders_status_buildtime[self.builder] = "0" #self.batches_buildtime[b.b_id]
418 def build_request(e):
419 if e.nodeType != Element.ELEMENT_NODE:
420 log.panic("xml: evil request element")
421 if e.nodeName == "group":
423 elif e.nodeName == "notification":
424 return Notification(e)
425 elif e.nodeName == "command":
429 log.panic("xml: evil request [%s]" % e.nodeName)
431 def parse_request(f):
433 return build_request(d.documentElement)
435 def parse_requests(f):
438 for r in d.documentElement.childNodes:
439 if is_blank(r): continue
440 res.append(build_request(r))