author | Matt Mackall <mpm@selenic.com> |
Thu, 01 Jan 2015 16:29:51 -0600 | |
branch | stable |
changeset 23697 | e88c08a82edd |
parent 22959 | 10116463b0b1 |
child 23136 | 6eab50a34fed |
permissions | -rw-r--r-- |
22959
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
1 |
#!/usr/bin/env python |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
2 |
|
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
3 |
""" |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
4 |
Small and dumb HTTP server for use in tests. |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
5 |
""" |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
6 |
|
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
7 |
from optparse import OptionParser |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
8 |
import BaseHTTPServer, SimpleHTTPServer, os, signal, subprocess, sys |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
9 |
|
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
10 |
|
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
11 |
def run(server_class=BaseHTTPServer.HTTPServer, |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
12 |
handler_class=SimpleHTTPServer.SimpleHTTPRequestHandler, |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
13 |
server_address=('localhost', 8000)): |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
14 |
httpd = server_class(server_address, handler_class) |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
15 |
httpd.serve_forever() |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
16 |
|
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
17 |
|
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
18 |
if __name__ == '__main__': |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
19 |
parser = OptionParser() |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
20 |
parser.add_option('-p', '--port', dest='port', type='int', default=8000, |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
21 |
help='TCP port to listen on', metavar='PORT') |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
22 |
parser.add_option('-H', '--host', dest='host', default='localhost', |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
23 |
help='hostname or IP to listen on', metavar='HOST') |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
24 |
parser.add_option('--pid', dest='pid', |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
25 |
help='file name where the PID of the server is stored') |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
26 |
parser.add_option('-f', '--foreground', dest='foreground', |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
27 |
action='store_true', |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
28 |
help='do not start the HTTP server in the background') |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
29 |
|
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
30 |
(options, args) = parser.parse_args() |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
31 |
|
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
32 |
signal.signal(signal.SIGTERM, lambda x, y: sys.exit(0)) |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
33 |
|
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
34 |
if options.foreground and options.pid: |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
35 |
parser.error("options --pid and --foreground are mutually exclusive") |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
36 |
|
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
37 |
if options.foreground: |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
38 |
run(server_address=(options.host, options.port)) |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
39 |
else: |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
40 |
# This doesn't attempt to cleanly detach the process, as it's not |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
41 |
# meant to be a long-lived, independent process. As a consequence, |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
42 |
# it's still part of the same process group, and keeps any file |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
43 |
# descriptors it might have inherited besided stdin/stdout/stderr. |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
44 |
# Trying to do things cleanly is more complicated, requires |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
45 |
# OS-dependent code, and is not worth the effort. |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
46 |
proc = subprocess.Popen([sys.executable, __file__, '-f', |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
47 |
'-H', options.host, '-p', str(options.port)], |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
48 |
stdin=open(os.devnull, 'r'), |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
49 |
stdout=open(os.devnull, 'w'), |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
50 |
stderr=subprocess.STDOUT) |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
51 |
if options.pid: |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
52 |
fp = file(options.pid, 'wb') |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
53 |
fp.write(str(proc.pid) + '\n') |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
54 |
fp.close() |