annotate simpypi/wsgi.py @ 40:bb69b82ddd17

misspelling
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 01 Mar 2012 10:16:12 -0800
parents ee29001674af
children c934505fa098
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 """
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2 request dispatcher:
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 data persisting across requests should go here
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 """
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 import os
38
ee29001674af begin to make a temporary copy (though itd be better to work in memory, ideally)
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
7 import pkginfo
ee29001674af begin to make a temporary copy (though itd be better to work in memory, ideally)
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
8 import shutil
ee29001674af begin to make a temporary copy (though itd be better to work in memory, ideally)
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
9 import tempfile
2
b03602153de2 removing more cruft
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
10 from webob import Request, Response, exc
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11
2
b03602153de2 removing more cruft
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
12 here = os.path.dirname(os.path.abspath(__file__))
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13
18
90777e79ea13 fix up factory (i hope)
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
14 class SimPyPI(object):
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15
12
1cdb25cef7ee print helpful message when running test app
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
16 def __init__(self, directory, index=None):
2
b03602153de2 removing more cruft
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
17
b03602153de2 removing more cruft
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
18 self.directory = directory
b03602153de2 removing more cruft
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
19 assert os.path.exists(directory)
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 # request handlers
10
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
22 self.handlers = dict([(method, getattr(self, method))
11
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
23 for method in ('GET', 'POST')])
34
ab52f46a3682 remove cruft
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
24 # TODO: HEAD, OPTIONS, maybe more
10
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
25
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
26 # cache index HTML
12
1cdb25cef7ee print helpful message when running test app
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
27 self.index = index or os.path.join(here, 'templates', 'index.html')
10
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
28 assert os.path.exists(self.index)
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
29 self.index = file(self.index).read()
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 def __call__(self, environ, start_response):
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 # get a request object
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 request = Request(environ)
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 # match the request to a handler
10
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
37 handler = self.handlers.get(request.method)
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
38 if handler:
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
39 res = handler(request)
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 else:
10
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
41 res = exc.HTTPNotFound()
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 return res(environ, start_response)
10
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
44
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
45 def GET(self, request):
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
46 return Response(body=self.index, content_type='text/html')
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
47
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
48 def POST(self, request):
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
49 """handle posting a package"""
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
50
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
51 # get the package
38
ee29001674af begin to make a temporary copy (though itd be better to work in memory, ideally)
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
52 try:
ee29001674af begin to make a temporary copy (though itd be better to work in memory, ideally)
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
53 package = request.POST['package']
ee29001674af begin to make a temporary copy (though itd be better to work in memory, ideally)
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
54 except KeyError:
ee29001674af begin to make a temporary copy (though itd be better to work in memory, ideally)
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
55 # sanity check: does the field exist?
ee29001674af begin to make a temporary copy (though itd be better to work in memory, ideally)
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
56 return exc.HTTPBadRequest()
ee29001674af begin to make a temporary copy (though itd be better to work in memory, ideally)
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
57
ee29001674af begin to make a temporary copy (though itd be better to work in memory, ideally)
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
58 # sanity check: is it a file? (TODO)
40
bb69b82ddd17 misspelling
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
59 if not hasattr(package, 'file') or not hasattr(package, 'filename'):
38
ee29001674af begin to make a temporary copy (though itd be better to work in memory, ideally)
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
60 return exc.HTTPBadRequest()
ee29001674af begin to make a temporary copy (though itd be better to work in memory, ideally)
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
61
ee29001674af begin to make a temporary copy (though itd be better to work in memory, ideally)
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
62 # successful response
ee29001674af begin to make a temporary copy (though itd be better to work in memory, ideally)
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
63 response = exc.HTTPSeeOther(add_slash=True)
10
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
64
38
ee29001674af begin to make a temporary copy (though itd be better to work in memory, ideally)
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
65 # make a temporary copy for pkginfo
ee29001674af begin to make a temporary copy (though itd be better to work in memory, ideally)
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
66 # (or PaInt?)
ee29001674af begin to make a temporary copy (though itd be better to work in memory, ideally)
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
67 tmpdir = tempfile.mkdtemp()
ee29001674af begin to make a temporary copy (though itd be better to work in memory, ideally)
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
68 try:
ee29001674af begin to make a temporary copy (though itd be better to work in memory, ideally)
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
69 path = os.path.join(tmpdir, filename)
ee29001674af begin to make a temporary copy (though itd be better to work in memory, ideally)
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
70 f = file(path, 'w')
ee29001674af begin to make a temporary copy (though itd be better to work in memory, ideally)
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
71 import pdb; pdb.set_trace()
ee29001674af begin to make a temporary copy (though itd be better to work in memory, ideally)
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
72 f.close()
36
a02d08627d9c stub a test to upload a package
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
73 # put the package in the right place
38
ee29001674af begin to make a temporary copy (though itd be better to work in memory, ideally)
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
74 finally:
ee29001674af begin to make a temporary copy (though itd be better to work in memory, ideally)
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
75 # cleanup
ee29001674af begin to make a temporary copy (though itd be better to work in memory, ideally)
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
76 shutil.rmtree(tmpdir)
36
a02d08627d9c stub a test to upload a package
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
77
35
315b7b49eaf4 add redirect
Jeff Hammel <jhammel@mozilla.com>
parents: 34
diff changeset
78 # redirect to the main page
38
ee29001674af begin to make a temporary copy (though itd be better to work in memory, ideally)
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
79 return response