#!/usr/bin/env python# -*- coding: iso8859-1 -*-## Copyright (C) 2003, 2004, 2005 Edgewall Software# Copyright (C) 2003, 2004, 2005 Jonas Borgström <jonas@edgewall.com># All rights reserved.## This software is licensed as described in the file COPYING, which# you should have received as part of this distribution. The terms# are also available at http://trac.edgewall.com/license.html.## This software consists of voluntary contributions made by many# individuals. For the exact contribution history, see the revision# history and logs, available at http://projects.edgewall.com/trac/.## Author: Jonas Borgström <jonas@edgewall.com>fromtrac.web.standaloneimportBasicAuth,DigestAuth,TracHTTPServerimportgetoptimportlocaleimportsysimportosdefusage():print'usage: %s [options] <projenv> [projenv] ...'%sys.argv[0]print'usage: %s [options] --env-parent-dir=[path]'%sys.argv[0]print'\nOptions:\n'print'-a --auth [project],[htdigest_file],[realm]'print'--basic-auth [project],[htpasswd_file],[realm]'print'-p --port [port]\t\tPort number to use (default: 80)'print'-b --hostname [hostname]\tIP to bind to (default: \'\')'print'-d --daemonize\tRun tracd in the background as a daemon'print'-e --env-parent-dir <path>\tParent directory of the project environments'printsys.exit(1)defadd_auth(auths,vals,cls):info=vals.split(',',3)iflen(info)!=3:usage()p,h,r=infoifauths.has_key(p):print>>sys.stderr,'Ignoring duplicate authentication option for ' \
'project: %s'%pelse:auths[p]=cls(h,r)defmain():locale.setlocale(locale.LC_ALL,'')port=80hostname=''auths={}daemonize=0env_parent_dir=Nonetry:opts,args=getopt.getopt(sys.argv[1:],"a:p:b:de:",["auth=","port=","hostname=","daemonize","env-parent-dir=","basic-auth="])exceptgetopt.GetoptError,e:printeusage()foro,ainopts:ifoin("-a","--auth"):add_auth(auths,a,DigestAuth)ifo=='--basic-auth':add_auth(auths,a,BasicAuth)ifoin("-p","--port"):port=int(a)elifoin("-b","--hostname"):hostname=aifoin("-d","--daemonize"):daemonize=1ifoin("-e","--env-parent-dir"):env_parent_dir=aifnotenv_parent_dirandnotargs:usage()server_address=(hostname,port)httpd=TracHTTPServer(server_address,env_parent_dir,args,auths)try:ifdaemonize:pid=os.fork()ifpid==0:os.chdir(os.sep)os.setpgrp()# Redirect standard file descriptorssys.stdin=open('/dev/null','r')sys.stdout=open('/dev/null','w')sys.stderr=open('/dev/null','w')else:print'tracd daemon running with pid: %d'%pidsys.exit(0)httpd.serve_forever()exceptOSError:sys.exit(1)if__name__=='__main__':main()