Skip to content

Instantly share code, notes, and snippets.

@jonforums
Created March 11, 2011 19:34
Show Gist options
  • Save jonforums/866432 to your computer and use it in GitHub Desktop.
Save jonforums/866432 to your computer and use it in GitHub Desktop.
Evaluating CherryPy 3.2's new tools.auth_digest and Mercurial
#! /usr/bin/env python
# coding: utf-8
import os
import sys
class Options(object):
pass
def main(options):
os.environ['HGENCODING'] = 'UTF-8'
# OTHER HACKS MAY BE NEEDED IF DIDN'T BUILD HG YOURSELF
#sys.path.append(r'C:\Program Files\Mercurial\Lib')
#os.environ["HGRCPATH"] = r'C:\hgweb\config'
import cherrypy
from cherrypy.process.win32 import ConsoleCtrlHandler
from mercurial.hgweb.hgwebdir_mod import hgwebdir
# lean-n-mean
cherrypy.engine.timeout_monitor.unsubscribe()
cherrypy.engine.autoreload.unsubscribe()
if hasattr(cherrypy.engine, 'console_control_handler'):
cherrypy.engine.console_control_handler.subscribe()
cherrypy.config.update({
#'environment':'production',
#'engine.autoreload.on':True,
'server.socket_host':options.server,
'server.socket_port':options.port,
'log.error_file':options.log,
'log.screen':True
})
if options.auth:
cherrypy.log('Configuring digest authentication')
get_ha1 = cherrypy.lib.auth_digest.get_ha1_file_htdigest(options.htdigest)
digest_auth = { 'tools.auth_digest.on':True,
'tools.auth_digest.realm':options.auth,
'tools.auth_digest.get_ha1':get_ha1,
'tools.auth_digest.key':'my_secret_key_a8937ee9yhhjueig03887g'
}
cherrypy.config.update({ '/':digest_auth })
cherrypy.tree.graft(hgwebdir('hgweb.config'), '/')
cherrypy.engine.start()
cherrypy.engine.block()
if __name__ == '__main__':
version = '0.3.0'
usage = '''
usage: python cphgweb.py [OPTIONS]
where OPTIONS is one of:
--auth REALM REALM access auth [default_realm]
--server ADDR address to listen on [0.0.0.0]
--port PORT port to listen on [8080]
--logfile FILE file to log to [cp_error.log]
--htdigest FILE credential store file [htdigest]
-h, --help this help message
--version version summary
'''
if '--version' in sys.argv:
print('cphgweb v%s' % version)
exit(1)
if '-h' in sys.argv or '--help' in sys.argv:
print(usage)
exit(1)
opts = Options()
for o in ['--server', '--port', '--logfile', '--htdigest', '--auth']:
try:
setattr(opts, o.replace('--',''), sys.argv[sys.argv.index(o)+1])
except ValueError:
pass
except IndexError:
pass
opts.auth = getattr(opts, 'auth', 'default_realm') if '--auth' in sys.argv else False
opts.server = getattr(opts, 'server', '0.0.0.0')
opts.port = int(getattr(opts, 'port', 8080))
opts.log = getattr(opts, 'logfile', 'cp_error.log')
opts.htdigest = getattr(opts, 'htdigest', 'htdigest')
main(opts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment