Skip to content

Commit

Permalink
Extract jinja-env setup and fix CustomHTMLMail test (fixes buildbot#691)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-sonestedt committed Jan 22, 2010
1 parent e9bb78e commit 3f13db1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 45 deletions.
48 changes: 46 additions & 2 deletions buildbot/status/web/base.py
@@ -1,13 +1,13 @@

import urlparse, urllib, time, re
import os, cgi, sys
import jinja2
from zope.interface import Interface
from twisted.web import resource, static
from twisted.python import log
from buildbot.status import builder
from buildbot.status.builder import SUCCESS, WARNINGS, FAILURE, SKIPPED, EXCEPTION
from buildbot import version, util
import jinja2
import os, cgi
from buildbot.process.properties import Properties

class ITopBox(Interface):
Expand Down Expand Up @@ -371,6 +371,50 @@ def map_branches(branches):

# jinja utilities

def createJinjaEnv(revlink=None, changecommentlink=None):
''' Create a jinja environment changecommentlink is used to
render HTML in the WebStatus and for mail changes
@type changecommentlink: tuple (2 strings) or C{None}
@param changecommentlink: a regular expression and replacement string that is applied
to all change comments. The first element represents what to search
for and the second yields an url (the <a href=""></a> is added outside this)
I.e. for Trac: (r'#(\d+)', r'http://buildbot.net/trac/ticket/\1')
@type revlink: string or C{None}
@param revlink: a replacement string that is applied to all revisions and
will, if set, create a link to the result of the replacement.
Use %s to insert the revision id in the url.
I.e. for Buildbot on github: ('http://github.com/djmitche/buildbot/tree/%s')
(The revision id will be URL encoded before inserted in the replacement string)
'''

if hasattr(sys, "frozen"):
assert False, 'Frozen config not supported with jinja (yet)'

default_loader = jinja2.PackageLoader('buildbot.status.web', 'templates')
root = os.path.join(os.getcwd(), 'templates')
loader = jinja2.ChoiceLoader([jinja2.FileSystemLoader(root),
default_loader])
env = jinja2.Environment(loader=loader,
extensions=['jinja2.ext.i18n'],
trim_blocks=True,
undefined=AlmostStrictUndefined)

env.filters['urlencode'] = urllib.quote
env.filters['email'] = emailfilter
env.filters['user'] = userfilter
env.filters['shortrev'] = shortrevfilter(revlink, env)
env.filters['revlink'] = revlinkfilter(revlink, env)

if changecommentlink:
regex, replace = changecommentlink
env.filters['changecomment'] = addlinkfilter(regex, replace)
else:
env.filters['changecomment'] = jinja2.escape

return env

def emailfilter(value):
''' Escape & obfuscate e-mail addresses
Expand Down
32 changes: 2 additions & 30 deletions buildbot/status/web/baseweb.py
Expand Up @@ -12,7 +12,7 @@

from buildbot.status.web.base import HtmlResource, \
build_get_class, ICurrentBox, BuildLineMixin, map_branches, \
StaticFile, path_to_root
StaticFile, path_to_root, createJinjaEnv
from buildbot.status.web.feeds import Rss20StatusResource, \
Atom10StatusResource
from buildbot.status.web.waterfall import WaterfallStatusResource
Expand All @@ -27,10 +27,6 @@
from buildbot.status.web.auth import IAuth, AuthFailResource
from buildbot.status.web.root import RootPage

import buildbot.status.web.base as webbase
import jinja2


# this class contains the status services (WebStatus and the older Waterfall)
# which can be put in c['status']. It also contains some of the resources
# that are attached to the WebStatus at various well-known URLs, which the
Expand Down Expand Up @@ -480,31 +476,7 @@ def __init__(self, http_port=None, distrib_port=None, allowForce=False,
self.site.buildbot_service = self

# Set up the jinja templating engine.
if hasattr(sys, "frozen"):
assert False, 'Frozen config not supported with jinja (yet)'
default_loader = jinja2.PackageLoader('buildbot.status.web', 'templates')
root = os.path.join(os.getcwd(), 'templates')
loader = jinja2.ChoiceLoader([jinja2.FileSystemLoader(root),
default_loader])
self.templates = jinja2.Environment(loader=loader,
extensions=['jinja2.ext.i18n'],
trim_blocks=True,
undefined=webbase.AlmostStrictUndefined)

self.templates.filters['urlencode'] = urllib.quote
self.templates.filters['email'] = webbase.emailfilter
self.templates.filters['user'] = webbase.userfilter
self.templates.filters['shortrev'] = \
webbase.shortrevfilter(revlink, self.templates)
self.templates.filters['revlink'] = \
webbase.revlinkfilter(revlink, self.templates)

if changecommentlink:
regex, replace = changecommentlink
self.templates.filters['changecomment'] = \
webbase.addlinkfilter(regex, replace)
else:
self.templates.filters['changecomment'] = jinja2.escape
self.templates = createJinjaEnv(revlink, changecommentlink)

# keep track of cached connections so we can break them when we shut
# down. See ticket #102 for more details.
Expand Down
21 changes: 8 additions & 13 deletions buildbot/test/test_status.py
Expand Up @@ -11,6 +11,7 @@
from buildbot.sourcestamp import SourceStamp
from buildbot.process.base import BuildRequest, Build
from buildbot.status import builder, base, words
from buildbot.status.web.base import createJinjaEnv
from buildbot.changes.changes import Change
from buildbot.process.builder import Builder
from time import sleep
Expand Down Expand Up @@ -153,23 +154,16 @@ def customTextMailMessage(attrs):
return ("\n".join(text), 'plain')

def customHTMLMailMessage(attrs):
loader = jinja2.PackageLoader('buildbot.status.web', 'templates')
templates = jinja2.Environment(loader=loader,
extensions=['jinja2.ext.i18n'],
trim_blocks=True)
template = templates.get_template("change.html")

templates = createJinjaEnv()
template = templates.get_template("change_macros.html")

logLines = 3
text = list()
text.append("<h3>STATUS <a href='%s'>%s</a>:</h3>" % (attrs['buildURL'],
attrs['result'].title()))
# TODO: include the same filters in an email rendering as are available in
# web renderings (bug #691)

#text.append("<h4>Recent Changes:</h4>")
#for c in attrs['changes']:
# text.append(template.module.change(**c.html_dict()))
text.append("<h4>Recent Changes:</h4>")
for c in attrs['changes']:
text.append(template.module.change(**c.html_dict()))

name, url, lines, status = attrs['logs'][-1]
text.append("<h4>Last %d lines of '%s':</h4>" % (logLines, name))
Expand Down Expand Up @@ -359,6 +353,7 @@ def testCustomTextMessage(self):
#self.fail(t)
self.failUnlessIn("comment1", t)
self.failUnlessIn("comment2", t)
self.failUnlessIn("author2", t)
self.failUnlessIn("Test 4 failed", t)
self.failUnlessIn("number was: 1", t)

Expand Down Expand Up @@ -393,7 +388,7 @@ def testCustomHTMLMessage(self):
#
#self.fail(t)
self.failUnlessIn("<h4>Last 3 lines of 'step.test':</h4>", t)
#self.failUnlessIn("Changed by: <b>author2</b>", t) # see comments in customHTMLMailMessage, above
self.failUnlessIn("Changed by</td><td><b>author2</b></td>", t)
self.failUnlessIn("Test 3 failed", t)
self.failUnlessIn("number was: 1", t)

Expand Down

0 comments on commit 3f13db1

Please sign in to comment.