Added initial set of extensions, including customized hgwebdir.cgi.
authorScott Ehlert <ds@alliedmods.net>
Tue Oct 20 12:51:05 2009 -0500 (2009-10-20)
changeset 0f59a7c3bf23e
child 1 8a55da2d6618
Added initial set of extensions, including customized hgwebdir.cgi.
.hgignore
buglink.py
hgwebdir.cgi
templatepath.py
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/.hgignore	Tue Oct 20 12:51:05 2009 -0500
     1.3 @@ -0,0 +1,1 @@
     1.4 +\.pyc$
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/buglink.py	Tue Oct 20 12:51:05 2009 -0500
     2.3 @@ -0,0 +1,77 @@
     2.4 +# vim: set ts=4 sw=4 tw=99 et ft=python:
     2.5 +
     2.6 +'''adds buglink template filter for turning bug numbers into links
     2.7 +
     2.8 +This is a modified version of an extension in use at hg.mozilla.org
     2.9 +The original can be found here:
    2.10 +http://hg.mozilla.org/users/bsmedberg_mozilla.com/hgpoller/file/tip/buglink.py
    2.11 +
    2.12 +To enable this extension:
    2.13 +
    2.14 +    [extensions]
    2.15 +    buglink = /path/to/buglink.py
    2.16 +'''
    2.17 +
    2.18 +import re
    2.19 +from mercurial import templatefilters
    2.20 +
    2.21 +bugzilla = r'<a href="https://bugs.alliedmods.net/show_bug.cgi?id=\2">\1</a>'
    2.22 +bug_re = re.compile(r'''# bug followed by any sequence of numbers, or
    2.23 +                        # a standalone sequence of numbers
    2.24 +                     (
    2.25 +                        (?:
    2.26 +                          bug |
    2.27 +                          b= |
    2.28 +                          # a sequence of 5+ numbers preceded by whitespace
    2.29 +                          (?=\b\#?\d{5,}\b) |
    2.30 +                          # numbers at the very beginning
    2.31 +                          ^(?=\d\b)
    2.32 +                        )
    2.33 +                        (?:\s*\#?)(\d+)
    2.34 +                     )''', re.I | re.X)
    2.35 +
    2.36 +flyspray = r'<a href="http://fs.alliedmods.net/index.php?do=details&task_id=\2">\1</a>'
    2.37 +amb_re = re.compile(r'''# amb followed by any sequence of numbers
    2.38 +                     (
    2.39 +                        (?:
    2.40 +                          amb
    2.41 +                        )
    2.42 +                        (?:\s*\#?)(\d+)
    2.43 +                     )''', re.I | re.X)
    2.44 +
    2.45 +def buglink(x):
    2.46 +    log = bug_re.sub(bugzilla, x)
    2.47 +    log = amb_re.sub(flyspray, log)
    2.48 +    return log
    2.49 +
    2.50 +templatefilters.filters["buglink"] = buglink
    2.51 +
    2.52 +if __name__ == '__main__':
    2.53 +    import unittest
    2.54 +    
    2.55 +    _tests = (
    2.56 +        ('bug 1', ('', 'bug 1', '1', '')),
    2.57 +        ('bug 123456', ('', 'bug 123456', '123456', '')),
    2.58 +        ('testb=1234x', ('test', 'b=1234', '1234', 'x')),
    2.59 +        ('ab4665521e2f', None),
    2.60 +        ('Aug 2008', None),
    2.61 +        ('b=#12345', ('', 'b=#12345', '12345', '')),
    2.62 +        ('GECKO_191a2_20080815_RELBRANCH', None),
    2.63 +        ('12345 is a bug', ('', '12345', '12345', ' is a bug')),
    2.64 +        (' 123456 whitespace!', (' ', '123456', '123456', ' whitespace!')),
    2.65 +        ('changeset 938479a4c9df', None),
    2.66 +        ('938479a4c9df', None),
    2.67 +        )
    2.68 +
    2.69 +    class TestBugRe(unittest.TestCase):
    2.70 +        def testreplacements(self):
    2.71 +            for str, result in _tests:
    2.72 +                if result is None:
    2.73 +                    resulttext = str
    2.74 +                else:
    2.75 +                    pretext, text, bugid, posttext = result
    2.76 +                    resulttext = '%s<a href="https://bugs.alliedmods.net/show_bug.cgi?id=%s">%s</a>%s' % (pretext, bugid, text, posttext)
    2.77 +
    2.78 +                self.assertEqual(resulttext, buglink(str))
    2.79 +
    2.80 +    unittest.main()
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/hgwebdir.cgi	Tue Oct 20 12:51:05 2009 -0500
     3.3 @@ -0,0 +1,83 @@
     3.4 +#!/usr/bin/env python
     3.5 +#
     3.6 +# An example CGI script to export multiple hgweb repos, edit as necessary
     3.7 +
     3.8 +# adjust python path if not a system-wide install:
     3.9 +#import sys
    3.10 +#sys.path.insert(0, "/path/to/python/lib")
    3.11 +
    3.12 +# enable importing on demand to reduce startup time
    3.13 +from mercurial import demandimport; demandimport.enable()
    3.14 +
    3.15 +# Uncomment to send python tracebacks to the browser if an error occurs:
    3.16 +import cgitb
    3.17 +cgitb.enable()
    3.18 +
    3.19 +# If you'd like to serve pages with UTF-8 instead of your default
    3.20 +# locale charset, you can do so by uncommenting the following lines.
    3.21 +# Note that this will cause your .hgrc files to be interpreted in
    3.22 +# UTF-8 and all your repo files to be displayed using UTF-8.
    3.23 +#
    3.24 +import os
    3.25 +#os.environ["HGENCODING"] = "UTF-8"
    3.26 +
    3.27 +from mercurial import extensions
    3.28 +from mercurial.hgweb.hgwebdir_mod import hgwebdir
    3.29 +import mercurial.hgweb.wsgicgi as wsgicgi
    3.30 +
    3.31 +# The config file looks like this.  You can have paths to individual
    3.32 +# repos, collections of repos in a directory tree, or both.
    3.33 +#
    3.34 +# [paths]
    3.35 +# virtual/path1 = /real/path1
    3.36 +# virtual/path2 = /real/path2
    3.37 +# virtual/root = /real/root/*
    3.38 +# / = /real/root2/*
    3.39 +# virtual/root2 = /real/root2/**
    3.40 +#
    3.41 +# [collections]
    3.42 +# /prefix/to/strip/off = /root/of/tree/full/of/repos
    3.43 +#
    3.44 +# paths example: 
    3.45 +#
    3.46 +# * First two lines mount one repository into one virtual path, like
    3.47 +# '/real/path1' into 'virtual/path1'.
    3.48 +#
    3.49 +# * The third entry mounts every mercurial repository found in '/real/root'
    3.50 +# in 'virtual/root'. This format is preferred over the [collections] one,
    3.51 +# since using absolute paths as configuration keys is not supported on every
    3.52 +# platform (especially on Windows).
    3.53 +#
    3.54 +# * The fourth entry is a special case mounting all repositories in
    3.55 +# /'real/root2' in the root of the virtual directory.
    3.56 +#
    3.57 +# * The fifth entry recursively finds all repositories under the real root,
    3.58 +# and mounts them using their relative path (to given real root) under the
    3.59 +# virtual root.
    3.60 +#
    3.61 +# collections example: say directory tree /foo contains repos /foo/bar,
    3.62 +# /foo/quux/baz.  Give this config section:
    3.63 +#   [collections]
    3.64 +#   /foo = /foo
    3.65 +# Then repos will list as bar and quux/baz.
    3.66 +#
    3.67 +# Alternatively you can pass a list of ('virtual/path', '/real/path') tuples
    3.68 +# or use a dictionary with entries like 'virtual/path': '/real/path'
    3.69 +
    3.70 +hgroot = '/repo/hg'
    3.71 +path = os.environ['SCRIPT_NAME']
    3.72 +repo_list = []
    3.73 +
    3.74 +if path is None or path is '':
    3.75 +    path = hgroot
    3.76 +else:
    3.77 +    path = path.replace('/', hgroot + '/', 1)
    3.78 +
    3.79 +for item in os.listdir(path):
    3.80 +    repo_path = os.path.join(path, item)
    3.81 +    if os.path.isdir(os.path.join(repo_path, '.hg')):
    3.82 +        repo_list.append(( item, repo_path ))
    3.83 +
    3.84 +application = hgwebdir(repo_list)
    3.85 +extensions.loadall(application.ui)
    3.86 +wsgicgi.launch(application)
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/templatepath.py	Tue Oct 20 12:51:05 2009 -0500
     4.3 @@ -0,0 +1,47 @@
     4.4 +# vim: set ts=4 sw=4 tw=99 et ft=python:
     4.5 +
     4.6 +'''allows overriding of templates directory for hgwebdir
     4.7 +
     4.8 +The code in hgweb/hgweb_mod.py allows you to override the templates directory path using the
     4.9 +web.templates option. However, this is not allowed by hgweb/hgwebdir_mod.py. This extension fixes
    4.10 +this problem by using the same web.templates option.
    4.11 +
    4.12 +However, extensions are not normally loaded when hgwebdir is in use. In order to fix this, you must
    4.13 +modify the hgwebdir CGI script like so:
    4.14 +
    4.15 +    from mercurial import extensions
    4.16 +    from mercurial.hgweb.hgwebdir_mod import hgwebdir
    4.17 +    import mercurial.hgweb.wsgicgi as wsgicgi
    4.18 +
    4.19 +    application = hgwebdir('hgweb.config')
    4.20 +    extensions.loadall(application.ui)
    4.21 +    wsgicgi.launch(application)
    4.22 +
    4.23 +To enable this extension:
    4.24 +
    4.25 +    [extensions]
    4.26 +    templatepath = /path/to/templatepath.py
    4.27 +'''
    4.28 +
    4.29 +import os
    4.30 +from mercurial import extensions, templater
    4.31 +
    4.32 +_ui = None
    4.33 +
    4.34 +def uisetup(ui):
    4.35 +    global _ui
    4.36 +    _ui = ui
    4.37 +    extensions.wrapfunction(templater, 'templatepath', hook_templatepath)
    4.38 +
    4.39 +def hook_templatepath(orig, name=None):
    4.40 +    templates = _ui.config('web', 'templates')
    4.41 +
    4.42 +    if not templates:
    4.43 +        return orig(name)
    4.44 +
    4.45 +    if name:
    4.46 +        path = os.path.join(templates, name)
    4.47 +        if os.path.exists(path):
    4.48 +            return os.path.normpath(path)
    4.49 +
    4.50 +    return [templates]