20
20
import mypy .build
21
21
import mypy .errors
22
22
import mypy .main
23
- import mypy .server .update
23
+ from mypy .server .update import FineGrainedBuildManager
24
24
from mypy .dmypy_util import STATUS_FILE , receive
25
25
from mypy .gclogger import GcLogger
26
26
from mypy .fscache import FileSystemCache
27
27
from mypy .fswatcher import FileSystemWatcher , FileData
28
+ from mypy .options import Options
28
29
29
30
30
31
def daemonize (func : Callable [[], None ], log_file : Optional [str ] = None ) -> int :
@@ -78,33 +79,44 @@ def daemonize(func: Callable[[], None], log_file: Optional[str] = None) -> int:
78
79
SOCKET_NAME = 'dmypy.sock' # In current directory.
79
80
80
81
82
+ def process_start_options (flags : List [str ]) -> Options :
83
+ import mypy .main
84
+ sources , options = mypy .main .process_options (['-i' ] + flags ,
85
+ require_targets = False ,
86
+ server_options = True )
87
+ if sources :
88
+ sys .exit ("dmypy: start/restart does not accept sources" )
89
+ if options .report_dirs :
90
+ sys .exit ("dmypy: start/restart cannot generate reports" )
91
+ if options .junit_xml :
92
+ sys .exit ("dmypy: start/restart does not support --junit-xml; "
93
+ "pass it to check/recheck instead" )
94
+ if not options .incremental :
95
+ sys .exit ("dmypy: start/restart should not disable incremental mode" )
96
+ if options .quick_and_dirty :
97
+ sys .exit ("dmypy: start/restart should not specify quick_and_dirty mode" )
98
+ if options .use_fine_grained_cache and not options .fine_grained_incremental :
99
+ sys .exit ("dmypy: fine-grained cache can only be used in experimental mode" )
100
+ # Our file change tracking can't yet handle changes to files that aren't
101
+ # specified in the sources list.
102
+ if options .follow_imports not in ('skip' , 'error' ):
103
+ sys .exit ("dmypy: follow-imports must be 'skip' or 'error'" )
104
+ return options
105
+
106
+
81
107
class Server :
82
108
83
109
# NOTE: the instance is constructed in the parent process but
84
110
# serve() is called in the grandchild (by daemonize()).
85
111
86
- def __init__ (self , flags : List [str ]) -> None :
112
+ def __init__ (self , options : Options , alt_lib_path : Optional [str ] = None ) -> None :
87
113
"""Initialize the server with the desired mypy flags."""
88
114
self .saved_cache = {} # type: mypy.build.SavedCache
89
- self .fine_grained_initialized = False
90
- sources , options = mypy .main .process_options (['-i' ] + flags ,
91
- require_targets = False ,
92
- server_options = True )
93
115
self .fine_grained = options .fine_grained_incremental
94
- if sources :
95
- sys .exit ("dmypy: start/restart does not accept sources" )
96
- if options .report_dirs :
97
- sys .exit ("dmypy: start/restart cannot generate reports" )
98
- if options .junit_xml :
99
- sys .exit ("dmypy: start/restart does not support --junit-xml; "
100
- "pass it to check/recheck instead" )
101
- if not options .incremental :
102
- sys .exit ("dmypy: start/restart should not disable incremental mode" )
103
- if options .quick_and_dirty :
104
- sys .exit ("dmypy: start/restart should not specify quick_and_dirty mode" )
105
- if options .use_fine_grained_cache and not options .fine_grained_incremental :
106
- sys .exit ("dmypy: fine-grained cache can only be used in experimental mode" )
107
116
self .options = options
117
+ self .alt_lib_path = alt_lib_path
118
+ self .fine_grained_manager = None # type: Optional[FineGrainedBuildManager]
119
+
108
120
if os .path .isfile (STATUS_FILE ):
109
121
os .unlink (STATUS_FILE )
110
122
if self .fine_grained :
@@ -214,15 +226,13 @@ def cmd_recheck(self) -> Dict[str, object]:
214
226
# Needed by tests.
215
227
last_manager = None # type: Optional[mypy.build.BuildManager]
216
228
217
- def check (self , sources : List [mypy .build .BuildSource ],
218
- alt_lib_path : Optional [str ] = None ) -> Dict [str , Any ]:
229
+ def check (self , sources : List [mypy .build .BuildSource ]) -> Dict [str , Any ]:
219
230
if self .fine_grained :
220
231
return self .check_fine_grained (sources )
221
232
else :
222
- return self .check_default (sources , alt_lib_path )
233
+ return self .check_default (sources )
223
234
224
- def check_default (self , sources : List [mypy .build .BuildSource ],
225
- alt_lib_path : Optional [str ] = None ) -> Dict [str , Any ]:
235
+ def check_default (self , sources : List [mypy .build .BuildSource ]) -> Dict [str , Any ]:
226
236
"""Check using the default (per-file) incremental mode."""
227
237
self .last_manager = None
228
238
blockers = False
@@ -231,7 +241,7 @@ def check_default(self, sources: List[mypy.build.BuildSource],
231
241
# saved_cache is mutated in place.
232
242
res = mypy .build .build (sources , self .options ,
233
243
saved_cache = self .saved_cache ,
234
- alt_lib_path = alt_lib_path )
244
+ alt_lib_path = self . alt_lib_path )
235
245
msgs = res .errors
236
246
self .last_manager = res .manager # type: Optional[mypy.build.BuildManager]
237
247
except mypy .errors .CompileError as err :
@@ -254,7 +264,7 @@ def check_default(self, sources: List[mypy.build.BuildSource],
254
264
255
265
def check_fine_grained (self , sources : List [mypy .build .BuildSource ]) -> Dict [str , Any ]:
256
266
"""Check using fine-grained incremental mode."""
257
- if not self .fine_grained_initialized :
267
+ if not self .fine_grained_manager :
258
268
return self .initialize_fine_grained (sources )
259
269
else :
260
270
return self .fine_grained_increment (sources )
@@ -267,9 +277,9 @@ def initialize_fine_grained(self, sources: List[mypy.build.BuildSource]) -> Dict
267
277
# Stores the initial state of sources as a side effect.
268
278
self .fswatcher .find_changed ()
269
279
try :
270
- # TODO: alt_lib_path
271
280
result = mypy .build .build (sources = sources ,
272
- options = self .options )
281
+ options = self .options ,
282
+ alt_lib_path = self .alt_lib_path )
273
283
except mypy .errors .CompileError as e :
274
284
output = '' .join (s + '\n ' for s in e .messages )
275
285
if e .use_stdout :
@@ -280,8 +290,7 @@ def initialize_fine_grained(self, sources: List[mypy.build.BuildSource]) -> Dict
280
290
messages = result .errors
281
291
manager = result .manager
282
292
graph = result .graph
283
- self .fine_grained_manager = mypy .server .update .FineGrainedBuildManager (manager , graph )
284
- self .fine_grained_initialized = True
293
+ self .fine_grained_manager = FineGrainedBuildManager (manager , graph )
285
294
self .previous_sources = sources
286
295
self .fscache .flush ()
287
296
@@ -310,6 +319,8 @@ def initialize_fine_grained(self, sources: List[mypy.build.BuildSource]) -> Dict
310
319
return {'out' : '' .join (s + '\n ' for s in messages ), 'err' : '' , 'status' : status }
311
320
312
321
def fine_grained_increment (self , sources : List [mypy .build .BuildSource ]) -> Dict [str , Any ]:
322
+ assert self .fine_grained_manager is not None
323
+
313
324
t0 = time .time ()
314
325
self .update_sources (sources )
315
326
changed = self .find_changed (sources )
0 commit comments