[z3-checkins] r33440 - in z3/deliverance/branches/packaged: . deliverance

ianb at codespeak.net ianb at codespeak.net
Thu Oct 19 03:14:29 CEST 2006


Author: ianb
Date: Thu Oct 19 03:14:21 2006
New Revision: 33440

Added:
   z3/deliverance/branches/packaged/deliverance/proxyapp.py   (contents, props changed)
   z3/deliverance/branches/packaged/deliverance/proxycommand.py   (contents, props changed)
Modified:
   z3/deliverance/branches/packaged/setup.py
Log:
Added a command-line script for setting up a proxy

Added: z3/deliverance/branches/packaged/deliverance/proxyapp.py
==============================================================================
--- (empty file)
+++ z3/deliverance/branches/packaged/deliverance/proxyapp.py	Thu Oct 19 03:14:21 2006
@@ -0,0 +1,34 @@
+"""
+WSGI proxy application that applies a deliverance theme while
+passing the request to another HTTP server
+"""
+
+from paste.proxy import TransparentProxy
+from deliverance import wsgifilter
+
+class ProxyDeliveranceApp(object):
+
+    def __init__(self, theme_uri, rule_uri, proxy,
+                 transparent=False):
+        self.theme_uri = theme_uri,
+        self.rule_uri = rule_uri,
+        self.proxy = proxy
+        self.transparent = transparent
+        self.subapp = self.make_app()
+        self.deliverance_app = wsgifilter.DeliveranceMiddleware(
+            self.subapp, theme_uri, rule_uri)
+
+    def make_app(self):
+        if self.transparent:
+            force_host = self.proxy
+        else:
+            force_host = None
+        return TransparentProxy(force_host=force_host)
+
+    def __call__(self, environ, start_response):
+        if not self.transparent:
+            # @@: Set forwarded header
+            environ['HTTP_HOST'] = self.proxy
+        return self.deliverance_app(
+            environ, start_response)
+    

Added: z3/deliverance/branches/packaged/deliverance/proxycommand.py
==============================================================================
--- (empty file)
+++ z3/deliverance/branches/packaged/deliverance/proxycommand.py	Thu Oct 19 03:14:21 2006
@@ -0,0 +1,83 @@
+import optparse
+import pkg_resources
+import sys
+from deliverance import proxyapp
+
+my_package = pkg_resources.get_distribution('Deliverance')
+
+pkg_resources.require('Paste')
+
+from paste import httpserver
+
+help = """\
+"""
+
+parser = optparse.OptionParser(
+    version=str(my_package),
+    usage="%%prog [OPTIONS]\n\n%s" % help)
+parser.add_option('-s', '--serve',
+                  help="The interface to serve on (default 0.0.0.0:80)",
+                  dest="serve",
+                  default="0.0.0.0:80")
+parser.add_option('-p', '--proxy',
+                  help="The host and port to proxy to (default localhost:8080)",
+                  dest="proxy",
+                  default='localhost:8080')
+parser.add_option('--theme',
+                  help="The URI of the theme to use",
+                  dest="theme")
+parser.add_option('--rule',
+                  help="The URI of the ruleset to use",
+                  dest="rule")
+parser.add_option('--transparent',
+                  help="Do not rewrite the Host header when passing the request on",
+                  action='store_true',
+                  dest='transparent')
+parser.add_option('--debug',
+                  help="Show tracebacks when an error occurs (use twice for fancy/dangerous traceback)",
+                  action="count",
+                  dest="debug")
+
+def strip(prefix, string):
+    if string.startswith(prefix):
+        return string[len(prefix):]
+    else:
+        return string
+
+def main(args=None):
+    if args is None:
+        args = sys.argv[1:]
+    options, args = parser.parse_args(args)
+    serve = strip('http://', options.serve)
+    if ':' not in serve:
+        serve += ':80'
+    proxy = strip('http://', options.proxy)
+    if ':' not in proxy:
+        proxy += ':80'
+    if not options.rule or not options.theme:
+        if not options.rule:
+            op = '--rule'
+        else:
+            op = '--theme'
+        print 'You must provide the %s option' % op
+        sys.exit(2)
+    app = proxyapp.ProxyDeliveranceApp(
+        theme_uri=options.theme,
+        rule_uri=options.rule,
+        proxy=proxy,
+        transparent=options.transparent)
+    if options.debug:
+        if options.debug > 1:
+            from paste.evalexception.middleware import EvalException
+            app = EvalException(app)
+        else:
+            from paste.exceptions.errormiddleware import ErrorMiddleware
+            app = ErrorMiddleware(app, debug=True)
+    print 'Serving on http://%s' % serve
+    print 'Proxying from http://%s' % proxy
+    try:
+        httpserver.serve(app, host=serve)
+    except KeyboardInterrupt:
+        print 'Exiting.'
+        sys.exit()
+    

Modified: z3/deliverance/branches/packaged/setup.py
==============================================================================
--- z3/deliverance/branches/packaged/setup.py	(original)
+++ z3/deliverance/branches/packaged/setup.py	Thu Oct 19 03:14:21 2006
@@ -19,12 +19,15 @@
       zip_safe=False,
       install_requires=[
         'lxml',
-        'paste'
+        'paste==dev,>=0.9.9a'
       ],
       include_package_data=True,
       entry_points="""
       [paste.filter_app_factory]
       main = deliverance.wsgifilter:make_filter
+
+      [console_scripts]
+      deliverance-proxy = deliverance.proxycommand:main
       """,
       )
 


More information about the z3-checkins mailing list