def get_module_name(filename, pythonpath): """Get the module name, or None if we can't determine it.""" if filename: filename = os.path.normpath(filename) # Keep path '' as is; infer_module will handle it. pythonpath = [path and os.path.normpath(path) for path in pythonpath] return module_utils.infer_module(filename, pythonpath).name
def yield_sorted_modules(self): """Yield modules from our sorted source files.""" for files in self.sorted_source_files: modules = [] for f in files: # Report errors for files we are analysing directly. report_errors = f in self.filenames # We'll use this function to report skipped files. report = logging.warning if report_errors else logging.info if not f.endswith('.py'): report('Skipping non-Python file: %s', f) continue module = module_utils.infer_module(f, self.pythonpath, preserve_init=True) if not any(module.path.startswith(d) for d in self.pythonpath): report('Skipping file not in pythonpath: %s', f) continue modules.append((module, report_errors)) if len(modules) == 1: yield modules[0] else: # If we have a cycle we run pytype over the files twice, ignoring errors # the first time so that we don't fail on missing dependencies. for module, _ in modules: yield module, False for module_and_report_errors in modules: yield module_and_report_errors
def test_not_found(self): mod = module_utils.infer_module(expand("bar/baz.py"), ["foo"]) expected_target = expand("bar/baz.py") expected_name, _ = os.path.splitext( expected_target.replace(os.sep, ".")) self.assert_module_equal(mod, "", expected_target, expected_name)
def test_multiple_paths(self): pythonpath = [expand("foo"), expand("bar/baz"), expand("bar")] mod = module_utils.infer_module(expand("bar/baz/qux.py"), pythonpath) self.assert_module_equal(mod, expand("bar/baz"), "qux.py", "qux") mod = module_utils.infer_module(expand("bar/qux.py"), pythonpath) self.assert_module_equal(mod, expand("bar"), "qux.py", "qux")
def test_name_in_package(self): mod = module_utils.infer_module(expand("foo/bar/baz.py"), [expand("foo")]) self.assert_module_equal(mod, expand("foo"), "bar/baz.py", "bar.baz")
def test_simple_name(self): mod = module_utils.infer_module(expand("foo/bar.py"), [expand("foo")]) self.assert_module_equal(mod, expand("foo"), "bar.py", "bar")