PageRenderTime 68ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/win32/hgwebdir_wsgi.py

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