Example #1
0
    def regenerate(self, bazel_targets: Iterable[str], cwd: str = ".") -> None:
        targets = bazel_utils.expand_bazel_targets(
            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
            pkg, _, _ = target.partition(":")
            target_dir = bazel_utils.normalize_relative_target_to_os_path(pkg[2:])

            _, parsed = self.parsed_cache.get_bzl(
                os.path.join(self.workspace_dir, target_dir)
            )

            if not parsed:
                self.maybe_traverse_non_bzl(target)
                continue

            if pkg in self.visited_bzl_dirs:
                continue
            self.visited_bzl_dirs.add(pkg)

            py_rules = parsed.get_rules_by_types(PY_RULE_TYPES)

            if not py_rules:
                if self.cfg.verbose:
                    print("No py targets found in %s:%s" % (pkg, BUILD_INPUT))
                continue

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

            if self.cfg.dry_run:
                continue

            self.generate_build_file(pkg, py_rules)
Example #2
0
def main():
    # type: () -> None
    ap = argparse.ArgumentParser(
        "bzl-gen",
        epilog=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    register_cmd_gen(None, get_generators(), sap=ap)

    args = ap.parse_args()

    workspace = bazel_utils.find_workspace()
    if not workspace:
        sys.exit("Run from a Bazel WORKSPACE.")
    try:
        if hasattr(args, "targets"):
            targets = args.targets
            require_build_file = not getattr(args, "missing_build_file_ok",
                                             False)

            targets = bazel_utils.expand_bazel_targets(
                workspace,
                targets,
                require_build_file=require_build_file,
                allow_nonexistent_npm_folders=True,
            )

            if not targets:
                sys.exit("No targets specified.")
            args.targets = targets

        args.func(args, (), ())
    except bazel_utils.BazelError as e:
        if os.environ.get("BZL_DEBUG"):
            raise
        sys.exit("ERROR: " + str(e))
    except subprocess.CalledProcessError as e:
        traceback.print_exc(file=sys.stderr)
        if e.output:
            print(e.output.decode("utf-8"), file=sys.stderr)
        if os.environ.get("BZL_DEBUG"):
            raise
        sys.exit(e.returncode)
    except KeyboardInterrupt:
        sys.exit("ERROR: interrupted")
Example #3
0
def main(ap, self_target):
    try:
        workspace = bazel_utils.find_workspace()
    except bazel_utils.BazelError as e:
        sys.exit("Bazel Error: {}".format(e))

    test_args = None
    try:
        # Hedge that we might not need to rebuild and exec. If for any
        # reason this fails, fall back to correct behavior.
        stdout, stderr = sys.stdout, sys.stderr
        with open("/dev/null", "w") as devnull:
            sys.stdout, sys.stderr = devnull, devnull
            test_args, unknown_args = ap.parse_known_args()
        # No built-in Bazel mode requires bzl to be up-to-date.
        rebuild_and_exec = test_args.mode not in bazel_modes
    except (SystemExit, AttributeError):
        rebuild_and_exec = True
    finally:
        sys.stdout, sys.stderr = stdout, stderr

    if os.environ.get("BZL_SKIP_BOOTSTRAP"):
        rebuild_and_exec = False
        # Propagate stats forward so we can sort of track the full metrics of itest.
        bootstrap_ms = int(os.environ.get("BZL_BOOTSTRAP_MS", 0))
        metrics.create_and_register_timer("bzl_bootstrap_ms",
                                          interval_ms=bootstrap_ms)
    if rebuild_and_exec:
        metrics.set_mode("_bzl_bootstrap")
        # If the tool requires an update, build it and re-exec.  Do this before we parse args in
        # case we have defined a newer mode.
        targets = []
        # Pass in targets that we are going to build. On average this minimizes target flapping
        # within bazel and saves time on small incremental updates without sacrificing correct
        # behavior.
        # do this for some itest modes and if there are no unknown args (as those can be
        # bazel flags that causes worse build flapping)
        if (test_args and test_args.mode
                in ("itest-run", "itest-start", "itest-reload")
                and not unknown_args):
            targets.append(itest.SVCCTL_TARGET)
            targets.append(test_args.target)
        # also do this for tool modes, so we can avoid an extra bazel build
        if test_args and test_args.mode in ("tool", "fmt"):
            targets.append(test_args.target)
        squelch_output = test_args and test_args.mode in ("tool", "go",
                                                          "go-env")
        run_build_tool(
            os.environ.get("BAZEL_PATH_FOR_BZL_REBUILD", "bazel"),
            self_target,
            targets,
            squelch_output=squelch_output,
        )

    args, remaining_args = ap.parse_known_args()
    metrics.set_mode(args.mode)
    subparser_map = ap._subparsers._group_actions[0].choices
    if remaining_args and (args.mode is None or not getattr(
            subparser_map[args.mode], "bzl_allow_unknown_args", False)):
        print(
            f"ERROR: unknown args for mode {args.mode}: {remaining_args}",
            file=sys.stderr,
        )
        sys.exit(2)

    bazel_args, mode_args = parse_bazel_args(remaining_args)
    if args.mode in (None, "help"):
        if not mode_args:
            ap.print_help()
            print()
        elif len(mode_args) == 1 and mode_args[0] not in bazel_modes:
            help_mode_parser = subparser_map[mode_args[0]]
            help_mode_parser.print_help()
        sys.stdout.flush()
        sys.exit(1 if args.mode is None else 0)

    if args.build_image and not args.build_image.startswith(
            args.docker_registry):
        args.build_image = os.path.join(args.docker_registry, args.build_image)

    try:
        if hasattr(args, "targets"):
            targets = args.targets
            require_build_file = not getattr(args, "missing_build_file_ok",
                                             False)

            targets = bazel_utils.expand_bazel_targets(
                workspace, targets, require_build_file=require_build_file)

            if not targets:
                sys.exit("No targets specified.")
            args.targets = targets

        args.func(args, bazel_args, mode_args)
    except bazel_utils.BazelError as e:
        if os.environ.get("BZL_DEBUG"):
            raise
        sys.exit("ERROR: " + str(e))
    except subprocess.CalledProcessError as e:
        print(e, file=sys.stderr)
        if e.output:
            print(e.output, file=sys.stderr)
        if os.environ.get("BZL_DEBUG"):
            raise
        sys.exit(e.returncode)
    except KeyboardInterrupt:
        sys.exit("ERROR: interrupted")