Publishing Repositories Using CherryPy

Introduction

For a really really simple way of publishing your Mercurial repositories on the Web without the need for Apache or IIS.

Some quotes from the CherryPy Web site:

CherryPy can run WSGI compliant applications like hgweb and hgwebdir.

Pre-requisties

Python 2.4 or newer (http://www.python.org/download/)

CherryPy 3.1 or newer (http://www.cherrypy.org/)

Read PublishingRepositories and HgWebDirStepByStep first to get an understanding of hgweb, hgwebdir, the hgweb.config file and Mercurial Lib folder setup.

Using HgWebDir

Here is my sample code that utilizes the hgwebdir module. Edit this code to suit your needs:

   1 # cphgwebdir.py
   2 
   3 # Adjust host and port to suit your Web presence:
   4 sUrlHost='0.0.0.0'
   5 iUrlPort=8080
   6 
   7 # Adjust encoding to suit or comment out:
   8 import os
   9 os.environ['HGENCODING']='UTF-8'
  10 
  11 import sys
  12 # Adjust path to your Mercurial Lib folder:
  13 sys.path.append(r'C:\Program Files\Mercurial\Lib')
  14 from mercurial.hgweb.hgwebdir_mod import hgwebdir
  15 
  16 import cherrypy
  17 cherrypy.config.update({
  18     # Default is development environment, uncomment below when in production
  19     #'environment':'production',
  20     'server.socket_host':sUrlHost,
  21     'server.socket_port':iUrlPort,
  22     'log.error_file':'error.log',
  23     'log.screen':True
  24 })
  25 # Use same hgweb.config file as for hgwebdir.cgi
  26 cherrypy.tree.graft(hgwebdir('hgweb.config'),script_name='/')
  27 cherrypy.engine.start()
  28 cherrypy.engine.block()

sUrlHost can be IP number or domain name (usually, depending on how your network is set up).

Save this code into the same folder as hgweb.config. Unless you have added a path, then save to the root of that path.

Suggested filename is cphgwebdir.py

From a command line just execute the file, like this from its folder:

/path/to/python cphgwebdir.py

That's it.

Using HgWeb

Use this if you don't want the fancy browser front-end.

Here is my sample code that utilizes the hgweb module. Edit this code to suit your needs:

   1 # cphgweb.py
   2 
   3 # Adjust host and port to suit your Web presence:
   4 sUrlHost='0.0.0.0'
   5 iUrlPort=8080
   6 
   7 # Change this to represent your repository/ies:
   8 lRepos=[
   9     #('<virtual path>','<absolute path>'),
  10     ('/project1',r'C:\Program Files\DemoRepos\project1'),
  11     ('/project2',r'C:\Program Files\DemoRepos\project2'),
  12     ('/project3',r'C:\Program Files\DemoRepos\project3')
  13 ]
  14 
  15 # Adjust encoding to suit or comment out:
  16 import os
  17 os.environ['HGENCODING']='UTF-8'
  18 
  19 import sys
  20 # Adjust path to your Mercurial Lib folder:
  21 sys.path.append(r'C:\Program Files\Mercurial\Lib')
  22 from mercurial.hgweb.hgweb_mod import hgweb
  23 
  24 import cherrypy
  25 cherrypy.config.update({
  26     # Default is development environment, uncomment below when in production
  27     #'environment':'production',
  28     'engine.autoreload.on':True,
  29     'server.socket_host':sUrlHost,
  30     'server.socket_port':iUrlPort,
  31     'log.error_file':'error.log',
  32     'log.screen':True
  33 })
  34 for (sName,sPath) in lRepos:
  35     cherrypy.tree.graft(hgweb(sPath),script_name=sName)
  36 cherrypy.engine.start()
  37 cherrypy.engine.block()

sUrlHost can be IP number or domain name (usually, depending on how your network is set up).

Change tuples in lRepos list to indicate the repositories that you want published, first element of each tuple must have a leading forward slash.

Suggested filename is cphgweb.py

From a command line just execute the file, like this from its folder:

/path/to/python cphgweb.py

You can make changes to the lRepos list while it is running, saving changes will initiate a restart.

Example command:

hg clone "http://192.168.0.10:8080/project2"


CategoryHowTo