annotate simpypi/factory.py @ 23:e72d9655d753

start stubbing a directory server
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 28 Feb 2012 16:59:20 -0800
parents de28a619880e
children 13ed82d10144
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
23
e72d9655d753 start stubbing a directory server
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
19 class DirectoryServer(StaticURLParser):
e72d9655d753 start stubbing a directory server
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
20 def __init__(self, directory):
e72d9655d753 start stubbing a directory server
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
21 StaticUrlParser.__init__(self, directory)
e72d9655d753 start stubbing a directory server
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
22
e72d9655d753 start stubbing a directory server
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
23 def __call__(self, environ, start_response):
e72d9655d753 start stubbing a directory server
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
24 import pdb; pdb.set_trace()
e72d9655d753 start stubbing a directory server
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
25
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 class PassthroughFileserver(object):
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 """serve files if they exist"""
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28
16
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
29 def __init__(self, app, directory):
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 self.app = app
16
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
31 self.directory = directory
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
32 self.fileserver = StaticURLParser(directory)
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 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
35 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
36 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
37 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
38 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
39
23
e72d9655d753 start stubbing a directory server
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
40 class NamespacedFileserver(DirectoryServer):
16
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
41
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
42 def __init__(self, app, directory, namespace):
23
e72d9655d753 start stubbing a directory server
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
43 DirectoryServer.__init__(self, directory)
e72d9655d753 start stubbing a directory server
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
44 self.app = app
16
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
45 self.namespace = namespace
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
46
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
47 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
48 path = environ['PATH_INFO']
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
49 if path == self.namespace:
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
50 environ['PATH_INFO'] = '/'
d3efc504c0b1 more towards the type of fileserver we actually care about
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
51 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
52 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
53 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
54 return self.fileserver(environ, start_response)
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55 return self.app(environ, start_response)
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56
23
e72d9655d753 start stubbing a directory server
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
57
1
24b8d06eae53 stub out a simple view
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
58 def factory(**app_conf):
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59 """create a webob view and wrap it in middleware"""
4
7caedb575794 serve multiple directories
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
60 directory = app_conf['directory']
19
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
61 app = SimPyPI(**app_conf)
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
62 return NamespacedFileserver(app, directory, '/index')
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
63
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
64 def main(args=sys.argv[:]):
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
65
19
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
66 # parse command line options
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
67 usage = '%prog [options]'
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
68 parser = optparse.OptionParser(usage=usage)
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
69 parser.add_option('-p', '--port', dest='port',
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
70 type='int', default=8080,
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
71 help="port to run the server on")
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
72 parser.add_option('-d', '--directory', dest='directory',
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
73 help='directory to serve')
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
74 options, args = parser.parse_args(args)
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
75
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
76 # create a temporary directory, if none specified
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
77 tmpdir = None
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
78 if not options.directory:
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
79 tmpdir = tempfile.mkdtemp()
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
80 options.directory = tmpdir
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
81
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
82 # serve
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
83 print "http://localhost:%d/" % options.port
2
b03602153de2 removing more cruft
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
84 try:
19
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
85 app = factory(directory=options.directory)
20
de28a619880e dereference options
Jeff Hammel <jhammel@mozilla.com>
parents: 19
diff changeset
86 server = simple_server.make_server(host='0.0.0.0', port=options.port, app=app)
3
f5360bb59e41 this now serves
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
87 server.serve_forever()
2
b03602153de2 removing more cruft
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
88 finally:
19
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
89 if tmpdir:
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
90 shutil.rmtree(tmpdir)
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
91
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
92 if __name__ == '__main__':
bf70fc5a115f make a real python program
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
93 main()