Example #1
0
    def compute_deps(
        self,
        python_path,
        pkg,
        rule_type,
        name,
        srcs,
        stub_srcs,
        main,
        pip_main,
        validate,
        is_py3_compatible,
    ):
        srcs = (srcs or []) + (stub_srcs or [])
        if main:
            srcs = srcs + [main]

        mapping = self.python_path_mappings.get(python_path)
        self_modules = mapping.compute_self_modules(pkg, srcs)

        target_dir = bazel_utils.normalize_relative_target_to_os_path(pkg[2:])

        all_deps = set()  # type: ignore[var-annotated]
        all_unknown_imports = set()  # type: ignore[var-annotated]
        all_unknown_froms = set()  # type: ignore[var-annotated]

        for src in set(srcs):
            src = os.path.join(target_dir, src)

            module_path = PythonPathMapping.convert_from_file_path_to_module(
                src)

            filename, parsed = mapping.find_closest_bzl_or_build(module_path)
            if not filename:
                raise bazel_utils.BazelError(
                    "Cannot locate %s:%s's source (or its closest BUILD / "
                    "BUILD.in file): %s/%s" % (pkg, name, target_dir, src))

            pkg_path = os.path.dirname(filename).replace(
                self.workspace_dir, "/")
            src_pkg = bazel_utils.normalize_os_path_to_target(pkg_path)

            if src_pkg != pkg:
                print(("WARNING: Skipping %s from %s:%s deps computation "
                       "since it belongs to %s") % (src, pkg, name, src_pkg))
                continue

            import_set, from_set = parse_imports(
                self.workspace_dir,
                src,
                py3_compatible=is_py3_compatible or src.endswith(".pyi"),
            )

            import_deps, unknown_imports = mapping.find_import_targets(
                src_pkg, self_modules, import_set)
            all_deps.update(import_deps)
            all_unknown_imports.update(unknown_imports)

            if validate:
                assert not unknown_imports, (
                    "Unable to locate modules %s (imported by %s) in any "
                    "library target (NOTE: bin and test targets are "
                    "ignored)") % (unknown_imports, src)

            from_deps, unknown_froms = mapping.find_from_targets(
                src_pkg, self_modules, from_set)
            all_deps.update(from_deps)
            all_unknown_froms.update(unknown_froms)

            if validate:
                assert not unknown_froms, (
                    "Unable to locate modules %s (imported by %s) in any "
                    "library target (NOTE: bin and test targets are "
                    "ignored)") % (unknown_froms, src)

        import_deps, unknown_imports = mapping.find_import_targets(
            pkg, self_modules, [])
        all_deps.update(import_deps)
        all_unknown_imports.update(unknown_imports)

        if pip_main:
            all_deps.add(pip_main)

        all_deps.discard("%s:%s" % (pkg, name))
        if name == os.path.basename(target_dir):
            all_deps.discard("%s" % pkg)

        return sort_deps(pkg, all_deps), all_unknown_imports, all_unknown_froms
Example #2
0
def main():
    workspace_dir, src = sys.argv[1:]
    import_set, from_set = parse_imports(workspace_dir, src)
    json_dict = {"import_set": list(import_set), "from_set": list(from_set)}
    print(json.dumps(json_dict))