2020import mypy .build
2121import mypy .errors
2222import mypy .main
23- import mypy .server .update
23+ from mypy .server .update import FineGrainedBuildManager
2424from mypy .dmypy_util import STATUS_FILE , receive
2525from mypy .gclogger import GcLogger
2626from mypy .fscache import FileSystemCache
2727from mypy .fswatcher import FileSystemWatcher , FileData
28+ from mypy .options import Options
2829
2930
3031def 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:
7879SOCKET_NAME = 'dmypy.sock' # In current directory.
7980
8081
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+
81107class Server :
82108
83109 # NOTE: the instance is constructed in the parent process but
84110 # serve() is called in the grandchild (by daemonize()).
85111
86- def __init__ (self , flags : List [str ]) -> None :
112+ def __init__ (self , options : Options , alt_lib_path : Optional [str ] = None ) -> None :
87113 """Initialize the server with the desired mypy flags."""
88114 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 )
93115 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" )
107116 self .options = options
117+ self .alt_lib_path = alt_lib_path
118+ self .fine_grained_manager = None # type: Optional[FineGrainedBuildManager]
119+
108120 if os .path .isfile (STATUS_FILE ):
109121 os .unlink (STATUS_FILE )
110122 if self .fine_grained :
@@ -214,15 +226,13 @@ def cmd_recheck(self) -> Dict[str, object]:
214226 # Needed by tests.
215227 last_manager = None # type: Optional[mypy.build.BuildManager]
216228
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 ]:
219230 if self .fine_grained :
220231 return self .check_fine_grained (sources )
221232 else :
222- return self .check_default (sources , alt_lib_path )
233+ return self .check_default (sources )
223234
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 ]:
226236 """Check using the default (per-file) incremental mode."""
227237 self .last_manager = None
228238 blockers = False
@@ -231,7 +241,7 @@ def check_default(self, sources: List[mypy.build.BuildSource],
231241 # saved_cache is mutated in place.
232242 res = mypy .build .build (sources , self .options ,
233243 saved_cache = self .saved_cache ,
234- alt_lib_path = alt_lib_path )
244+ alt_lib_path = self . alt_lib_path )
235245 msgs = res .errors
236246 self .last_manager = res .manager # type: Optional[mypy.build.BuildManager]
237247 except mypy .errors .CompileError as err :
@@ -254,7 +264,7 @@ def check_default(self, sources: List[mypy.build.BuildSource],
254264
255265 def check_fine_grained (self , sources : List [mypy .build .BuildSource ]) -> Dict [str , Any ]:
256266 """Check using fine-grained incremental mode."""
257- if not self .fine_grained_initialized :
267+ if not self .fine_grained_manager :
258268 return self .initialize_fine_grained (sources )
259269 else :
260270 return self .fine_grained_increment (sources )
@@ -267,9 +277,9 @@ def initialize_fine_grained(self, sources: List[mypy.build.BuildSource]) -> Dict
267277 # Stores the initial state of sources as a side effect.
268278 self .fswatcher .find_changed ()
269279 try :
270- # TODO: alt_lib_path
271280 result = mypy .build .build (sources = sources ,
272- options = self .options )
281+ options = self .options ,
282+ alt_lib_path = self .alt_lib_path )
273283 except mypy .errors .CompileError as e :
274284 output = '' .join (s + '\n ' for s in e .messages )
275285 if e .use_stdout :
@@ -280,8 +290,7 @@ def initialize_fine_grained(self, sources: List[mypy.build.BuildSource]) -> Dict
280290 messages = result .errors
281291 manager = result .manager
282292 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 )
285294 self .previous_sources = sources
286295 self .fscache .flush ()
287296
@@ -310,6 +319,8 @@ def initialize_fine_grained(self, sources: List[mypy.build.BuildSource]) -> Dict
310319 return {'out' : '' .join (s + '\n ' for s in messages ), 'err' : '' , 'status' : status }
311320
312321 def fine_grained_increment (self , sources : List [mypy .build .BuildSource ]) -> Dict [str , Any ]:
322+ assert self .fine_grained_manager is not None
323+
313324 t0 = time .time ()
314325 self .update_sources (sources )
315326 changed = self .find_changed (sources )
0 commit comments