annotate simpypi/factory.py @ 27:fb03f34a982f

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