annotate contrib/win32/hgwebdir_wsgi.py @ 10572:704af22f4907

add wsgi script for Microsoft IIS with isapi-wsgi
author Sune Foldager <cryo@cyanite.org>
date Wed, 03 Mar 2010 14:42:43 +0100
parents
children 36715cd6ee3d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10572
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
1 # An example WSGI script for IIS/isapi-wsgi to export multiple hgweb repos
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
2 # Copyright 2010 Sune Foldager <cryo@cyanite.org>
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
3 #
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
4 # Requirements:
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
5 # - Python 2.6
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
6 # - IIS 7
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
7 # - PyWin32 build 214 or newer
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
8 #
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
9 # Earlier versions will in general work as well, but the PyWin32 version is
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
10 # necessary for win32traceutil to work correctly.
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
11 #
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
12 #
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
13 # Installation and use:
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
14 #
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
15 # - Download the isapi-wsgi source and run python setup.py install:
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
16 # http://code.google.com/p/isapi-wsgi/
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
17 #
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
18 # - Run this script (i.e. python hgwebdir_wsgi.py) to get a shim dll. The
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
19 # shim is identical for all scripts, so you can just copy and rename one
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
20 # from an earlier run instead.
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
21 #
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
22 # - Setup an IIS application where your hgwebdir is to be served from.
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
23 # Make sure it's assigned a 32-bit app pool.
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
24 #
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
25 # - In the application, setup a wildcard script handler mapping of type
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
26 # IpsapiModule, with the shim dll as its executable. This file MUST reside
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
27 # in the same directory as the shim. Remove all other handlers, if you wish.
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
28 #
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
29 # - Make sure the ISAPI and CGI restrictions (configured globally on the
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
30 # web server) includes the shim dll, to allow it to run.
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
31 #
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
32 # - Adjust the configuration variables below to match your needs.
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
33 #
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
34
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
35 # Configuration file location
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
36 hgweb_config = r'c:\src\iis\hg\hgweb.config'
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
37
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
38 # Global settings for IIS path translation
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
39 path_strip = 0 # Strip this many path elements off (when using url rewrite)
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
40 path_prefix = 1 # This many path elements are prefixes (depends on the
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
41 # virtual path of the IIS application).
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
42
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
43 import sys
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
44
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
45 # To stop serving pages in UTF-8, remove the two lines below
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
46 import os
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
47 os.environ['HGENCODING'] = 'UTF-8'
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
48
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
49 # Adjust python path if this is not a system-wide install
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
50 #sys.path.insert(0, "/path/to/python/lib")
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
51
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
52
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
53 # Enable tracing. Run 'python -m win32traceutil' to debug
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
54 if hasattr(sys, 'isapidllhandle'):
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
55 import win32traceutil
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
56
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
57 import isapi_wsgi
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
58
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
59 from mercurial import demandimport; demandimport.enable()
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
60 from mercurial.hgweb.hgwebdir_mod import hgwebdir
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
61
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
62 # Example tweak: Replace isapi_wsgi's handler to provide better error message
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
63 # Other stuff could also be done here, like logging errors etc.
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
64 class WsgiHandler(isapi_wsgi.IsapiWsgiHandler):
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
65 error_status = '500 Internal Server Error' # less silly error message
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
66
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
67 isapi_wsgi.IsapiWsgiHandler = WsgiHandler
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
68
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
69 # Only create the hgwebdir instance once
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
70 application = hgwebdir(hgweb_config)
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
71
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
72 def handler(environ, start_response):
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
73
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
74 # Translate IIS's weird URLs
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
75 url = environ['SCRIPT_NAME'] + environ['PATH_INFO']
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
76 paths = url[1:].split('/')[path_strip:]
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
77 script_name = '/' + '/'.join(paths[:path_prefix])
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
78 path_info = '/'.join(paths[path_prefix:])
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
79 if path_info:
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
80 path_info = '/' + path_info
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
81 environ['SCRIPT_NAME'] = script_name
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
82 environ['PATH_INFO'] = path_info
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
83
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
84 return application(environ, start_response)
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
85
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
86 def __ExtensionFactory__():
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
87 return isapi_wsgi.ISAPISimpleHandler(handler)
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
88
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
89 if __name__=='__main__':
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
90 from isapi.install import *
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
91 params = ISAPIParameters()
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
92 HandleCommandLine(params)