mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
This removes the need to install any specific version of Python or figure out how to configure it by instead asking users to install `uv` and letting it manage Python. Among other advantages, `uv` is designed to be fast enough to embed directly into our scripts. We were already using this in `bench_runner.py` so that the script could import non standard library dependencies. Moving to it for the rest of our Python unifies the approach and will also enable dependencies whenever needed. I've left `github_tools` alone as it has special handling with its own Bazel setup. I've updated the contributing tools to explain the approach here.
117 lines
3.2 KiB
Python
Executable File
117 lines
3.2 KiB
Python
Executable File
#!/usr/bin/env -S uv run --script
|
|
|
|
# /// script
|
|
# requires-python = ">=3.11"
|
|
# ///
|
|
|
|
|
|
"""Computes the potentially differing rules from some git commit.
|
|
|
|
Wraps the "target-determinator" Go program here:
|
|
https://github.com/bazel-contrib/target-determinator
|
|
|
|
The purpose is to compute the potentially impacted set of targets from some
|
|
provided Git commit to the current checkout.
|
|
|
|
This script will ensure a cached version of the latest release is available, and
|
|
then forward a limited set of flags to it. This script also filters the
|
|
resulting targets using `bazel query` to make it the most relevant list for
|
|
continuous integration.
|
|
"""
|
|
|
|
__copyright__ = """
|
|
Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
Exceptions. See /LICENSE for license information.
|
|
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
"""
|
|
|
|
import subprocess
|
|
import argparse
|
|
import tempfile
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import scripts_utils
|
|
|
|
|
|
def log(s: str) -> None:
|
|
print(s, file=sys.stderr)
|
|
|
|
|
|
def filter_targets(bazel: Path, targets: str) -> str:
|
|
# Need to quote targets for inclusion in another query.
|
|
quoted_targets = "\n".join([f'"{t}"' for t in targets.splitlines()])
|
|
|
|
with tempfile.NamedTemporaryFile(mode="w+") as tmp:
|
|
query = (
|
|
f"let t = set({quoted_targets}) in "
|
|
"kind(rule, $t) except attr(tags, manual, $t)\n"
|
|
)
|
|
query_lines = query.splitlines()
|
|
if len(query_lines) <= 10:
|
|
query_snippet = "\n".join(query_lines)
|
|
else:
|
|
query_snippet = "\n".join(
|
|
query_lines[:5] + ["..."] + query_lines[-5:]
|
|
)
|
|
log(f"Bazel query snippet:\n```\n{query_snippet}\n```")
|
|
tmp.write(query)
|
|
tmp.flush()
|
|
try:
|
|
p = subprocess.run(
|
|
[str(bazel), "query", f"--query_file={tmp.name}"],
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE,
|
|
check=True,
|
|
encoding="utf-8",
|
|
)
|
|
return p.stdout
|
|
except subprocess.CalledProcessError as err:
|
|
log(err.stderr)
|
|
log("Full query file:\n```")
|
|
with open(tmp.name) as f:
|
|
log(f.read())
|
|
log("```")
|
|
raise
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(__doc__)
|
|
parser.add_argument(
|
|
"baseline", nargs=1, help="Git commit of the diff baseline."
|
|
)
|
|
parser.add_argument(
|
|
"args",
|
|
nargs="*",
|
|
help="Remaining args to forward to the underlying tool.",
|
|
)
|
|
parsed_args = parser.parse_args()
|
|
|
|
scripts_utils.chdir_repo_root()
|
|
bazel = Path(scripts_utils.locate_bazel())
|
|
target_determinator = scripts_utils.get_release(
|
|
scripts_utils.Release.TARGET_DETERMINATOR
|
|
)
|
|
p = subprocess.run(
|
|
[
|
|
target_determinator,
|
|
f"--bazel={bazel}",
|
|
parsed_args.baseline[0],
|
|
]
|
|
+ parsed_args.args,
|
|
check=True,
|
|
stdout=subprocess.PIPE,
|
|
encoding="utf-8",
|
|
)
|
|
|
|
targets = p.stdout
|
|
if targets.strip() != "":
|
|
targets = filter_targets(bazel, targets)
|
|
log(f"Found {len(targets.splitlines())} impacted targets!")
|
|
|
|
print(targets.rstrip())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|