← Back to team overview

dulwich-users team mailing list archive

[PATCH 4/4] Move dul-web's main functionality to web.

 

From: Dave Borowitz <dborowitz@xxxxxxxxxx>

Change-Id: I54a5c821e4cb92e6e71540a9a8bca4ccda89a706
---
 NEWS           |    2 +-
 bin/dul-web    |   35 +++--------------------------------
 dulwich/web.py |   33 ++++++++++++++++++++++++++++++++-
 3 files changed, 36 insertions(+), 34 deletions(-)

diff --git a/NEWS b/NEWS
index d016e42..722e10b 100644
--- a/NEWS
+++ b/NEWS
@@ -29,7 +29,7 @@
 
   * Move reference WSGI handler to web.py. (Dave Borowitz)
 
-  * Make dul-daemon a trivial wrapper around server functionality.
+  * Make dul-daemon/dul-web trivial wrappers around server functionality.
     (Dave Borowitz)
 
 
diff --git a/bin/dul-web b/bin/dul-web
index c68a7b2..910356a 100755
--- a/bin/dul-web
+++ b/bin/dul-web
@@ -17,36 +17,7 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 # MA  02110-1301, USA.
 
-import os
-import sys
-from dulwich.log_utils import default_logging_config
-from dulwich.repo import Repo
-from dulwich.server import DictBackend
-from dulwich.web import (
-    logger,
-    HTTPGitApplication,
-    HTTPGitRequestHandler,
-    )
-from wsgiref.simple_server import (
-    make_server,
-    )
+from dulwich.web import main
 
-
-# TODO: allow serving on other addresses/ports via command-line flag
-LISTEN_ADDR=''
-PORT = 8000
-
-
-if __name__ == "__main__":
-    if len(sys.argv) > 1:
-        gitdir = sys.argv[1]
-    else:
-        gitdir = os.getcwd()
-
-    default_logging_config()
-    backend = DictBackend({"/": Repo(gitdir)})
-    app = HTTPGitApplication(backend)
-    server = make_server(LISTEN_ADDR, PORT, app,
-                         handler_class=HTTPGitRequestHandler)
-    logger.info('Listening for HTTP connections on %s:%d', LISTEN_ADDR, PORT)
-    server.serve_forever()
+if __name__ == '__main__':
+    main()
diff --git a/dulwich/web.py b/dulwich/web.py
index d1a60d4..f689d18 100644
--- a/dulwich/web.py
+++ b/dulwich/web.py
@@ -21,6 +21,7 @@
 from cStringIO import StringIO
 import os
 import re
+import sys
 import time
 
 try:
@@ -31,7 +32,11 @@ from dulwich import log_utils
 from dulwich.protocol import (
     ReceivableProtocol,
     )
+from dulwich.repo import (
+    Repo,
+    )
 from dulwich.server import (
+    DictBackend,
     DEFAULT_HANDLERS,
     )
 
@@ -351,6 +356,7 @@ class HTTPGitApplication(object):
 try:
     from wsgiref.simple_server import (
         WSGIRequestHandler,
+        make_server,
         )
 
     class HTTPGitRequestHandler(WSGIRequestHandler):
@@ -365,7 +371,32 @@ try:
 
         def log_error(self, *args):
             logger.error(*args)
+
+
+    def main(argv=sys.argv):
+        """Entry point for starting an HTTP git server."""
+        if len(argv) > 1:
+            gitdir = argv[1]
+        else:
+            gitdir = os.getcwd()
+
+        # TODO: allow serving on other addresses/ports via command-line flag
+        listen_addr=''
+        port = 8000
+
+        log_utils.default_logging_config()
+        backend = DictBackend({'/': Repo(gitdir)})
+        app = HTTPGitApplication(backend)
+        server = make_server(listen_addr, port, app,
+                             handler_class=HTTPGitRequestHandler)
+        logger.info('Listening for HTTP connections on %s:%d', listen_addr,
+                    port)
+        server.serve_forever()
+
 except ImportError:
     # No wsgiref found; don't provide the reference functionality, but leave the
     # rest of the WSGI-based implementation.
-    pass
+    def main(argv=sys.argv):
+        """Stub entry point for failing to start a server without wsgiref."""
+        sys.stderr.write('Sorry, the wsgiref module is required for dul-web.\n')
+        sys.exit(1)
-- 
1.7.0.4




References