Package buildbot :: Package status :: Package web :: Module feeds
[frames] | no frames]

Source Code for Module buildbot.status.web.feeds

  1  # This file is part of Buildbot.  Buildbot is free software: you can 
  2  # redistribute it and/or modify it under the terms of the GNU General Public 
  3  # License as published by the Free Software Foundation, version 2. 
  4  # 
  5  # This program is distributed in the hope that it will be useful, but WITHOUT 
  6  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
  8  # details. 
  9  # 
 10  # You should have received a copy of the GNU General Public License along with 
 11  # this program; if not, write to the Free Software Foundation, Inc., 51 
 12  # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
 13  # 
 14  # Copyright Buildbot Team Members 
 15   
 16  # This module enables ATOM and RSS feeds from webstatus. 
 17  # 
 18  # It is based on "feeder.py" which was part of the Buildbot 
 19  # configuration for the Subversion project. The original file was 
 20  # created by Lieven Gobaerts and later adjusted by API 
 21  # (apinheiro@igalia.coma) and also here 
 22  # http://code.google.com/p/pybots/source/browse/trunk/master/Feeder.py 
 23  # 
 24  # All subsequent changes to feeder.py where made by Chandan-Dutta 
 25  # Chowdhury <chandan-dutta.chowdhury @ hp.com> and Gareth Armstrong 
 26  # <gareth.armstrong @ hp.com>. 
 27  # 
 28  # Those modifications are as follows: 
 29  # 1) the feeds are usable from baseweb.WebStatus 
 30  # 2) feeds are fully validated ATOM 1.0 and RSS 2.0 feeds, verified 
 31  #    with code from http://feedvalidator.org 
 32  # 3) nicer xml output 
 33  # 4) feeds can be filtered as per the /waterfall display with the 
 34  #    builder and category filters 
 35  # 5) cleaned up white space and imports 
 36  # 
 37  # Finally, the code was directly integrated into these two files, 
 38  # buildbot/status/web/feeds.py (you're reading it, ;-)) and 
 39  # buildbot/status/web/baseweb.py. 
 40   
 41  import os 
 42  import re 
 43  import time 
 44  from twisted.web import resource 
 45  from buildbot.status.builder import FAILURE 
 46   
47 -class XmlResource(resource.Resource):
48 contentType = "text/xml; charset=UTF-8" 49 docType = '' 50
51 - def getChild(self, name, request):
52 return self
53
54 - def render(self, request):
55 data = self.content(request) 56 request.setHeader("content-type", self.contentType) 57 if request.method == "HEAD": 58 request.setHeader("content-length", len(data)) 59 return '' 60 return data
61 62 _abbr_day = [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] 63 _abbr_mon = ['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 64 'Sep', 'Oct', 'Nov', 'Dec'] 65
66 -def rfc822_time(tstamp):
67 res = time.strftime("%%s, %d %%s %Y %H:%M:%S GMT", 68 tstamp) 69 res = res % (tstamp.tm_wday, tstamp.tm_mon) 70 return res
71
72 -class FeedResource(XmlResource):
73 pageTitle = None 74 link = 'http://dummylink' 75 language = 'en-us' 76 description = 'Dummy rss' 77 status = None 78
79 - def __init__(self, status, categories=None, pageTitle=None):
80 self.status = status 81 self.categories = categories 82 self.pageTitle = pageTitle 83 self.title = self.status.getTitle() 84 self.link = self.status.getBuildbotURL() 85 self.description = 'List of builds' 86 self.pubdate = time.gmtime(int(time.time())) 87 self.user = self.getEnv(['USER', 'USERNAME'], 'buildmaster') 88 self.hostname = self.getEnv(['HOSTNAME', 'COMPUTERNAME'], 89 'buildmaster') 90 self.children = {}
91
92 - def getEnv(self, keys, fallback):
93 for key in keys: 94 if key in os.environ: 95 return os.environ[key] 96 return fallback
97
98 - def getBuilds(self, request):
99 builds = [] 100 # THIS is lifted straight from the WaterfallStatusResource Class in 101 # status/web/waterfall.py 102 # 103 # we start with all Builders available to this Waterfall: this is 104 # limited by the config-file -time categories= argument, and defaults 105 # to all defined Builders. 106 allBuilderNames = self.status.getBuilderNames(categories=self.categories) 107 builders = [self.status.getBuilder(name) for name in allBuilderNames] 108 109 # but if the URL has one or more builder= arguments (or the old show= 110 # argument, which is still accepted for backwards compatibility), we 111 # use that set of builders instead. We still don't show anything 112 # outside the config-file time set limited by categories=. 113 showBuilders = request.args.get("show", []) 114 showBuilders.extend(request.args.get("builder", [])) 115 if showBuilders: 116 builders = [b for b in builders if b.name in showBuilders] 117 118 # now, if the URL has one or category= arguments, use them as a 119 # filter: only show those builders which belong to one of the given 120 # categories. 121 showCategories = request.args.get("category", []) 122 if showCategories: 123 builders = [b for b in builders if b.category in showCategories] 124 125 failures_only = request.args.get("failures_only", "false") 126 127 maxFeeds = 25 128 129 # Copy all failed builds in a new list. 130 # This could clearly be implemented much better if we had 131 # access to a global list of builds. 132 for b in builders: 133 lastbuild = b.getLastFinishedBuild() 134 if lastbuild is None: 135 continue 136 137 lastnr = lastbuild.getNumber() 138 139 totalbuilds = 0 140 i = lastnr 141 while i >= 0: 142 build = b.getBuild(i) 143 i -= 1 144 if not build: 145 continue 146 147 results = build.getResults() 148 149 if failures_only == "false" or results == FAILURE: 150 totalbuilds += 1 151 builds.append(build) 152 153 # stop for this builder when our total nr. of feeds is reached 154 if totalbuilds >= maxFeeds: 155 break 156 157 # Sort build list by date, youngest first. 158 # To keep compatibility with python < 2.4, use this for sorting instead: 159 # We apply Decorate-Sort-Undecorate 160 deco = [(build.getTimes(), build) for build in builds] 161 deco.sort() 162 deco.reverse() 163 builds = [build for (b1, build) in deco] 164 165 if builds: 166 builds = builds[:min(len(builds), maxFeeds)] 167 return builds
168
169 - def content(self, request):
170 builds = self.getBuilds(request) 171 172 build_cxts = [] 173 174 for build in builds: 175 start, finished = build.getTimes() 176 finishedTime = time.gmtime(int(finished)) 177 link = re.sub(r'index.html', "", self.status.getURLForThing(build)) 178 179 # title: trunk r22191 (plus patch) failed on 180 # 'i686-debian-sarge1 shared gcc-3.3.5' 181 ss = build.getSourceStamp() 182 source = "" 183 if ss.branch: 184 source += "Branch %s " % ss.branch 185 if ss.revision: 186 source += "Revision %s " % str(ss.revision) 187 if ss.patch: 188 source += " (plus patch)" 189 if ss.changes: 190 pass 191 if (ss.branch is None and ss.revision is None and ss.patch is None 192 and not ss.changes): 193 source += "Latest revision " 194 got_revision = build.getProperty("got_revision") 195 if got_revision: 196 got_revision = str(got_revision) 197 if len(got_revision) > 40: 198 got_revision = "[revision string too long]" 199 source += "(Got Revision: %s)" % got_revision 200 failflag = (build.getResults() != FAILURE) 201 pageTitle = ('%s %s on "%s"' % 202 (source, ["failed","succeeded"][failflag], 203 build.getBuilder().getName())) 204 205 # Add information about the failing steps. 206 failed_steps = [] 207 log_lines = [] 208 for s in build.getSteps(): 209 if s.getResults()[0] == FAILURE: 210 failed_steps.append(s.getName()) 211 212 # Add the last 30 lines of each log. 213 for log in s.getLogs(): 214 log_lines.append('Last lines of build log "%s":' % 215 log.getName()) 216 log_lines.append([]) 217 try: 218 logdata = log.getText() 219 except IOError: 220 # Probably the log file has been removed 221 logdata ='** log file not available **' 222 unilist = list() 223 for line in logdata.split('\n')[-30:]: 224 unilist.append(unicode(line,'utf-8')) 225 log_lines.extend(unilist) 226 227 bc = {} 228 bc['date'] = rfc822_time(finishedTime) 229 bc['summary_link'] = ('%sbuilders/%s' % 230 (self.link, 231 build.getBuilder().getName())) 232 bc['name'] = build.getBuilder().getName() 233 bc['number'] = build.getNumber() 234 bc['responsible_users'] = build.getResponsibleUsers() 235 bc['failed_steps'] = failed_steps 236 bc['pageTitle'] = pageTitle 237 bc['link'] = link 238 bc['log_lines'] = log_lines 239 240 if finishedTime is not None: 241 bc['rfc822_pubdate'] = rfc822_time(finishedTime) 242 bc['rfc3339_pubdate'] = time.strftime("%Y-%m-%dT%H:%M:%SZ", 243 finishedTime) 244 245 # Every RSS/Atom item must have a globally unique ID 246 guid = ('tag:%s@%s,%s:%s' % 247 (self.user, self.hostname, 248 time.strftime("%Y-%m-%d", finishedTime), 249 time.strftime("%Y%m%d%H%M%S", finishedTime))) 250 bc['guid'] = guid 251 252 build_cxts.append(bc) 253 254 pageTitle = self.pageTitle 255 if not pageTitle: 256 pageTitle = 'Build status of %s' % self.title 257 258 cxt = {} 259 cxt['pageTitle'] = pageTitle 260 cxt['title_url'] = self.link 261 cxt['title'] = self.title 262 cxt['language'] = self.language 263 cxt['description'] = self.description 264 if self.pubdate is not None: 265 cxt['rfc822_pubdate'] = rfc822_time( self.pubdate) 266 cxt['rfc3339_pubdate'] = time.strftime("%Y-%m-%dT%H:%M:%SZ", 267 self.pubdate) 268 269 cxt['builds'] = build_cxts 270 template = request.site.buildbot_service.templates.get_template(self.template_file) 271 return template.render(**cxt).encode('utf-8').strip()
272
273 -class Rss20StatusResource(FeedResource):
274 # contentType = 'application/rss+xml' (browser dependent) 275 template_file = 'feed_rss20.xml' 276
277 - def __init__(self, status, categories=None, pageTitle=None):
278 FeedResource.__init__(self, status, categories, pageTitle)
279
280 -class Atom10StatusResource(FeedResource):
281 # contentType = 'application/atom+xml' (browser dependent) 282 template_file = 'feed_atom10.xml' 283
284 - def __init__(self, status, categories=None, pageTitle=None):
285 FeedResource.__init__(self, status, categories, pageTitle)
286