main: Add an argparse-based command line interface
authorW. Trevor King <wking@tremily.us>
Thu, 20 Feb 2014 19:00:16 +0000 (11:00 -0800)
committerW. Trevor King <wking@tremily.us>
Thu, 20 Feb 2014 19:23:43 +0000 (11:23 -0800)
And a package-cache.py wrapper script to call it.

package-cache.py [new file with mode: 0755]
package_cache/main.py [new file with mode: 0644]

diff --git a/package-cache.py b/package-cache.py
new file mode 100755 (executable)
index 0000000..4378609
--- /dev/null
@@ -0,0 +1,6 @@
+#!/usr/bin/env python
+
+from package_cache.main import run
+
+
+run()
diff --git a/package_cache/main.py b/package_cache/main.py
new file mode 100644 (file)
index 0000000..c6c814e
--- /dev/null
@@ -0,0 +1,40 @@
+# Copyright
+
+import argparse as _argparse
+import logging as _logging
+import wsgiref.simple_server as _wsgiref_simple_server
+
+from . import __version__
+from . import server as _server
+
+
+LOG = _logging.getLogger(__name__)
+
+
+def run(*args, **kwargs):
+    """Run the package-cache server using Python's WSGI reference server
+    """
+    parser = _argparse.ArgumentParser(description=__doc__)
+    parser.add_argument(
+        '--version', action='version',
+        version='%(prog)s {}'.format(__version__))
+    parser.add_argument(
+        '--host', metavar='HOSTNAME', default='localhost',
+        help='Host to listen as')
+    parser.add_argument(
+        '--port', metavar='INT', default=4000, type=int,
+        help='Port to listen to')
+    parser.add_argument(
+        '--source', metavar='URL', action='append',
+        help='URL for an upstream mirror')
+    parser.add_argument(
+        '--cache', metavar='PATH', default='/tmp/package-cache',
+        help='Path to the package cache directory')
+
+    args = parser.parse_args()
+
+    server = _server.Server(sources=args.source or [], cache=args.cache)
+    wsgi = _wsgiref_simple_server.make_server(
+        host=args.host, port=args.port, app=server)
+    LOG.info('serving WSGI on {}:{}'.format(args.host, args.port))
+    wsgi.serve_forever()