views:

141

answers:

1

I need to determine state of last build (success/failure) and I do it like this:

report_url = 'http://.../ViewLatestBuildReport.aspx'
success_marker = '<td class="header-title" colspan="2">BUILD SUCCESSFUL</td>'
page = urllib.urlopen(report_url)
if all(success_marker not in line for line in page):
  # build is not good, do something
  ...

But this is wasteful (loads entire HTML page), error-prone (I already ran into a bytes/unicode bug) and fragile.

A: 

OK, I sort of figured it out. CCTray tracks build status by polling XmlServerReport.aspx, which is (surprise!) XML.

So my current solution looks like this:

import sys, urllib, xml.sax, xml.sax.handler

report_url = 'http://.../CCNET/XmlServerReport.aspx'

class FoundBuildStatus(Exception):
  def __init__(self, status):
    self.build_status = status

class Handler(xml.sax.handler.ContentHandler):
  def startElement(self, name, attrs):
    if name == 'Project' and attrs.get('name') == '...':
      status = attrs.get('lastBuildStatus')
      if status:
        raise FoundBuildStatus(status)

page = urllib.urlopen(report_url)
try:
  xml.sax.parse(page, Handler())
except FoundBuildStatus, ex:
  if ex.build_status == 'Failure':
    # build is not good, do something
    ...

In my environment it is about 8 times faster than initial HTML-based solution.

Constantin