def _make_manager(self) -> BuildManager: errors = Errors() options = Options() fscache = FileSystemCache() manager = BuildManager( data_dir='', lib_path=[], ignore_prefix='', source_set=BuildSourceSet([]), reports=Reports('', {}), options=options, version_id=__version__, plugin=Plugin(options), errors=errors, flush_errors=lambda msgs, serious: None, fscache=fscache, ) return manager
def _make_manager(self) -> BuildManager: errors = Errors() options = Options() fscache = FileSystemCache() search_paths = SearchPaths((), (), (), ()) manager = BuildManager( data_dir='', search_paths=search_paths, ignore_prefix='', source_set=BuildSourceSet([]), reports=Reports('', {}), options=options, version_id=__version__, plugin=Plugin(options), plugins_snapshot={}, errors=errors, flush_errors=lambda msgs, serious: None, fscache=fscache, stdout=sys.stdout, stderr=sys.stderr, ) return manager
def build(sources: List[BuildSource], target: int, alt_lib_path: str = None, bin_dir: str = None, pyversion: Tuple[int, int] = defaults.PYTHON3_VERSION, custom_typing_module: str = None, report_dirs: Dict[str, str] = {}, flags: List[str] = None, python_path: bool = False) -> BuildResult: """Analyze a program. A single call to build performs parsing, semantic analysis and optionally type checking for the program *and* all imported modules, recursively. Return BuildResult if successful; otherwise raise CompileError. Args: target: select passes to perform (a build target constant, e.g. C) sources: list of sources to build alt_lib_dir: an additional directory for looking up library modules (takes precedence over other directories) bin_dir: directory containing the mypy script, used for finding data directories; if omitted, use '.' as the data directory pyversion: Python version (major, minor) custom_typing_module: if not None, use this module id as an alias for typing flags: list of build options (e.g. COMPILE_ONLY) """ flags = flags or [] data_dir = default_data_dir(bin_dir) # Determine the default module search path. lib_path = default_lib_path(data_dir, pyversion, python_path) if TEST_BUILTINS in flags: # Use stub builtins (to speed up test cases and to make them easier to # debug). lib_path.insert(0, os.path.join(os.path.dirname(__file__), 'test', 'data', 'lib-stub')) else: for source in sources: if source.path: # Include directory of the program file in the module search path. lib_path.insert( 0, remove_cwd_prefix_from_path(dirname(source.path))) # Do this even if running as a file, for sanity (mainly because with # multiple builds, there could be a mix of files/modules, so its easier # to just define the semantics that we always add the current director # to the lib_path lib_path.insert(0, os.getcwd()) # Add MYPYPATH environment variable to front of library path, if defined. lib_path[:0] = mypy_path() # If provided, insert the caller-supplied extra module path to the # beginning (highest priority) of the search path. if alt_lib_path: lib_path.insert(0, alt_lib_path) # TODO Reports is global to a build manager but only supports a single "main file" # Fix this. reports = Reports(sources[0].effective_path, data_dir, report_dirs) # Construct a build manager object that performs all the stages of the # build in the correct order. # # Ignore current directory prefix in error messages. manager = BuildManager(data_dir, lib_path, target, pyversion=pyversion, flags=flags, ignore_prefix=os.getcwd(), custom_typing_module=custom_typing_module, reports=reports) # Construct information that describes the initial files. __main__ is the # implicit module id and the import context is empty initially ([]). initial_states = [] # type: List[UnprocessedFile] for source in sources: content = source.load(lib_path, pyversion) info = StateInfo(source.effective_path, source.module, [], manager) initial_state = UnprocessedFile(info, content) initial_states += [initial_state] # Perform the build by sending the files as new file (UnprocessedFile is the # initial state of all files) to the manager. The manager will process the # file and all dependant modules recursively. result = manager.process(initial_states) reports.finish() return result
def build(sources: List[BuildSource], target: int, alt_lib_path: str = None, bin_dir: str = None, pyversion: Tuple[int, int] = defaults.PYTHON3_VERSION, custom_typing_module: str = None, report_dirs: Dict[str, str] = {}, flags: List[str] = None, python_path: bool = False) -> BuildResult: """Analyze a program. A single call to build performs parsing, semantic analysis and optionally type checking for the program *and* all imported modules, recursively. Return BuildResult if successful; otherwise raise CompileError. Args: target: select passes to perform (a build target constant, e.g. C) sources: list of sources to build alt_lib_dir: an additional directory for looking up library modules (takes precedence over other directories) bin_dir: directory containing the mypy script, used for finding data directories; if omitted, use '.' as the data directory pyversion: Python version (major, minor) custom_typing_module: if not None, use this module id as an alias for typing flags: list of build options (e.g. COMPILE_ONLY) """ flags = flags or [] data_dir = default_data_dir(bin_dir) # Determine the default module search path. lib_path = default_lib_path(data_dir, pyversion, python_path) if TEST_BUILTINS in flags: # Use stub builtins (to speed up test cases and to make them easier to # debug). lib_path.insert(0, os.path.join(os.path.dirname(__file__), 'test', 'data', 'lib-stub')) else: for source in sources: if source.path: # Include directory of the program file in the module search path. lib_path.insert( 0, remove_cwd_prefix_from_path(dirname(source.path))) # Do this even if running as a file, for sanity (mainly because with # multiple builds, there could be a mix of files/modules, so its easier # to just define the semantics that we always add the current director # to the lib_path lib_path.insert(0, os.getcwd()) # If provided, insert the caller-supplied extra module path to the # beginning (highest priority) of the search path. if alt_lib_path: lib_path.insert(0, alt_lib_path) # TODO Reports is global to a build manager but only supports a single "main file" # Fix this. reports = Reports(sources[0].effective_path, data_dir, report_dirs) # Construct a build manager object that performs all the stages of the # build in the correct order. # # Ignore current directory prefix in error messages. manager = BuildManager(data_dir, lib_path, target, pyversion=pyversion, flags=flags, ignore_prefix=os.getcwd(), custom_typing_module=custom_typing_module, reports=reports) # Construct information that describes the initial files. __main__ is the # implicit module id and the import context is empty initially ([]). initial_states = [] # type: List[UnprocessedFile] for source in sources: content = source.load(lib_path) info = StateInfo(source.effective_path, source.module, [], manager) initial_state = UnprocessedFile(info, content) initial_states += [initial_state] # Perform the build by sending the files as new file (UnprocessedFile is the # initial state of all files) to the manager. The manager will process the # file and all dependant modules recursively. result = manager.process(initial_states) reports.finish() return result
def build(program_path: str, target: int, module: str = None, argument: str = None, program_text: Union[str, bytes] = None, alt_lib_path: str = None, bin_dir: str = None, pyversion: Tuple[int, int] = defaults.PYTHON3_VERSION, custom_typing_module: str = None, report_dirs: Dict[str, str] = {}, flags: List[str] = None, python_path: bool = False) -> BuildResult: """Analyze a program. A single call to build performs parsing, semantic analysis and optionally type checking for the program *and* all imported modules, recursively. Return BuildResult if successful; otherwise raise CompileError. Args: program_path: the path to the main source file (if module argument is given, this can be None => will be looked up) target: select passes to perform (a build target constant, e.g. C) module: name of the initial module; __main__ by default program_text: the main source file contents; if omitted, read from file alt_lib_dir: an additional directory for looking up library modules (takes precedence over other directories) bin_dir: directory containing the mypy script, used for finding data directories; if omitted, use '.' as the data directory pyversion: Python version (major, minor) custom_typing_module: if not None, use this module id as an alias for typing flags: list of build options (e.g. COMPILE_ONLY) """ flags = flags or [] module = module or '__main__' data_dir = default_data_dir(bin_dir) # Determine the default module search path. lib_path = default_lib_path(data_dir, target, pyversion, python_path) if TEST_BUILTINS in flags: # Use stub builtins (to speed up test cases and to make them easier to # debug). lib_path.insert( 0, os.path.join(os.path.dirname(__file__), 'test', 'data', 'lib-stub')) elif program_path: # Include directory of the program file in the module search path. lib_path.insert(0, remove_cwd_prefix_from_path(dirname(program_path))) else: # Building/running a module. lib_path.insert(0, os.getcwd()) # If provided, insert the caller-supplied extra module path to the # beginning (highest priority) of the search path. if alt_lib_path: lib_path.insert(0, alt_lib_path) if program_text is None: program_path = program_path or lookup_program(module, lib_path) program_text = read_program(program_path) else: program_path = program_path or '<string>' reports = Reports(program_path, data_dir, report_dirs) # Construct a build manager object that performs all the stages of the # build in the correct order. # # Ignore current directory prefix in error messages. manager = BuildManager(data_dir, lib_path, target, pyversion=pyversion, flags=flags, ignore_prefix=os.getcwd(), custom_typing_module=custom_typing_module, reports=reports) # Construct information that describes the initial file. __main__ is the # implicit module id and the import context is empty initially ([]). info = StateInfo(program_path, module, [], manager) # Perform the build by sending the file as new file (UnprocessedFile is the # initial state of all files) to the manager. The manager will process the # file and all dependant modules recursively. result = manager.process(UnprocessedFile(info, program_text)) reports.finish() return result
def build(program_path: str, target: int, module: str = None, argument: str = None, program_text: Union[str, bytes] = None, alt_lib_path: str = None, bin_dir: str = None, pyversion: Tuple[int, int] = defaults.PYTHON3_VERSION, custom_typing_module: str = None, report_dirs: Dict[str, str] = {}, flags: List[str] = None, python_path: bool = False) -> BuildResult: """Analyze a program. A single call to build performs parsing, semantic analysis and optionally type checking for the program *and* all imported modules, recursively. Return BuildResult if successful; otherwise raise CompileError. Args: program_path: the path to the main source file (if module argument is given, this can be None => will be looked up) target: select passes to perform (a build target constant, e.g. C) module: name of the initial module; __main__ by default program_text: the main source file contents; if omitted, read from file alt_lib_dir: an additional directory for looking up library modules (takes precedence over other directories) bin_dir: directory containing the mypy script, used for finding data directories; if omitted, use '.' as the data directory pyversion: Python version (major, minor) custom_typing_module: if not None, use this module id as an alias for typing flags: list of build options (e.g. COMPILE_ONLY) """ flags = flags or [] module = module or '__main__' data_dir = default_data_dir(bin_dir) # Determine the default module search path. lib_path = default_lib_path(data_dir, target, pyversion, python_path) if TEST_BUILTINS in flags: # Use stub builtins (to speed up test cases and to make them easier to # debug). lib_path.insert(0, os.path.join(os.path.dirname(__file__), 'test', 'data', 'lib-stub')) elif program_path: # Include directory of the program file in the module search path. lib_path.insert( 0, remove_cwd_prefix_from_path(dirname(program_path))) else: # Building/running a module. lib_path.insert(0, os.getcwd()) # If provided, insert the caller-supplied extra module path to the # beginning (highest priority) of the search path. if alt_lib_path: lib_path.insert(0, alt_lib_path) if program_text is None: program_path = program_path or lookup_program(module, lib_path) program_text = read_program(program_path) else: program_path = program_path or '<string>' reports = Reports(program_path, data_dir, report_dirs) # Construct a build manager object that performs all the stages of the # build in the correct order. # # Ignore current directory prefix in error messages. manager = BuildManager(data_dir, lib_path, target, pyversion=pyversion, flags=flags, ignore_prefix=os.getcwd(), custom_typing_module=custom_typing_module, reports=reports) # Construct information that describes the initial file. __main__ is the # implicit module id and the import context is empty initially ([]). info = StateInfo(program_path, module, [], manager) # Perform the build by sending the file as new file (UnprocessedFile is the # initial state of all files) to the manager. The manager will process the # file and all dependant modules recursively. result = manager.process(UnprocessedFile(info, program_text)) reports.finish() return result