| 1 |
#!C:\programme\python\2.3\python.exe |
|---|
| 2 |
# -*- coding: iso8859-1 -*- |
|---|
| 3 |
# |
|---|
| 4 |
# Author: Florent Xicluna <laxyf@yahoo.fr> |
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
# The service module defines a single class (TracWindowsService) that contains |
|---|
| 8 |
# the functionality for running Trac as a Windows Service. |
|---|
| 9 |
# |
|---|
| 10 |
# To use this class, users must do the following: |
|---|
| 11 |
# 1. Download and install the win32all package |
|---|
| 12 |
# (http://starship.python.net/crew/mhammond/win32/) |
|---|
| 13 |
# 2. Edit the constants section with the proper information. |
|---|
| 14 |
# 3. Open a command prompt and navigate to the directory where this file |
|---|
| 15 |
# is located. Use one of the following commands to |
|---|
| 16 |
# install/start/stop/remove the service: |
|---|
| 17 |
# > tracservice.py install |
|---|
| 18 |
# > tracservice.py start |
|---|
| 19 |
# > tracservice.py stop |
|---|
| 20 |
# > tracservice.py remove |
|---|
| 21 |
# Additionally, typing "tracservice.py" will present the user with all of the |
|---|
| 22 |
# available options. |
|---|
| 23 |
# |
|---|
| 24 |
# Once installed, the service will be accessible through the Services |
|---|
| 25 |
# management console just like any other Windows Service. All service |
|---|
| 26 |
# startup exceptions encountered by the TracWindowsService class will be |
|---|
| 27 |
# viewable in the Windows event viewer (this is useful for debugging |
|---|
| 28 |
# service startup errors); all application specific output or exceptions that |
|---|
| 29 |
# are not captured by the standard Trac logging mechanism should |
|---|
| 30 |
# appear in the stdout/stderr logs. |
|---|
| 31 |
# |
|---|
| 32 |
|
|---|
| 33 |
import locale |
|---|
| 34 |
import sys |
|---|
| 35 |
import os |
|---|
| 36 |
|
|---|
| 37 |
import win32serviceutil |
|---|
| 38 |
import win32service |
|---|
| 39 |
|
|---|
| 40 |
from trac.web.standalone import BasicAuth, DigestAuth, TracHTTPServer |
|---|
| 41 |
|
|---|
| 42 |
# == Editable CONSTANTS SECTION ============================================ |
|---|
| 43 |
|
|---|
| 44 |
PYTHON = r'C:\Python23\python.exe' |
|---|
| 45 |
INSTANCE_HOME = r'c:\path\to\instance\trac' |
|---|
| 46 |
|
|---|
| 47 |
# Trac options (see C:\Python23\Script\tracd) |
|---|
| 48 |
OPTS = [ |
|---|
| 49 |
( '--auth', ('trac,%s\conf\htdigest,TracRealm' % INSTANCE_HOME) ), |
|---|
| 50 |
( '--port', '80' ), |
|---|
| 51 |
] |
|---|
| 52 |
|
|---|
| 53 |
# == End of CONSTANTS SECTION ============================================== |
|---|
| 54 |
|
|---|
| 55 |
# Other constants |
|---|
| 56 |
PYTHONDIR = os.path.split(PYTHON)[0] |
|---|
| 57 |
PYTHONSERVICE_EXE=r'%s\Lib\site-packages\win32\pythonservice.exe' % PYTHONDIR |
|---|
| 58 |
LOG_DIR = r'%s\log' % INSTANCE_HOME |
|---|
| 59 |
|
|---|
| 60 |
# Trac instance(s) |
|---|
| 61 |
ARGS = [ INSTANCE_HOME, ] |
|---|
| 62 |
|
|---|
| 63 |
def add_auth(auths, vals, cls): |
|---|
| 64 |
info = vals.split(',', 3) |
|---|
| 65 |
p, h, r = info |
|---|
| 66 |
if auths.has_key(p): |
|---|
| 67 |
print >>sys.stderr, 'Ignoring duplicate authentication option for ' \ |
|---|
| 68 |
'project: %s' % p |
|---|
| 69 |
else: |
|---|
| 70 |
auths[p] = cls(h, r) |
|---|
| 71 |
|
|---|
| 72 |
class TracWindowsService(win32serviceutil.ServiceFramework): |
|---|
| 73 |
"""Trac Windows Service helper class. |
|---|
| 74 |
|
|---|
| 75 |
The TracWindowsService class contains all the functionality required |
|---|
| 76 |
for running Trac application as a Windows Service. |
|---|
| 77 |
|
|---|
| 78 |
For information on installing the application, please refer to the |
|---|
| 79 |
documentation at the end of this module or navigate to the directory |
|---|
| 80 |
where this module is located and type "tracservice.py" from the command |
|---|
| 81 |
prompt. |
|---|
| 82 |
""" |
|---|
| 83 |
|
|---|
| 84 |
_svc_name_ = 'Trac_%s' % str(hash(INSTANCE_HOME)) |
|---|
| 85 |
_svc_display_name_ = 'Trac instance at %s' % INSTANCE_HOME |
|---|
| 86 |
_exe_name_ = PYTHONSERVICE_EXE |
|---|
| 87 |
|
|---|
| 88 |
def SvcDoRun(self): |
|---|
| 89 |
""" Called when the Windows Service runs. """ |
|---|
| 90 |
|
|---|
| 91 |
self.ReportServiceStatus(win32service.SERVICE_START_PENDING) |
|---|
| 92 |
self.httpd = self.trac_init() |
|---|
| 93 |
self.ReportServiceStatus(win32service.SERVICE_RUNNING) |
|---|
| 94 |
try: |
|---|
| 95 |
self.httpd.serve_forever() |
|---|
| 96 |
except OSError: |
|---|
| 97 |
sys.exit(1) |
|---|
| 98 |
|
|---|
| 99 |
def SvcStop(self): |
|---|
| 100 |
"""Called when Windows receives a service stop request.""" |
|---|
| 101 |
|
|---|
| 102 |
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) |
|---|
| 103 |
if self.httpd: |
|---|
| 104 |
self.httpd.server_close() |
|---|
| 105 |
self.ReportServiceStatus(win32service.SERVICE_STOPPED) |
|---|
| 106 |
|
|---|
| 107 |
def trac_init(self): |
|---|
| 108 |
""" Checks for the required data and initializes the application. """ |
|---|
| 109 |
|
|---|
| 110 |
locale.setlocale(locale.LC_ALL, '') |
|---|
| 111 |
port = 80 |
|---|
| 112 |
hostname = '' |
|---|
| 113 |
auths = {} |
|---|
| 114 |
daemonize = 0 |
|---|
| 115 |
env_parent_dir = None |
|---|
| 116 |
|
|---|
| 117 |
for o, a in OPTS: |
|---|
| 118 |
if o in ("-a", "--auth"): |
|---|
| 119 |
add_auth(auths, a, DigestAuth) |
|---|
| 120 |
if o == '--basic-auth': |
|---|
| 121 |
add_auth(auths, a, BasicAuth) |
|---|
| 122 |
if o in ("-p", "--port"): |
|---|
| 123 |
port = int(a) |
|---|
| 124 |
elif o in ("-b", "--hostname"): |
|---|
| 125 |
hostname = a |
|---|
| 126 |
if o in ("-e", "--env-parent-dir"): |
|---|
| 127 |
env_parent_dir = a |
|---|
| 128 |
|
|---|
| 129 |
if not env_parent_dir and not ARGS: |
|---|
| 130 |
raise ValueError("""No Trac project specified""") |
|---|
| 131 |
|
|---|
| 132 |
sys.stdout = open(os.path.join(LOG_DIR, 'stdout.log'),'a') |
|---|
| 133 |
sys.stderr = open(os.path.join(LOG_DIR, 'stderr.log'),'a') |
|---|
| 134 |
|
|---|
| 135 |
server_address = (hostname, port) |
|---|
| 136 |
return TracHTTPServer(server_address, env_parent_dir, ARGS, auths) |
|---|
| 137 |
|
|---|
| 138 |
if __name__ == '__main__': |
|---|
| 139 |
# The following are the most common command-line arguments that are used |
|---|
| 140 |
# with this module: |
|---|
| 141 |
# tracservice.py install (Installs the service with manual startup) |
|---|
| 142 |
# tracservice.py --startup auto install (Installs the service with auto startup) |
|---|
| 143 |
# tracservice.py start (Starts the service) |
|---|
| 144 |
# tracservice.py stop (Stops the service) |
|---|
| 145 |
# tracservice.py remove (Removes the service) |
|---|
| 146 |
# |
|---|
| 147 |
# For a full list of arguments, simply type "tracservice.py". |
|---|
| 148 |
win32serviceutil.HandleCommandLine(TracWindowsService) |
|---|