代码之家  ›  专栏  ›  技术社区  ›  Constantin

如何访问XML版本的CruiseContol。NET Web仪表板报告(通过HTTP)?

  •  1
  • Constantin  · 技术社区  · 17 年前

    我需要确定上次构建的状态(成功/失败),我这样做:

    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
      ...
    

    但这是浪费(加载整个HTML页面)、容易出错(我已经遇到了bytes/unicode错误)和脆弱的。

    1 回复  |  直到 17 年前
        1
  •  1
  •   Constantin    17 年前

    好吧,我有点想通了。CCTray通过轮询跟踪构建状态 XmlServerReport.aspx ,这是(令人惊讶的!)XML。

    所以我目前的解决方案看起来像这样:

    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
        ...
    

    在我的环境中,它比最初的基于HTML的解决方案快大约8倍。