annotate simpypi/factory.py @ 19:bf70fc5a115f

make a real python program
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 28 Feb 2012 16:02:53 -0800
parents 77357c5c33c2
children de28a619880e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
1 #!/usr/bin/env python
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
2
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
3 """
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
4 factories for simpypi
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
5 """
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6
19
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
7 import optparse
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
8 import os
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
9 import shutil
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
10 import sys
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
11 import tempfile
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
12
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 from paste.httpexceptions import HTTPExceptionHandler
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 from paste.urlparser import StaticURLParser
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 from pkg_resources import resource_filename
19
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
16 from wsgi import SimPyPI
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
17 from wsgiref import simple_server
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 class PassthroughFileserver(object):
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 """serve files if they exist"""
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21
16
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
22 def __init__(self, app, directory):
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 self.app = app
16
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
24 self.directory = directory
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
25 self.fileserver = StaticURLParser(directory)
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 def __call__(self, environ, start_response):
16
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
28 path = self.path(environ['PATH_INFO'].strip('/'))
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
29 if path and os.path.exists(os.path.join(self.directory, path)):
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
30 return self.fileserver(environ, start_response)
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
31 return self.app(environ, start_response)
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
32
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
33 class NamespacedFileserver(PassthroughFileserver):
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
34
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
35 def __init__(self, app, directory, namespace):
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
36 PassthroughFileserver.__init__(self, app, directory)
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
37 self.namespace = namespace
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
38
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
39 def __call__(self, environ, start_response):
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
40 path = environ['PATH_INFO']
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
41 if path == self.namespace:
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
42 environ['PATH_INFO'] = '/'
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
43 return self.fileserver(environ, start_response)
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
44 elif path.startswith(self.namespace + '/'):
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
45 environ['PATH_INFO'] = path[len(self.namespace):]
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
46 return self.fileserver(environ, start_response)
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 return self.app(environ, start_response)
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48
1
24b8d06eae53 stub out a simple view
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
49 def factory(**app_conf):
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 """create a webob view and wrap it in middleware"""
4
7caedb575794 serve multiple directories
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
51 directory = app_conf['directory']
19
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
52 app = SimPyPI(**app_conf)
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
53 return NamespacedFileserver(app, directory, '/index')
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
54
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
55 def main(args=sys.argv[:]):
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56
19
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
57 # parse command line options
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
58 usage = '%prog [options]'
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
59 parser = optparse.OptionParser(usage=usage)
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
60 parser.add_option('-p', '--port', dest='port',
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
61 type='int', default=8080,
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
62 help="port to run the server on")
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
63 parser.add_option('-d', '--directory', dest='directory',
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
64 help='directory to serve')
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
65 options, args = parser.parse_args(args)
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
66
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
67 # create a temporary directory, if none specified
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
68 tmpdir = None
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
69 if not options.directory:
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
70 tmpdir = tempfile.mkdtemp()
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
71 options.directory = tmpdir
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
72
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
73 # serve
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
74 print "http://localhost:%d/" % options.port
2
b03602153de2 removing more cruft
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
75 try:
19
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
76 app = factory(directory=options.directory)
12
1cdb25cef7ee print helpful message when running test app
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
77 server = simple_server.make_server(host='0.0.0.0', port=port, app=app)
3
f5360bb59e41 this now serves
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
78 server.serve_forever()
2
b03602153de2 removing more cruft
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
79 finally:
19
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
80 if tmpdir:
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
81 shutil.rmtree(tmpdir)
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
82
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
83 if __name__ == '__main__':
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
84 main()