sapling/tests/dumbhttp.py
Jun Wu 9dc21f8d0b codemod: import from the edenscm package
Summary:
D13853115 adds `edenscm/` to `sys.path` and code still uses `import mercurial`.
That has nasty problems if both `import mercurial` and
`import edenscm.mercurial` are used, because Python would think `mercurial.foo`
and `edenscm.mercurial.foo` are different modules so code like
`try: ... except mercurial.error.Foo: ...`, or `isinstance(x, mercurial.foo.Bar)`
would fail to handle the `edenscm.mercurial` version. There are also some
module-level states (ex. `extensions._extensions`) that would cause trouble if
they have multiple versions in a single process.

Change imports to use the `edenscm` so ideally the `mercurial` is no longer
imported at all. Add checks in extensions.py to catch unexpected extensions
importing modules from the old (wrong) locations when running tests.

Reviewed By: phillco

Differential Revision: D13868981

fbshipit-source-id: f4e2513766957fd81d85407994f7521a08e4de48
2019-01-29 17:25:32 -08:00

103 lines
2.5 KiB
Python
Executable File

#!/usr/bin/env python
from __future__ import absolute_import
import optparse
import os
import signal
import socket
import sys
from edenscm.mercurial import server, util
"""
Small and dumb HTTP server for use in tests.
"""
httpserver = util.httpserver
OptionParser = optparse.OptionParser
if os.environ.get("HGIPV6", "0") == "1":
class simplehttpserver(httpserver.httpserver):
address_family = socket.AF_INET6
else:
simplehttpserver = httpserver.httpserver
class _httprequesthandler(httpserver.simplehttprequesthandler):
def log_message(self, format, *args):
httpserver.simplehttprequesthandler.log_message(self, format, *args)
sys.stderr.flush()
class simplehttpservice(object):
def __init__(self, host, port):
self.address = (host, port)
def init(self):
self.httpd = simplehttpserver(self.address, _httprequesthandler)
def run(self):
self.httpd.serve_forever()
if __name__ == "__main__":
parser = OptionParser()
parser.add_option(
"-p",
"--port",
dest="port",
type="int",
default=8000,
help="TCP port to listen on",
metavar="PORT",
)
parser.add_option(
"-H",
"--host",
dest="host",
default="localhost",
help="hostname or IP to listen on",
metavar="HOST",
)
parser.add_option("--logfile", help="file name of access/error log")
parser.add_option(
"--pid", dest="pid", help="file name where the PID of the server is stored"
)
parser.add_option(
"-f",
"--foreground",
dest="foreground",
action="store_true",
help="do not start the HTTP server in the background",
)
parser.add_option("--daemon-postexec", action="append")
(options, args) = parser.parse_args()
signal.signal(signal.SIGTERM, lambda x, y: sys.exit(0))
if options.foreground and options.logfile:
parser.error("options --logfile and --foreground are mutually " "exclusive")
if options.foreground and options.pid:
parser.error("options --pid and --foreground are mutually exclusive")
opts = {
"pid_file": options.pid,
"daemon": not options.foreground,
"daemon_postexec": options.daemon_postexec,
}
service = simplehttpservice(options.host, options.port)
server.runservice(
opts,
initfn=service.init,
runfn=service.run,
logfile=options.logfile,
runargs=[sys.executable, __file__] + sys.argv[1:],
)