Mercurial > hg > simpypi
comparison 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 |
comparison
equal
deleted
inserted
replaced
26:f8ff95180d04 | 27:fb03f34a982f |
---|---|
8 import os | 8 import os |
9 import shutil | 9 import shutil |
10 import sys | 10 import sys |
11 import tempfile | 11 import tempfile |
12 | 12 |
13 from paste.httpexceptions import HTTPExceptionHandler | 13 from fileserver import DirectoryServer |
14 from paste.urlparser import StaticURLParser | |
15 from pkg_resources import resource_filename | |
16 from wsgi import SimPyPI | 14 from wsgi import SimPyPI |
17 from wsgiref import simple_server | 15 from wsgiref import simple_server |
18 | 16 |
19 class DirectoryServer(StaticURLParser): | |
20 def __init__(self, directory): | |
21 StaticURLParser.__init__(self, directory) | |
22 | |
23 def index(self, directory): | |
24 """ | |
25 generate a directory listing for a given directory | |
26 """ | |
27 parts = ['<html><head><title>Simple Index</title></head><body>'] | |
28 listings = os.listdir(directory) | |
29 listings = [(os.path.isdir(os.path.join(directory, entry)) and entry + '/' or entry, entry) | |
30 for entry in listings] | |
31 for link, entry in listings: | |
32 parts.append('<a href="%s">%s</a><br/>' % (link, entry)) | |
33 parts.append('</body></html>') | |
34 return '\n'.join(parts) | |
35 | |
36 def __call__(self, environ, start_response): | |
37 | |
38 # normalize path | |
39 # from paste.urlparser.StaticURLParser | |
40 path_info = environ.get('PATH_INFO', '') | |
41 if not path_info: | |
42 return self.add_slash(environ, start_response) | |
43 full = self.normpath(os.path.join(self.directory, path_info.strip('/'))) | |
44 if not full.startswith(self.root_directory): | |
45 # Out of bounds | |
46 return self.not_found(environ, start_response) | |
47 | |
48 # return index listing | |
49 if os.path.isdir(full): | |
50 if not path_info.endswith('/'): | |
51 return self.add_slash(environ, start_response) | |
52 index = self.index(full) | |
53 response_headers = [('Content-Type', 'text/html'), | |
54 ('Content-Length', str(len(index)))] | |
55 start_response('200 OK', response_headers) | |
56 return [index] | |
57 | |
58 return StaticURLParser.__call__(self, environ, start_response) | |
59 | 17 |
60 class PassthroughFileserver(object): | 18 class PassthroughFileserver(object): |
61 """serve files if they exist""" | 19 """serve files if they exist""" |
62 | 20 |
63 def __init__(self, app, directory): | 21 def __init__(self, app, directory): |