Fixed rating menu not showing localized strings
[zeroinstall.git] / tests / server.py
blobde29fefb8bbc96d835196e48f0cf79d1f74f6488
1 #!/usr/bin/env python
2 import os, sys, urlparse
3 import BaseHTTPServer
4 import traceback
6 next_step = None
8 class Give404:
9 def __init__(self, path):
10 self.path = path
12 def __str__(self):
13 return self.path
15 def __repr__(self):
16 return "404 on " + self.path
18 class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
19 def do_GET(self):
20 parsed = urlparse.urlparse(self.path)
22 leaf = os.path.basename(parsed.path)
24 acceptable = dict([(str(x), x) for x in next_step])
26 resp = acceptable.get(parsed.path, None) or \
27 acceptable.get(leaf, None) or \
28 acceptable.get('*', None)
30 if not resp:
31 self.send_error(404, "Expected %s; got %s" % (next_step, parsed.path))
33 if os.path.exists(leaf) and not isinstance(resp, Give404):
34 self.send_response(200)
35 self.end_headers()
36 self.wfile.write(file(leaf).read())
37 self.wfile.close()
38 else:
39 self.send_error(404, "Missing: %s" % leaf)
41 def handle_requests(*script):
42 server_address = ('localhost', 8000)
43 httpd = BaseHTTPServer.HTTPServer(server_address, MyHandler)
44 child = os.fork()
45 if child:
46 return child
47 # We are the child
48 try:
49 sys.stderr = sys.stdout
50 print "Waiting for request"
51 global next_step
52 for next_step in script:
53 if type(next_step) != tuple: next_step = (next_step,)
54 for x in next_step:
55 httpd.handle_request()
56 print "Done"
57 os._exit(0)
58 except:
59 traceback.print_exc()
60 os._exit(1)