# HG changeset patch # User Jeff Hammel # Date 1330473773 28800 # Node ID bf70fc5a115fc79ee4385e5541c45d003b59ae97 # Parent 90777e79ea136d310d6f6c62ab2f01ebcef684c4 make a real python program diff -r 90777e79ea13 -r bf70fc5a115f simpypi/factory.py --- a/simpypi/factory.py Tue Feb 28 15:47:05 2012 -0800 +++ b/simpypi/factory.py Tue Feb 28 16:02:53 2012 -0800 @@ -1,9 +1,20 @@ -import os +#!/usr/bin/env python + +""" +factories for simpypi +""" -from dispatcher import Dispatcher +import optparse +import os +import shutil +import sys +import tempfile + from paste.httpexceptions import HTTPExceptionHandler from paste.urlparser import StaticURLParser from pkg_resources import resource_filename +from wsgi import SimPyPI +from wsgiref import simple_server class PassthroughFileserver(object): """serve files if they exist""" @@ -38,19 +49,36 @@ def factory(**app_conf): """create a webob view and wrap it in middleware""" directory = app_conf['directory'] - app = Dispatcher(**app_conf) - return PassthroughFileserver(app, directory, '/index') + app = SimPyPI(**app_conf) + return NamespacedFileserver(app, directory, '/index') + +def main(args=sys.argv[:]): -if __name__ == '__main__': - import shutil - import tempfile - from wsgiref import simple_server - port = 8080 - print "http://localhost:%d/" % port - tempdir = tempfile.mkdtemp() + # parse command line options + usage = '%prog [options]' + parser = optparse.OptionParser(usage=usage) + parser.add_option('-p', '--port', dest='port', + type='int', default=8080, + help="port to run the server on") + parser.add_option('-d', '--directory', dest='directory', + help='directory to serve') + options, args = parser.parse_args(args) + + # create a temporary directory, if none specified + tmpdir = None + if not options.directory: + tmpdir = tempfile.mkdtemp() + options.directory = tmpdir + + # serve + print "http://localhost:%d/" % options.port try: - app = factory(directory=tempdir) + app = factory(directory=options.directory) server = simple_server.make_server(host='0.0.0.0', port=port, app=app) server.serve_forever() finally: - shutil.rmtree(tempdir) + if tmpdir: + shutil.rmtree(tmpdir) + +if __name__ == '__main__': + main()