Show More
@@ -1,55 +1,63 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 |
|
2 | |||
3 | from __future__ import absolute_import |
|
3 | from __future__ import absolute_import | |
4 |
|
4 | |||
5 | """ |
|
5 | """ | |
6 | Small and dumb HTTP server for use in tests. |
|
6 | Small and dumb HTTP server for use in tests. | |
7 | """ |
|
7 | """ | |
8 |
|
8 | |||
9 | import optparse |
|
9 | import optparse | |
|
10 | import os | |||
10 | import signal |
|
11 | import signal | |
|
12 | import socket | |||
11 | import sys |
|
13 | import sys | |
12 |
|
14 | |||
13 | from mercurial import ( |
|
15 | from mercurial import ( | |
14 | server, |
|
16 | server, | |
15 | util, |
|
17 | util, | |
16 | ) |
|
18 | ) | |
17 |
|
19 | |||
18 | httpserver = util.httpserver |
|
20 | httpserver = util.httpserver | |
19 | OptionParser = optparse.OptionParser |
|
21 | OptionParser = optparse.OptionParser | |
20 |
|
22 | |||
|
23 | if os.environ.get('HGIPV6', '0') == '1': | |||
|
24 | class simplehttpserver(httpserver.httpserver): | |||
|
25 | address_family = socket.AF_INET6 | |||
|
26 | else: | |||
|
27 | simplehttpserver = httpserver.httpserver | |||
|
28 | ||||
21 | class simplehttpservice(object): |
|
29 | class simplehttpservice(object): | |
22 | def __init__(self, host, port): |
|
30 | def __init__(self, host, port): | |
23 | self.address = (host, port) |
|
31 | self.address = (host, port) | |
24 | def init(self): |
|
32 | def init(self): | |
25 |
self.httpd = |
|
33 | self.httpd = simplehttpserver( | |
26 | self.address, httpserver.simplehttprequesthandler) |
|
34 | self.address, httpserver.simplehttprequesthandler) | |
27 | def run(self): |
|
35 | def run(self): | |
28 | self.httpd.serve_forever() |
|
36 | self.httpd.serve_forever() | |
29 |
|
37 | |||
30 | if __name__ == '__main__': |
|
38 | if __name__ == '__main__': | |
31 | parser = OptionParser() |
|
39 | parser = OptionParser() | |
32 | parser.add_option('-p', '--port', dest='port', type='int', default=8000, |
|
40 | parser.add_option('-p', '--port', dest='port', type='int', default=8000, | |
33 | help='TCP port to listen on', metavar='PORT') |
|
41 | help='TCP port to listen on', metavar='PORT') | |
34 | parser.add_option('-H', '--host', dest='host', default='localhost', |
|
42 | parser.add_option('-H', '--host', dest='host', default='localhost', | |
35 | help='hostname or IP to listen on', metavar='HOST') |
|
43 | help='hostname or IP to listen on', metavar='HOST') | |
36 | parser.add_option('--pid', dest='pid', |
|
44 | parser.add_option('--pid', dest='pid', | |
37 | help='file name where the PID of the server is stored') |
|
45 | help='file name where the PID of the server is stored') | |
38 | parser.add_option('-f', '--foreground', dest='foreground', |
|
46 | parser.add_option('-f', '--foreground', dest='foreground', | |
39 | action='store_true', |
|
47 | action='store_true', | |
40 | help='do not start the HTTP server in the background') |
|
48 | help='do not start the HTTP server in the background') | |
41 | parser.add_option('--daemon-postexec', action='append') |
|
49 | parser.add_option('--daemon-postexec', action='append') | |
42 |
|
50 | |||
43 | (options, args) = parser.parse_args() |
|
51 | (options, args) = parser.parse_args() | |
44 |
|
52 | |||
45 | signal.signal(signal.SIGTERM, lambda x, y: sys.exit(0)) |
|
53 | signal.signal(signal.SIGTERM, lambda x, y: sys.exit(0)) | |
46 |
|
54 | |||
47 | if options.foreground and options.pid: |
|
55 | if options.foreground and options.pid: | |
48 | parser.error("options --pid and --foreground are mutually exclusive") |
|
56 | parser.error("options --pid and --foreground are mutually exclusive") | |
49 |
|
57 | |||
50 | opts = {'pid_file': options.pid, |
|
58 | opts = {'pid_file': options.pid, | |
51 | 'daemon': not options.foreground, |
|
59 | 'daemon': not options.foreground, | |
52 | 'daemon_postexec': options.daemon_postexec} |
|
60 | 'daemon_postexec': options.daemon_postexec} | |
53 | service = simplehttpservice(options.host, options.port) |
|
61 | service = simplehttpservice(options.host, options.port) | |
54 | server.runservice(opts, initfn=service.init, runfn=service.run, |
|
62 | server.runservice(opts, initfn=service.init, runfn=service.run, | |
55 | runargs=[sys.executable, __file__] + sys.argv[1:]) |
|
63 | runargs=[sys.executable, __file__] + sys.argv[1:]) |
General Comments 0
You need to be logged in to leave comments.
Login now