← Back to team overview

dulwich-users team mailing list archive

[PATCH 1/4] Move reference WSGI handler to web.py.

 

From: Dave Borowitz <dborowitz@xxxxxxxxxx>

This is conditionally defined only if wsgiref is available for python
2.4 compatibility. The failure mode is still the same: if wsgiref is not
installed, users can import the web module but not run dul-web.

Change-Id: I0cbeb7fa878f8567e059409e87b7f4225fda143f
---
 bin/dul-web    |   16 +---------------
 dulwich/web.py |   26 ++++++++++++++++++++++++++
 2 files changed, 27 insertions(+), 15 deletions(-)
 mode change 100644 => 100755 bin/dul-web

diff --git a/bin/dul-web b/bin/dul-web
old mode 100644
new mode 100755
index 15b1a0e..c68a7b2
--- a/bin/dul-web
+++ b/bin/dul-web
@@ -25,27 +25,13 @@ from dulwich.server import DictBackend
 from dulwich.web import (
     logger,
     HTTPGitApplication,
+    HTTPGitRequestHandler,
     )
 from wsgiref.simple_server import (
-    WSGIRequestHandler,
     make_server,
     )
 
 
-class HTTPGitRequestHandler(WSGIRequestHandler):
-    """Handler that uses dulwich's logger for logging exceptions."""
-
-    def log_exception(self, exc_info):
-        logger.exception('Exception happened during processing of request',
-                         exc_info=exc_info)
-
-    def log_message(self, format, *args):
-        logger.info(format, *args)
-
-    def log_error(self, *args):
-        logger.error(*args)
-
-
 # TODO: allow serving on other addresses/ports via command-line flag
 LISTEN_ADDR=''
 PORT = 8000
diff --git a/dulwich/web.py b/dulwich/web.py
index e25ec4b..da11553 100644
--- a/dulwich/web.py
+++ b/dulwich/web.py
@@ -345,3 +345,29 @@ class HTTPGitApplication(object):
         if handler is None:
             return req.not_found('Sorry, that method is not supported')
         return handler(req, self.backend, mat)
+
+
+# The reference server implementation is based on wsgiref, which is not
+# distributed with python 2.4. If wsgiref is not present, users will not be able
+# to use the HTTP server without a little extra work.
+try:
+    from wsgiref.simple_server import (
+        WSGIRequestHandler,
+        )
+
+    class HTTPGitRequestHandler(WSGIRequestHandler):
+        """Handler that uses dulwich's logger for logging exceptions."""
+
+        def log_exception(self, exc_info):
+            logger.exception('Exception happened during processing of request',
+                             exc_info=exc_info)
+
+        def log_message(self, format, *args):
+            logger.info(format, *args)
+
+        def log_error(self, *args):
+            logger.error(*args)
+except ImportError:
+    # No wsgiref found; don't provide the reference functionality, but leave the
+    # rest of the WSGI-based implementation.
+    pass
-- 
1.7.0.4




References