-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
dmypy_server.py
574 lines (508 loc) · 23.5 KB
/
dmypy_server.py
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
"""Server for mypy daemon mode.
This implements a daemon process which keeps useful state in memory
to enable fine-grained incremental reprocessing of changes.
"""
import argparse
import base64
import io
import json
import os
import pickle
import subprocess
import sys
import time
import traceback
from contextlib import redirect_stderr, redirect_stdout
from typing import AbstractSet, Any, Callable, Dict, List, Mapping, Optional, Sequence, Tuple
import mypy.build
import mypy.errors
import mypy.main
from mypy.find_sources import create_source_list, InvalidSourceList
from mypy.server.update import FineGrainedBuildManager
from mypy.dmypy_util import receive
from mypy.ipc import IPCServer
from mypy.fscache import FileSystemCache
from mypy.fswatcher import FileSystemWatcher, FileData
from mypy.modulefinder import BuildSource, compute_search_paths
from mypy.options import Options
from mypy.suggestions import SuggestionFailure, SuggestionEngine
from mypy.typestate import reset_global_state
from mypy.version import __version__
MYPY = False
if MYPY:
from typing_extensions import Final
MEM_PROFILE = False # type: Final # If True, dump memory profile after initialization
if sys.platform == 'win32':
from subprocess import STARTUPINFO
def daemonize(options: Options,
status_file: str,
timeout: Optional[int] = None,
log_file: Optional[str] = None) -> int:
"""Create the daemon process via "dmypy daemon" and pass options via command line
When creating the daemon grandchild, we create it in a new console, which is
started hidden. We cannot use DETACHED_PROCESS since it will cause console windows
to pop up when starting. See
https://github.com/python/cpython/pull/4150#issuecomment-340215696
for more on why we can't have nice things.
It also pickles the options to be unpickled by mypy.
"""
command = [sys.executable, '-m', 'mypy.dmypy', '--status-file', status_file, 'daemon']
pickeled_options = pickle.dumps((options.snapshot(), timeout, log_file))
command.append('--options-data="{}"'.format(base64.b64encode(pickeled_options).decode()))
info = STARTUPINFO()
info.dwFlags = 0x1 # STARTF_USESHOWWINDOW aka use wShowWindow's value
info.wShowWindow = 0 # SW_HIDE aka make the window invisible
try:
subprocess.Popen(command,
creationflags=0x10, # CREATE_NEW_CONSOLE
startupinfo=info)
return 0
except subprocess.CalledProcessError as e:
return e.returncode
else:
def _daemonize_cb(func: Callable[[], None], log_file: Optional[str] = None) -> int:
"""Arrange to call func() in a grandchild of the current process.
Return 0 for success, exit status for failure, negative if
subprocess killed by signal.
"""
# See https://stackoverflow.com/questions/473620/how-do-you-create-a-daemon-in-python
sys.stdout.flush()
sys.stderr.flush()
pid = os.fork()
if pid:
# Parent process: wait for child in case things go bad there.
npid, sts = os.waitpid(pid, 0)
sig = sts & 0xff
if sig:
print("Child killed by signal", sig)
return -sig
sts = sts >> 8
if sts:
print("Child exit status", sts)
return sts
# Child process: do a bunch of UNIX stuff and then fork a grandchild.
try:
os.setsid() # Detach controlling terminal
os.umask(0o27)
devnull = os.open('/dev/null', os.O_RDWR)
os.dup2(devnull, 0)
os.dup2(devnull, 1)
os.dup2(devnull, 2)
os.close(devnull)
pid = os.fork()
if pid:
# Child is done, exit to parent.
os._exit(0)
# Grandchild: run the server.
if log_file:
sys.stdout = sys.stderr = open(log_file, 'a', buffering=1)
fd = sys.stdout.fileno()
os.dup2(fd, 2)
os.dup2(fd, 1)
func()
finally:
# Make sure we never get back into the caller.
os._exit(1)
def daemonize(options: Options,
status_file: str,
timeout: Optional[int] = None,
log_file: Optional[str] = None) -> int:
"""Run the mypy daemon in a grandchild of the current process
Return 0 for success, exit status for failure, negative if
subprocess killed by signal.
"""
return _daemonize_cb(Server(options, status_file, timeout).serve, log_file)
# Server code.
CONNECTION_NAME = 'dmypy' # type: Final
def process_start_options(flags: List[str], allow_sources: bool) -> Options:
sources, options = mypy.main.process_options(['-i'] + flags,
require_targets=False,
server_options=True)
if sources and not allow_sources:
sys.exit("dmypy: start/restart does not accept sources")
if options.report_dirs:
sys.exit("dmypy: start/restart cannot generate reports")
if options.junit_xml:
sys.exit("dmypy: start/restart does not support --junit-xml; "
"pass it to check/recheck instead")
if not options.incremental:
sys.exit("dmypy: start/restart should not disable incremental mode")
# Our file change tracking can't yet handle changes to files that aren't
# specified in the sources list.
if options.follow_imports not in ('skip', 'error'):
sys.exit("dmypy: follow-imports must be 'skip' or 'error'")
return options
ModulePathPair = Tuple[str, str]
ModulePathPairs = List[ModulePathPair]
ChangesAndRemovals = Tuple[ModulePathPairs, ModulePathPairs]
class Server:
# NOTE: the instance is constructed in the parent process but
# serve() is called in the grandchild (by daemonize()).
def __init__(self, options: Options,
status_file: str,
timeout: Optional[int] = None) -> None:
"""Initialize the server with the desired mypy flags."""
self.options = options
# Snapshot the options info before we muck with it, to detect changes
self.options_snapshot = options.snapshot()
self.timeout = timeout
self.fine_grained_manager = None # type: Optional[FineGrainedBuildManager]
if os.path.isfile(status_file):
os.unlink(status_file)
self.fscache = FileSystemCache()
options.incremental = True
options.fine_grained_incremental = True
options.show_traceback = True
if options.use_fine_grained_cache:
# Using fine_grained_cache implies generating and caring
# about the fine grained cache
options.cache_fine_grained = True
else:
options.cache_dir = os.devnull
# Fine-grained incremental doesn't support general partial types
# (details in https://github.com/python/mypy/issues/4492)
options.local_partial_types = True
self.status_file = status_file
def _response_metadata(self) -> Dict[str, str]:
py_version = '{}_{}'.format(self.options.python_version[0], self.options.python_version[1])
return {
'platform': self.options.platform,
'python_version': py_version,
}
def serve(self) -> None:
"""Serve requests, synchronously (no thread or fork)."""
command = None
try:
server = IPCServer(CONNECTION_NAME, self.timeout)
with open(self.status_file, 'w') as f:
json.dump({'pid': os.getpid(), 'connection_name': server.connection_name}, f)
f.write('\n') # I like my JSON with a trailing newline
while True:
with server:
data = receive(server)
resp = {} # type: Dict[str, Any]
if 'command' not in data:
resp = {'error': "No command found in request"}
else:
command = data['command']
if not isinstance(command, str):
resp = {'error': "Command is not a string"}
else:
command = data.pop('command')
try:
resp = self.run_command(command, data)
except Exception:
# If we are crashing, report the crash to the client
tb = traceback.format_exception(*sys.exc_info())
resp = {'error': "Daemon crashed!\n" + "".join(tb)}
resp.update(self._response_metadata())
server.write(json.dumps(resp).encode('utf8'))
raise
try:
resp.update(self._response_metadata())
server.write(json.dumps(resp).encode('utf8'))
except OSError:
pass # Maybe the client hung up
if command == 'stop':
reset_global_state()
sys.exit(0)
finally:
# If the final command is something other than a clean
# stop, remove the status file. (We can't just
# simplify the logic and always remove the file, since
# that could cause us to remove a future server's
# status file.)
if command != 'stop':
os.unlink(self.status_file)
try:
server.cleanup() # try to remove the socket dir on Linux
except OSError:
pass
exc_info = sys.exc_info()
if exc_info[0] and exc_info[0] is not SystemExit:
traceback.print_exception(*exc_info)
def run_command(self, command: str, data: Mapping[str, object]) -> Dict[str, object]:
"""Run a specific command from the registry."""
key = 'cmd_' + command
method = getattr(self.__class__, key, None)
if method is None:
return {'error': "Unrecognized command '%s'" % command}
else:
return method(self, **data)
# Command functions (run in the server via RPC).
def cmd_status(self, fswatcher_dump_file: Optional[str] = None) -> Dict[str, object]:
"""Return daemon status."""
res = {} # type: Dict[str, object]
res.update(get_meminfo())
if fswatcher_dump_file:
data = self.fswatcher.dump_file_data() if hasattr(self, 'fswatcher') else {}
# Using .dumps and then writing was noticably faster than using dump
s = json.dumps(data)
with open(fswatcher_dump_file, 'w') as f:
f.write(s)
return res
def cmd_stop(self) -> Dict[str, object]:
"""Stop daemon."""
# We need to remove the status file *before* we complete the
# RPC. Otherwise a race condition exists where a subsequent
# command can see a status file from a dying server and think
# it is a live one.
os.unlink(self.status_file)
return {}
def cmd_run(self, version: str, args: Sequence[str]) -> Dict[str, object]:
"""Check a list of files, triggering a restart if needed."""
try:
# Process options can exit on improper arguments, so we need to catch that and
# capture stderr so the client can report it
stderr = io.StringIO()
stdout = io.StringIO()
with redirect_stderr(stderr):
with redirect_stdout(stdout):
sources, options = mypy.main.process_options(
['-i'] + list(args),
require_targets=True,
server_options=True,
fscache=self.fscache,
program='mypy-daemon',
header=argparse.SUPPRESS)
# Signal that we need to restart if the options have changed
if self.options_snapshot != options.snapshot():
return {'restart': 'configuration changed'}
if __version__ != version:
return {'restart': 'mypy version changed'}
if self.fine_grained_manager:
manager = self.fine_grained_manager.manager
start_plugins_snapshot = manager.plugins_snapshot
_, current_plugins_snapshot = mypy.build.load_plugins(options, manager.errors)
if current_plugins_snapshot != start_plugins_snapshot:
return {'restart': 'plugins changed'}
except InvalidSourceList as err:
return {'out': '', 'err': str(err), 'status': 2}
except SystemExit as e:
return {'out': stdout.getvalue(), 'err': stderr.getvalue(), 'status': e.code}
return self.check(sources)
def cmd_check(self, files: Sequence[str]) -> Dict[str, object]:
"""Check a list of files."""
try:
sources = create_source_list(files, self.options, self.fscache)
except InvalidSourceList as err:
return {'out': '', 'err': str(err), 'status': 2}
return self.check(sources)
def cmd_recheck(self,
remove: Optional[List[str]] = None,
update: Optional[List[str]] = None) -> Dict[str, object]:
"""Check the same list of files we checked most recently.
If remove/update is given, they modify the previous list;
if all are None, stat() is called for each file in the previous list.
"""
t0 = time.time()
if not self.fine_grained_manager:
return {'error': "Command 'recheck' is only valid after a 'check' command"}
sources = self.previous_sources
if remove:
removals = set(remove)
sources = [s for s in sources if s.path and s.path not in removals]
if update:
known = {s.path for s in sources if s.path}
added = [p for p in update if p not in known]
try:
added_sources = create_source_list(added, self.options, self.fscache)
except InvalidSourceList as err:
return {'out': '', 'err': str(err), 'status': 2}
sources = sources + added_sources # Make a copy!
t1 = time.time()
manager = self.fine_grained_manager.manager
manager.log("fine-grained increment: cmd_recheck: {:.3f}s".format(t1 - t0))
res = self.fine_grained_increment(sources, remove, update)
self.fscache.flush()
self.update_stats(res)
return res
def check(self, sources: List[BuildSource]) -> Dict[str, Any]:
"""Check using fine-grained incremental mode."""
if not self.fine_grained_manager:
res = self.initialize_fine_grained(sources)
else:
res = self.fine_grained_increment(sources)
self.fscache.flush()
self.update_stats(res)
return res
def update_stats(self, res: Dict[str, Any]) -> None:
if self.fine_grained_manager:
manager = self.fine_grained_manager.manager
manager.dump_stats()
res['stats'] = manager.stats
manager.stats = {}
def initialize_fine_grained(self, sources: List[BuildSource]) -> Dict[str, Any]:
self.fswatcher = FileSystemWatcher(self.fscache)
t0 = time.time()
self.update_sources(sources)
t1 = time.time()
try:
result = mypy.build.build(sources=sources,
options=self.options,
fscache=self.fscache)
except mypy.errors.CompileError as e:
output = ''.join(s + '\n' for s in e.messages)
if e.use_stdout:
out, err = output, ''
else:
out, err = '', output
return {'out': out, 'err': err, 'status': 2}
messages = result.errors
self.fine_grained_manager = FineGrainedBuildManager(result)
self.previous_sources = sources
# If we are using the fine-grained cache, build hasn't actually done
# the typechecking on the updated files yet.
# Run a fine-grained update starting from the cached data
if result.used_cache:
t2 = time.time()
# Pull times and hashes out of the saved_cache and stick them into
# the fswatcher, so we pick up the changes.
for state in self.fine_grained_manager.graph.values():
meta = state.meta
if meta is None: continue
assert state.path is not None
self.fswatcher.set_file_data(
state.path,
FileData(st_mtime=float(meta.mtime), st_size=meta.size, md5=meta.hash))
changed, removed = self.find_changed(sources)
# Find anything that has had its dependency list change
for state in self.fine_grained_manager.graph.values():
if not state.is_fresh():
assert state.path is not None
changed.append((state.id, state.path))
t3 = time.time()
# Run an update
messages = self.fine_grained_manager.update(changed, removed)
t4 = time.time()
self.fine_grained_manager.manager.add_stats(
update_sources_time=t1 - t0,
build_time=t2 - t1,
find_changes_time=t3 - t2,
fg_update_time=t4 - t3,
files_changed=len(removed) + len(changed))
else:
# Stores the initial state of sources as a side effect.
self.fswatcher.find_changed()
if MEM_PROFILE:
from mypy.memprofile import print_memory_profile
print_memory_profile(run_gc=False)
status = 1 if messages else 0
return {'out': ''.join(s + '\n' for s in messages), 'err': '', 'status': status}
def fine_grained_increment(self,
sources: List[BuildSource],
remove: Optional[List[str]] = None,
update: Optional[List[str]] = None,
) -> Dict[str, Any]:
assert self.fine_grained_manager is not None
manager = self.fine_grained_manager.manager
t0 = time.time()
if remove is None and update is None:
# Use the fswatcher to determine which files were changed
# (updated or added) or removed.
self.update_sources(sources)
changed, removed = self.find_changed(sources)
else:
# Use the remove/update lists to update fswatcher.
# This avoids calling stat() for unchanged files.
changed, removed = self.update_changed(sources, remove or [], update or [])
manager.search_paths = compute_search_paths(sources, manager.options, manager.data_dir)
t1 = time.time()
manager.log("fine-grained increment: find_changed: {:.3f}s".format(t1 - t0))
messages = self.fine_grained_manager.update(changed, removed)
t2 = time.time()
manager.log("fine-grained increment: update: {:.3f}s".format(t2 - t1))
manager.add_stats(
find_changes_time=t1 - t0,
fg_update_time=t2 - t1,
files_changed=len(removed) + len(changed))
status = 1 if messages else 0
self.previous_sources = sources
return {'out': ''.join(s + '\n' for s in messages), 'err': '', 'status': status}
def update_sources(self, sources: List[BuildSource]) -> None:
paths = [source.path for source in sources if source.path is not None]
self.fswatcher.add_watched_paths(paths)
def update_changed(self,
sources: List[BuildSource],
remove: List[str],
update: List[str],
) -> ChangesAndRemovals:
changed_paths = self.fswatcher.update_changed(remove, update)
return self._find_changed(sources, changed_paths)
def find_changed(self, sources: List[BuildSource]) -> ChangesAndRemovals:
changed_paths = self.fswatcher.find_changed()
return self._find_changed(sources, changed_paths)
def _find_changed(self, sources: List[BuildSource],
changed_paths: AbstractSet[str]) -> ChangesAndRemovals:
# Find anything that has been added or modified
changed = [(source.module, source.path)
for source in sources
if source.path and source.path in changed_paths]
# Now find anything that has been removed from the build
modules = {source.module for source in sources}
omitted = [source for source in self.previous_sources if source.module not in modules]
removed = []
for source in omitted:
path = source.path
assert path
removed.append((source.module, path))
# Find anything that has had its module path change because of added or removed __init__s
last = {s.path: s.module for s in self.previous_sources}
for s in sources:
assert s.path
if s.path in last and last[s.path] != s.module:
# Mark it as removed from its old name and changed at its new name
removed.append((last[s.path], s.path))
changed.append((s.module, s.path))
return changed, removed
def cmd_suggest(self, function: str, json: bool, callsites: bool) -> Dict[str, object]:
"""Suggest a signature for a function."""
if not self.fine_grained_manager:
return {'error': "Command 'suggest' is only valid after a 'check' command"}
engine = SuggestionEngine(self.fine_grained_manager)
try:
if callsites:
out = engine.suggest_callsites(function)
else:
out = engine.suggest(function, json)
except SuggestionFailure as err:
return {'error': str(err)}
else:
if not out:
out = "No suggestions\n"
elif not out.endswith("\n"):
out += "\n"
return {'out': out, 'err': "", 'status': 0}
def cmd_hang(self) -> Dict[str, object]:
"""Hang for 100 seconds, as a debug hack."""
time.sleep(100)
return {}
# Misc utilities.
MiB = 2**20 # type: Final
def get_meminfo() -> Dict[str, Any]:
res = {} # type: Dict[str, Any]
try:
import psutil # type: ignore # It's not in typeshed yet
except ImportError:
if sys.platform != 'win32':
res['memory_psutil_missing'] = (
'psutil not found, run pip install mypy[dmypy] '
'to install the needed components for dmypy'
)
else:
process = psutil.Process(os.getpid())
meminfo = process.memory_info()
res['memory_rss_mib'] = meminfo.rss / MiB
res['memory_vms_mib'] = meminfo.vms / MiB
if sys.platform == 'win32':
res['memory_maxrss_mib'] = meminfo.peak_wset / MiB
else:
# See https://stackoverflow.com/questions/938733/total-memory-used-by-python-process
import resource # Since it doesn't exist on Windows.
rusage = resource.getrusage(resource.RUSAGE_SELF)
if sys.platform == 'darwin':
factor = 1
else:
factor = 1024 # Linux
res['memory_maxrss_mib'] = rusage.ru_maxrss * factor / MiB
return res