If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!

Screenshots

Shell access

Calling the plugin with hg

Browser access

Calling the plugin from your browser

Source code

""" plugin_h_mercurial.py

Author:  Hans Christian v. Stockhausen <hc at vst.io>
Date:    2010-12-09

This is a plugin for web2py. It consists of the single controller file
plugin_mercurial.py. It wraps Mercurial's hgwebdir wsgi app and allows
one to interact with the mercurial repository of the web2py application
either from a webbrowser or the hg client.

Notes:

- I am new to Mercurial. Not sure whether this is really useful or just
  plain dangerous.
- Not sure to what degree this overlaps with web2py's built in Mercurial
  support, but I didn't get the impression that one can use HTTP pulls...

"""

from mercurial import hgweb

def index():
    """ Controller to wrap hgweb

    You can access this endpoint either from a browser in which case the 
    hgweb interface is displayed or from the mercurial client.

    hg clone http://localhost:8000/myapp/plugin_mercurial/index myapp

    To be able to push to the repository you need to edit/create the file 
    application/<myapp>/.hg/hgrc and add the following entries for example.

    [web]
    allow_push = *
    push_ssl = False

    Clearly this is recommended for a trusted environment only. Also see the
    hgrc documentation at http://www.selenic.com/mercurial/hgrc.5.html#web.

    """

    # HACK - hgweb expects the wsgi version to be reported in a tuple
    wsgi_version = request.wsgi.environ['wsgi.version']
    request.wsgi.environ['wsgi.version'] = (wsgi_version, 0)

    # map this controller's URL to the repository location and instantiate app
    config = {URL(r=request).xml():'applications/'+request.application}
    wsgi_app = hgweb.hgwebdir(config)

    # invoke wsgi app and return results via web2py API
    # http://web2py.com/book/default/chapter/04#WSGI
    items = wsgi_app(request.wsgi.environ, request.wsgi.start_response)
    for item in items:
        response.write(item, escape=False)
    return response.body.getvalue()

hg push

You can also push to the repository. See the docstring for index() above.

multiple repositories

The hgwebdir wsgi application can expose multiple repositories although for a web2py application-specific plugin this is probably not what you want. If you do however want just that, try tweaking the config variable that is passed to the hgwebdir constructor. For example you could pass the name of the repository to access through request.args[0]. URLs are even longer then, so you might want to setup some rules in routes.py.

config = dict(
    'myapp/plugin_mercurial/index/repo1'='path/to/repo1',
    'myapp/plugin_mercurial/index/repo2'='path/to/repo2',
    'myapp/plugin_mercurial/index/repo3'='path/to/repo3'
    )

Related slices

Comments (4)

  • Login to post



  • 0
    hcvst 9 years ago
    Thanks mr.freeze, it works. - really cool app you built here.

  • 0
    hcvst 9 years ago
    hm, when I try to update this slice it renders as HTML (tags) after saving (preview looks good - using Google Chrome 8.0.552.215 on Mac OS X). Other than to fix some spelling mistakes I just wanted to add a section on multiple repositories: The `hgwebdir` wsgi application can expose multiple repositories although for a web2py application-specific plugin this is probably not what you want. If you do however want just that, try tweaking the `config` variable that is passed to the `hgwebdir` constructor. For example you could pass the name of the repo to access through request.args[0]. URLs are even longer then, so you might want to setup some rules in routing.py.

  • 0
    mrfreeze 9 years ago
    Looks like the wmd toolbar has gone rogue. Thanks for the heads up.

  • 0
    mrfreeze 9 years ago
    Okay, it should be fixed now. Sorry about that.

Hosting graciously provided by:
Python Anywhere