/usr/lib/python3/dist-packages/mypy/dmypy.py is in python3-mypy 0.560-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | """Client for mypy daemon mode.
Highly experimental! Only supports UNIX-like systems.
This manages a daemon process which keeps useful state in memory
rather than having to read it back from disk on each run.
"""
import argparse
import json
import os
import signal
import socket
import sys
import time
from typing import Any, Callable, Dict, List, Mapping, Optional, Sequence, Tuple, TypeVar
from mypy.dmypy_util import STATUS_FILE, receive
# Argument parser. Subparsers are tied to action functions by the
# @action(subparse) decorator.
parser = argparse.ArgumentParser(description="Client for mypy daemon mode",
fromfile_prefix_chars='@')
parser.set_defaults(action=None)
subparsers = parser.add_subparsers()
start_parser = subparsers.add_parser('start', help="Start daemon")
start_parser.add_argument('--log-file', metavar='FILE', type=str,
help="Direct daemon stdout/stderr to FILE")
start_parser.add_argument('flags', metavar='FLAG', nargs='*', type=str,
help="Regular mypy flags (precede with --)")
status_parser = subparsers.add_parser('status', help="Show daemon status")
stop_parser = subparsers.add_parser('stop', help="Stop daemon (asks it politely to go away)")
kill_parser = subparsers.add_parser('kill', help="Kill daemon (kills the process)")
restart_parser = subparsers.add_parser('restart',
help="Restart daemon (stop or kill followed by start)")
restart_parser.add_argument('--log-file', metavar='FILE', type=str,
help="Direct daemon stdout/stderr to FILE")
restart_parser.add_argument('flags', metavar='FLAG', nargs='*', type=str,
help="Regular mypy flags (precede with --)")
check_parser = subparsers.add_parser('check', help="Check some files (requires running daemon)")
check_parser.add_argument('-q', '--quiet', action='store_true',
help="Suppress instrumentation stats")
check_parser.add_argument('files', metavar='FILE', nargs='+', help="File (or directory) to check")
recheck_parser = subparsers.add_parser('recheck',
help="Check the same files as the most previous check run (requires running daemon)")
recheck_parser.add_argument('-q', '--quiet', action='store_true',
help="Suppress instrumentation stats")
hang_parser = subparsers.add_parser('hang', help="Hang for 100 seconds")
daemon_parser = subparsers.add_parser('daemon', help="Run daemon in foreground")
daemon_parser.add_argument('flags', metavar='FLAG', nargs='*', type=str,
help="Regular mypy flags (precede with --)")
help_parser = subparsers.add_parser('help')
def main() -> None:
"""The code is top-down."""
args = parser.parse_args()
if not args.action:
parser.print_usage()
else:
args.action(args)
ActionFunction = Callable[[argparse.Namespace], None]
def action(subparser: argparse.ArgumentParser) -> Callable[[ActionFunction], None]:
"""Decorator to tie an action function to a subparser."""
def register(func: ActionFunction) -> None:
subparser.set_defaults(action=func)
return register
# Action functions (run in client from command line).
# TODO: Use a separate exception instead of SystemExit to indicate failures.
@action(start_parser)
def do_start(args: argparse.Namespace) -> None:
"""Start daemon (it must not already be running).
This is where mypy flags are set. Setting flags is a bit awkward;
you have to use e.g.:
dmypy start -- --strict
since we don't want to duplicate mypy's huge list of flags.
"""
try:
pid, sockname = get_status()
except SystemExit as err:
# Lazy import so this import doesn't slow down other commands.
from mypy.dmypy_server import daemonize, Server
if daemonize(Server(args.flags).serve, args.log_file):
sys.exit(1)
wait_for_server()
else:
sys.exit("Daemon is still alive")
@action(status_parser)
def do_status(args: argparse.Namespace) -> None:
"""Print daemon status.
This verifies that it is responsive to requests.
"""
status = read_status()
show_stats(status)
check_status(status)
try:
response = request('status')
except Exception as err:
print("Daemon is stuck; consider %s kill" % sys.argv[0])
raise
else:
show_stats(response)
@action(stop_parser)
def do_stop(args: argparse.Namespace) -> None:
"""Stop daemon politely (via a request)."""
try:
response = request('stop')
except Exception as err:
sys.exit("Daemon is stuck; consider %s kill" % sys.argv[0])
else:
if response:
print("Stop response:", response)
else:
print("Daemon stopped")
@action(kill_parser)
def do_kill(args: argparse.Namespace) -> None:
"""Kill daemon rudely (by killing the process)."""
pid, sockname = get_status()
try:
os.kill(pid, signal.SIGKILL)
except os.error as err:
sys.exit(str(err))
else:
print("Daemon killed")
@action(restart_parser)
def do_restart(args: argparse.Namespace) -> None:
"""Restart daemon.
We first try to stop it politely if it's running. This also sets
mypy flags (and has the same issues as start).
"""
try:
response = request('stop')
except SystemExit:
pass
else:
if response:
sys.exit("Status: %s" % str(response))
else:
print("Daemon stopped")
# Lazy import so this import doesn't slow down other commands.
from mypy.dmypy_server import daemonize, Server
if daemonize(Server(args.flags).serve, args.log_file):
sys.exit(1)
wait_for_server()
def wait_for_server(timeout: float = 5.0) -> None:
"""Wait until the server is up.
Exit if it doesn't happen within the timeout.
"""
endtime = time.time() + timeout
while time.time() < endtime:
try:
data = read_status()
except SystemExit:
# If the file isn't there yet, retry later.
time.sleep(0.1)
continue
# If the file's content is bogus or the process is dead, fail.
pid, sockname = check_status(data)
print("Daemon started")
return
sys.exit("Timed out waiting for daemon to start")
@action(check_parser)
def do_check(args: argparse.Namespace) -> None:
"""Ask the daemon to check a list of files."""
t0 = time.time()
response = request('check', files=args.files)
t1 = time.time()
response['roundtrip_time'] = t1 - t0
check_output(response, args.quiet)
@action(recheck_parser)
def do_recheck(args: argparse.Namespace) -> None:
"""Ask the daemon to check the same list of files it checked most recently.
This doesn't work across daemon restarts.
"""
t0 = time.time()
response = request('recheck')
t1 = time.time()
response['roundtrip_time'] = t1 - t0
check_output(response, args.quiet)
def check_output(response: Dict[str, Any], quiet: bool) -> None:
"""Print the output from a check or recheck command."""
try:
out, err, status = response['out'], response['err'], response['status']
except KeyError:
sys.exit("Response: %s" % str(response))
sys.stdout.write(out)
sys.stderr.write(err)
if not quiet:
show_stats(response)
if status:
sys.exit(status)
def show_stats(response: Mapping[str, object]) -> None:
for key, value in sorted(response.items()):
if key not in ('out', 'err'):
print("%-24s: %10s" % (key, "%.3f" % value if isinstance(value, float) else value))
@action(hang_parser)
def do_hang(args: argparse.Namespace) -> None:
"""Hang for 100 seconds, as a debug hack."""
request('hang')
@action(daemon_parser)
def do_daemon(args: argparse.Namespace) -> None:
"""Serve requests in the foreground."""
# Lazy import so this import doesn't slow down other commands.
from mypy.dmypy_server import Server
Server(args.flags).serve()
@action(help_parser)
def do_help(args: argparse.Namespace) -> None:
"""Print full help (same as dmypy --help)."""
parser.print_help()
# Client-side infrastructure.
def request(command: str, **kwds: object) -> Dict[str, Any]:
"""Send a request to the daemon.
Return the JSON dict with the response.
"""
args = dict(kwds)
if command:
args.update(command=command)
data = json.dumps(args)
pid, sockname = get_status()
sock = socket.socket(socket.AF_UNIX)
sock.connect(sockname)
sock.sendall(data.encode('utf8'))
sock.shutdown(socket.SHUT_WR)
try:
response = receive(sock)
except OSError as err:
return {'error': str(err)}
else:
return response
finally:
sock.close()
def get_status() -> Tuple[int, str]:
"""Read status file and check if the process is alive.
Return (pid, sockname) on success.
Raise SystemExit(<message>) if something's wrong.
"""
data = read_status()
return check_status(data)
def check_status(data: Dict[str, Any]) -> Tuple[int, str]:
"""Check if the process is alive.
Return (pid, sockname) on success.
Raise SystemExit(<message>) if something's wrong.
"""
if 'pid' not in data:
raise SystemExit("Invalid status file (no pid field)")
pid = data['pid']
if not isinstance(pid, int):
raise SystemExit("pid field is not an int")
try:
os.kill(pid, 0)
except OSError as err:
raise SystemExit("Daemon has died")
if 'sockname' not in data:
raise SystemExit("Invalid status file (no sockname field)")
sockname = data['sockname']
if not isinstance(sockname, str):
raise SystemExit("sockname field is not a string")
return pid, sockname
def read_status() -> Dict[str, object]:
"""Read status file."""
if not os.path.isfile(STATUS_FILE):
raise SystemExit("No status file found")
with open(STATUS_FILE) as f:
try:
data = json.load(f)
except Exception as err:
raise SystemExit("Malformed status file (not JSON)")
if not isinstance(data, dict):
raise SystemExit("Invalid status file (not a dict)")
return data
# Run main().
if __name__ == '__main__':
main()
|