Show More
@@ -1,72 +1,75 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 os | |
11 | import signal |
|
11 | import signal | |
12 | import socket |
|
12 | import socket | |
13 | import sys |
|
13 | import sys | |
14 |
|
14 | |||
15 | from mercurial import ( |
|
15 | from mercurial import ( | |
|
16 | pycompat, | |||
16 | server, |
|
17 | server, | |
17 | util, |
|
18 | util, | |
18 | ) |
|
19 | ) | |
19 |
|
20 | |||
20 | httpserver = util.httpserver |
|
21 | httpserver = util.httpserver | |
21 | OptionParser = optparse.OptionParser |
|
22 | OptionParser = optparse.OptionParser | |
22 |
|
23 | |||
23 | if os.environ.get('HGIPV6', '0') == '1': |
|
24 | if os.environ.get('HGIPV6', '0') == '1': | |
24 | class simplehttpserver(httpserver.httpserver): |
|
25 | class simplehttpserver(httpserver.httpserver): | |
25 | address_family = socket.AF_INET6 |
|
26 | address_family = socket.AF_INET6 | |
26 | else: |
|
27 | else: | |
27 | simplehttpserver = httpserver.httpserver |
|
28 | simplehttpserver = httpserver.httpserver | |
28 |
|
29 | |||
29 | class _httprequesthandler(httpserver.simplehttprequesthandler): |
|
30 | class _httprequesthandler(httpserver.simplehttprequesthandler): | |
30 | def log_message(self, format, *args): |
|
31 | def log_message(self, format, *args): | |
31 | httpserver.simplehttprequesthandler.log_message(self, format, *args) |
|
32 | httpserver.simplehttprequesthandler.log_message(self, format, *args) | |
32 | sys.stderr.flush() |
|
33 | sys.stderr.flush() | |
33 |
|
34 | |||
34 | class simplehttpservice(object): |
|
35 | class simplehttpservice(object): | |
35 | def __init__(self, host, port): |
|
36 | def __init__(self, host, port): | |
36 | self.address = (host, port) |
|
37 | self.address = (host, port) | |
37 | def init(self): |
|
38 | def init(self): | |
38 | self.httpd = simplehttpserver(self.address, _httprequesthandler) |
|
39 | self.httpd = simplehttpserver(self.address, _httprequesthandler) | |
39 | def run(self): |
|
40 | def run(self): | |
40 | self.httpd.serve_forever() |
|
41 | self.httpd.serve_forever() | |
41 |
|
42 | |||
42 | if __name__ == '__main__': |
|
43 | if __name__ == '__main__': | |
43 | parser = OptionParser() |
|
44 | parser = OptionParser() | |
44 | parser.add_option('-p', '--port', dest='port', type='int', default=8000, |
|
45 | parser.add_option('-p', '--port', dest='port', type='int', default=8000, | |
45 | help='TCP port to listen on', metavar='PORT') |
|
46 | help='TCP port to listen on', metavar='PORT') | |
46 | parser.add_option('-H', '--host', dest='host', default='localhost', |
|
47 | parser.add_option('-H', '--host', dest='host', default='localhost', | |
47 | help='hostname or IP to listen on', metavar='HOST') |
|
48 | help='hostname or IP to listen on', metavar='HOST') | |
48 | parser.add_option('--logfile', help='file name of access/error log') |
|
49 | parser.add_option('--logfile', help='file name of access/error log') | |
49 | parser.add_option('--pid', dest='pid', |
|
50 | parser.add_option('--pid', dest='pid', | |
50 | help='file name where the PID of the server is stored') |
|
51 | help='file name where the PID of the server is stored') | |
51 | parser.add_option('-f', '--foreground', dest='foreground', |
|
52 | parser.add_option('-f', '--foreground', dest='foreground', | |
52 | action='store_true', |
|
53 | action='store_true', | |
53 | help='do not start the HTTP server in the background') |
|
54 | help='do not start the HTTP server in the background') | |
54 | parser.add_option('--daemon-postexec', action='append') |
|
55 | parser.add_option('--daemon-postexec', action='append') | |
55 |
|
56 | |||
56 | (options, args) = parser.parse_args() |
|
57 | (options, args) = parser.parse_args() | |
57 |
|
58 | |||
58 | signal.signal(signal.SIGTERM, lambda x, y: sys.exit(0)) |
|
59 | signal.signal(signal.SIGTERM, lambda x, y: sys.exit(0)) | |
59 |
|
60 | |||
60 | if options.foreground and options.logfile: |
|
61 | if options.foreground and options.logfile: | |
61 | parser.error("options --logfile and --foreground are mutually " |
|
62 | parser.error("options --logfile and --foreground are mutually " | |
62 | "exclusive") |
|
63 | "exclusive") | |
63 | if options.foreground and options.pid: |
|
64 | if options.foreground and options.pid: | |
64 | parser.error("options --pid and --foreground are mutually exclusive") |
|
65 | parser.error("options --pid and --foreground are mutually exclusive") | |
65 |
|
66 | |||
66 | opts = {'pid_file': options.pid, |
|
67 | opts = {b'pid_file': options.pid, | |
67 | 'daemon': not options.foreground, |
|
68 | b'daemon': not options.foreground, | |
68 | 'daemon_postexec': options.daemon_postexec} |
|
69 | b'daemon_postexec': options.daemon_postexec} | |
69 | service = simplehttpservice(options.host, options.port) |
|
70 | service = simplehttpservice(options.host, options.port) | |
|
71 | runargs = [sys.executable, __file__] + sys.argv[1:] | |||
|
72 | runargs = [pycompat.fsencode(a) for a in runargs] | |||
70 | server.runservice(opts, initfn=service.init, runfn=service.run, |
|
73 | server.runservice(opts, initfn=service.init, runfn=service.run, | |
71 | logfile=options.logfile, |
|
74 | logfile=options.logfile, | |
72 |
runargs= |
|
75 | runargs=runargs) |
General Comments 0
You need to be logged in to leave comments.
Login now