Mercurial > hg > simpypi
changeset 19:bf70fc5a115f
make a real python program
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 28 Feb 2012 16:02:53 -0800 |
parents | 90777e79ea13 |
children | de28a619880e |
files | simpypi/factory.py |
diffstat | 1 files changed, 41 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- 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()