Esempio n. 1
0
    def get_bzl(self, directory):
        if not directory.endswith(os.path.sep):
            directory += os.path.sep

        if directory in self.parsed_bzls:
            return self.parsed_bzls[directory]

        if directory in self.empty_bzl_dirs:
            return "", None

        bzl_file = os.path.join(directory, BUILD_INPUT)
        assert directory.startswith(
            self.workspace_dir
        ), "Trying to read {} outside of workspace: {}".format(
            BUILD_INPUT, directory)

        if not os.path.isfile(bzl_file):
            self.empty_bzl_dirs.add(directory)
            return "", None

        real_bzl_file = os.path.realpath(bzl_file)

        if bzl_file != real_bzl_file:  # symlink
            _, entry = self.get_bzl(os.path.dirname(real_bzl_file))
        else:
            entry = build_parser.parse_file(real_bzl_file)

        if entry:
            self.parsed_bzls[directory] = (real_bzl_file, entry)
        else:
            self.empty_bzl_dirs.add(directory)

        return real_bzl_file, entry
Esempio n. 2
0
    def get_bzl(self, directory):
        if not directory.endswith("/"):
            directory += "/"

        if directory in self.parsed_bzls:
            return self.parsed_bzls[directory]

        if directory in self.empty_bzl_dirs:
            return "", None

        bzl_file = os.path.join(directory, "BUILD.in")
        assert directory.startswith(self.workspace_dir), (
            "Trying to read BUILD.in outside of workspace: " + directory)

        if not os.path.isfile(bzl_file):
            self.empty_bzl_dirs.add(directory)
            return "", None

        real_bzl_file = os.path.realpath(bzl_file)

        if bzl_file != real_bzl_file:  # symlink
            _, entry = self.get_bzl(os.path.dirname(real_bzl_file))
        else:
            entry = build_parser.parse_file(real_bzl_file)

        if entry:
            self.parsed_bzls[directory] = (real_bzl_file, entry)
        else:
            self.empty_bzl_dirs.add(directory)

        return real_bzl_file, entry
Esempio n. 3
0
    def get_build(self, directory):
        if directory in self.parsed_builds:
            return self.parsed_builds[directory]

        if directory in self.empty_build_dirs:
            return "", None

        build_file = os.path.join(directory, DEFAULT_BUILD)
        assert directory.startswith(
            self.workspace_dir
        ), "Trying to read {} outside of workspace: {}".format(
            DEFAULT_BUILD, directory)

        if not os.path.isfile(build_file):
            build_file = os.path.join(directory, ALT_BUILD)
            if not os.path.isfile(build_file):
                self.empty_build_dirs.add(directory)
                return "", None

        real_build_file = os.path.realpath(build_file)

        if build_file != real_build_file:  # symlink
            _, entry = self.get_build(os.path.dirname(real_build_file))
        else:
            entry = build_parser.parse_file(real_build_file)

        if entry:
            self.parsed_builds[directory] = (real_build_file, entry)
        else:
            self.empty_build_dirs.add(directory)

        return real_build_file, entry
Esempio n. 4
0
    def regenerate(self, bazel_targets: Iterable[str], cwd: str = ".") -> None:
        targets = bazel_utils.expand_bazel_target_dirs(
            self.workspace_dir,
            [t for t in bazel_targets if not t.startswith("@")],
            require_build_file=False,
            cwd=cwd,
        )

        for target in targets:
            assert target.startswith(
                "//"), "Target must be absolute: " + target
            target_dir = bazel_utils.normalize_relative_target_to_os_path(
                target[2:])

            if target_dir in self.visited_dirs:
                continue
            self.visited_dirs.add(target_dir)

            build_bzl = os.path.join(self.workspace_dir, target_dir,
                                     BUILD_INPUT)
            if not os.path.isfile(build_bzl):
                continue

            parsed = build_parser.parse_file(build_bzl)

            pip_rules = parsed.get_rules_by_types(PIP_GEN_RULE_TYPES)
            if not pip_rules:
                if self.verbose:
                    print("No pip targets found in %s/%s" %
                          (target_dir, BUILD_INPUT))
                continue

            if not self.skip_deps_generation:
                for rule in pip_rules:
                    self.regenerate(
                        build_parser.maybe_expand_attribute(
                            rule.attr_map.get("deps", [])),
                        cwd=os.path.join(self.workspace_dir, target_dir),
                    )

            if self.verbose:
                head = "(dry run) " if self.dry_run else ""
                print(
                    head + "Processing pip targets in %s: %s" %
                    (target_dir, [rule.attr_map["name"]
                                  for rule in pip_rules]))

            if self.dry_run:
                continue

            self.process_pip_rules(target_dir, pip_rules)
Esempio n. 5
0
    def regenerate(self, bazel_targets: Iterable[str]) -> None:
        go_packages = targets2packages(bazel_targets)
        go_packages = [
            p for p in go_packages
            if os.path.join(self.go_src_dir, p) not in self.visited_dirs
        ]

        if not go_packages:
            return

        tool_path = runfiles.data_path(
            "@dbx_build_tools//go/src/dropbox/build_tools/gen-build-go/gen-build-go"
        )

        args = [tool_path]
        if self.verbose:
            args += ["--verbose"]
        if self.skip_deps_generation:
            args += ["--skip-deps-generation"]
        if self.dry_run:
            args += ["--dry-run"]

        # Write out some temporary BUILD files that we can merge with
        # exisiting ones.
        tmp_buildfile = "BUILD.gen-build-go~"
        args += ["--build-filename", tmp_buildfile]
        args += go_packages
        output = run_cmd(args, use_go_env=True, verbose=self.verbose)

        if self.dry_run or self.verbose:
            print(output)

        if self.dry_run:
            return

        for line in output.split("\n"):
            line = line.strip()
            if not line:
                continue

            chunks = line.split()
            if (len(chunks) == 2 and chunks[0] == "Writing"
                    and chunks[1].endswith(tmp_buildfile)):
                dirpath = os.path.dirname(chunks[1])
                self.generated_files[dirpath].append(chunks[1])
                self.visited_dirs.add(dirpath)

                bzl_path = os.path.join(dirpath, BUILD_INPUT)
                if not os.path.isfile(bzl_path):
                    continue

                bzl_content = build_parser.parse_file(bzl_path)
                for rule in bzl_content.get_rules_by_types(GO_RULE_TYPES):
                    assert srcs_allowed(
                        bzl_path) or "srcs" not in rule.attr_map, (
                            "Do not specify `srcs` in %s - "
                            "these are autoinferred by `bzl gen`" % bzl_path)
                    assert "go_versions" not in rule.attr_map, (
                        "Do not specify `go_versions` in %s - "
                        "code should build with all supported Go versions." %
                        bzl_path)
            else:
                print(line)