Don't try to read dpkg database unless its directory is accessible to us.
[zeroinstall/zeroinstall-rsl.git] / tests / server.py
blob5f00409084c8063f8022d5ec0891715d455e83aa
1 #!/usr/bin/env python
2 import os, sys
3 import BaseHTTPServer
4 import traceback
6 next_step = None
8 class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
9 def do_GET(self):
10 leaf = os.path.basename(self.path)
11 if next_step != ('*',) and leaf not in next_step:
12 self.send_error(404, "Expected %s; got %s" % (next_step, leaf))
14 if os.path.exists(leaf):
15 self.send_response(200)
16 self.end_headers()
17 self.wfile.write(file(leaf).read())
18 self.wfile.close()
19 else:
20 self.send_error(404, "Missing: %s" % leaf)
22 def handle_requests(*script):
23 server_address = ('localhost', 8000)
24 httpd = BaseHTTPServer.HTTPServer(server_address, MyHandler)
25 child = os.fork()
26 if child:
27 return child
28 # We are the child
29 try:
30 sys.stderr = sys.stdout
31 print "Waiting for request"
32 global next_step
33 for next_step in script:
34 if type(next_step) != tuple: next_step = (next_step,)
35 for x in next_step:
36 httpd.handle_request()
37 print "Done"
38 os._exit(0)
39 except:
40 traceback.print_exc()
41 os._exit(1)