contrib/win32/hgwebdir_wsgi.py
author FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
Wed, 10 Feb 2016 22:44:28 +0900
changeset 28044 e4f70e79a65f
parent 22198 77142de48ae4
child 28187 d3da97e58d42
permissions -rw-r--r--
f: add whitespace around operator This is fixing for 'missing whitespace in expression' check-code rule. check-code has overlooked this, because a file isn't recognized as one to be checked (this problem is fixed by subsequent patch).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
#
10578
36715cd6ee3d win32/hgwebdir_wsgi: clarify copyright license
Martin Geisler <mg@lazybytes.net>
parents: 10572
diff changeset
     4
# This software may be used and distributed according to the terms of the
36715cd6ee3d win32/hgwebdir_wsgi: clarify copyright license
Martin Geisler <mg@lazybytes.net>
parents: 10572
diff changeset
     5
# GNU General Public License version 2 or any later version.
36715cd6ee3d win32/hgwebdir_wsgi: clarify copyright license
Martin Geisler <mg@lazybytes.net>
parents: 10572
diff changeset
     6
#
10572
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
     7
# Requirements:
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
     8
# - Python 2.6
10586
efd3b71fc293 win32/hgwebdir_wsgi: clarify documentation and clean up script a bit
Sune Foldager <cryo@cyanite.org>
parents: 10578
diff changeset
     9
# - PyWin32 build 214 or newer
efd3b71fc293 win32/hgwebdir_wsgi: clarify documentation and clean up script a bit
Sune Foldager <cryo@cyanite.org>
parents: 10578
diff changeset
    10
# - Mercurial installed from source (python setup.py install)
10572
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    11
# - IIS 7
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
# 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
    14
# necessary for win32traceutil to work correctly.
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    15
#
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    16
#
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    17
# Installation and use:
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    18
#
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    19
# - 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
    20
#   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
    21
#
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    22
# - 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
    23
#   shim is identical for all scripts, so you can just copy and rename one
10586
efd3b71fc293 win32/hgwebdir_wsgi: clarify documentation and clean up script a bit
Sune Foldager <cryo@cyanite.org>
parents: 10578
diff changeset
    24
#   from an earlier run, if you wish.
10572
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    25
#
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    26
# - Setup an IIS application where your hgwebdir is to be served from.
10586
efd3b71fc293 win32/hgwebdir_wsgi: clarify documentation and clean up script a bit
Sune Foldager <cryo@cyanite.org>
parents: 10578
diff changeset
    27
#   On 64-bit systems, make sure it's assigned a 32-bit app pool.
10572
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
# - In the application, setup a wildcard script handler mapping of type
17424
e7cfe3587ea4 fix trivial spelling errors
Mads Kiilerich <mads@kiilerich.com>
parents: 14974
diff changeset
    30
#   IsapiModule with the shim dll as its executable. This file MUST reside
10572
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    31
#   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
    32
#
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    33
# - 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
    34
#   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
    35
#
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    36
# - 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
    37
#
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    38
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    39
# Configuration file location
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    40
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
    41
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    42
# Global settings for IIS path translation
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    43
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
    44
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
    45
                 # virtual path of the IIS application).
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    46
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    47
import sys
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    48
10586
efd3b71fc293 win32/hgwebdir_wsgi: clarify documentation and clean up script a bit
Sune Foldager <cryo@cyanite.org>
parents: 10578
diff changeset
    49
# Adjust python path if this is not a system-wide install
efd3b71fc293 win32/hgwebdir_wsgi: clarify documentation and clean up script a bit
Sune Foldager <cryo@cyanite.org>
parents: 10578
diff changeset
    50
#sys.path.insert(0, r'c:\path\to\python\lib')
efd3b71fc293 win32/hgwebdir_wsgi: clarify documentation and clean up script a bit
Sune Foldager <cryo@cyanite.org>
parents: 10578
diff changeset
    51
efd3b71fc293 win32/hgwebdir_wsgi: clarify documentation and clean up script a bit
Sune Foldager <cryo@cyanite.org>
parents: 10578
diff changeset
    52
# Enable tracing. Run 'python -m win32traceutil' to debug
14974
a2aa75118837 win32/hgwebdir_wsgi: use getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents: 10586
diff changeset
    53
if getattr(sys, 'isapidllhandle', None) is not None:
10586
efd3b71fc293 win32/hgwebdir_wsgi: clarify documentation and clean up script a bit
Sune Foldager <cryo@cyanite.org>
parents: 10578
diff changeset
    54
    import win32traceutil
22198
77142de48ae4 cleanup: make sure we always access members of imported modules
Mads Kiilerich <madski@unity3d.com>
parents: 17424
diff changeset
    55
    win32traceutil.SetupForPrint # silence unused import warning
10586
efd3b71fc293 win32/hgwebdir_wsgi: clarify documentation and clean up script a bit
Sune Foldager <cryo@cyanite.org>
parents: 10578
diff changeset
    56
efd3b71fc293 win32/hgwebdir_wsgi: clarify documentation and clean up script a bit
Sune Foldager <cryo@cyanite.org>
parents: 10578
diff changeset
    57
# To serve pages in local charset instead of UTF-8, remove the two lines below
10572
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    58
import os
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    59
os.environ['HGENCODING'] = 'UTF-8'
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    60
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
import isapi_wsgi
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    63
from mercurial import demandimport; demandimport.enable()
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    64
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
    65
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    66
# 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
    67
# 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
    68
class WsgiHandler(isapi_wsgi.IsapiWsgiHandler):
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    69
    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
    70
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    71
isapi_wsgi.IsapiWsgiHandler = WsgiHandler
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    72
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    73
# Only create the hgwebdir instance once
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    74
application = hgwebdir(hgweb_config)
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    75
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    76
def handler(environ, start_response):
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    77
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    78
    # Translate IIS's weird URLs
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    79
    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
    80
    paths = url[1:].split('/')[path_strip:]
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    81
    script_name = '/' + '/'.join(paths[:path_prefix])
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    82
    path_info = '/'.join(paths[path_prefix:])
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    83
    if path_info:
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    84
        path_info = '/' + path_info
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    85
    environ['SCRIPT_NAME'] = script_name
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    86
    environ['PATH_INFO'] = path_info
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    87
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    88
    return application(environ, start_response)
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    89
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    90
def __ExtensionFactory__():
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    91
    return isapi_wsgi.ISAPISimpleHandler(handler)
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    92
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    93
if __name__=='__main__':
22198
77142de48ae4 cleanup: make sure we always access members of imported modules
Mads Kiilerich <madski@unity3d.com>
parents: 17424
diff changeset
    94
    from isapi.install import ISAPIParameters, HandleCommandLine
10572
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    95
    params = ISAPIParameters()
704af22f4907 add wsgi script for Microsoft IIS with isapi-wsgi
Sune Foldager <cryo@cyanite.org>
parents:
diff changeset
    96
    HandleCommandLine(params)