Mercurial > hg > simpypi
annotate simpypi/wsgi.py @ 38:ee29001674af
begin to make a temporary copy (though itd be better to work in memory, ideally)
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Thu, 01 Mar 2012 10:02:31 -0800 |
parents | 1bdece293671 |
children | bb69b82ddd17 |
rev | line source |
---|---|
0 | 1 """ |
2 request dispatcher: | |
3 data persisting across requests should go here | |
4 """ | |
5 | |
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 | 10 from webob import Request, Response, exc |
0 | 11 |
2 | 12 here = os.path.dirname(os.path.abspath(__file__)) |
0 | 13 |
18 | 14 class SimPyPI(object): |
0 | 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 | 17 |
18 self.directory = directory | |
19 assert os.path.exists(directory) | |
0 | 20 |
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 | 23 for method in ('GET', 'POST')]) |
34 | 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 | 30 |
31 def __call__(self, environ, start_response): | |
32 | |
33 # get a request object | |
34 request = Request(environ) | |
35 | |
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 | 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 | 42 |
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) |
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
|
59 if not hasattr(package, 'file') or not hastattr(package, '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
|
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 | 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 |