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