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.
81 lines
2.2 KiB
Python
Executable File
81 lines
2.2 KiB
Python
Executable File
#!/usr/bin/env -S uv run --script
|
|
|
|
# /// script
|
|
# requires-python = ">=3.11"
|
|
# ///
|
|
|
|
# NOTE: The `uv` shebang and inline metadata above are only used for direct
|
|
# execution of this script outside of Bazel. When executed by Bazel (e.g., as a
|
|
# workspace_status_command), Bazel uses its own hermetic Python toolchain
|
|
# and ignores this metadata.
|
|
|
|
|
|
"""Bazel `--workspace_status_command` script.
|
|
|
|
This script is designed to be used in Bazel`s `--workspace_status_command` and
|
|
generate any desirable status artifacts.
|
|
"""
|
|
|
|
__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 os
|
|
import subprocess
|
|
import sys
|
|
|
|
|
|
def use_jj() -> bool:
|
|
if os.path.isdir(".jj"):
|
|
return True
|
|
elif os.path.exists(".git"):
|
|
return False
|
|
print("Can't tell whether to use jj or git:", os.getcwd())
|
|
sys.exit(1)
|
|
|
|
|
|
def git_commit_sha() -> str:
|
|
return subprocess.check_output(
|
|
["git", "rev-parse", "--short", "HEAD"], encoding="utf-8"
|
|
).strip()
|
|
|
|
|
|
def git_dirty_suffix() -> str:
|
|
status = subprocess.check_output(
|
|
["git", "status", "--porcelain"], encoding="utf-8"
|
|
).strip()
|
|
return ".dirty" if len(status) > 0 else ""
|
|
|
|
|
|
def jj_commit_sha() -> str:
|
|
# Get the first 9 characters of the commit id of the parent of the current
|
|
# working copy.
|
|
return subprocess.check_output(
|
|
["jj", "log", "-r", "@-", "--no-graph", "-T", "commit_id.shortest(9)"],
|
|
encoding="utf-8",
|
|
).strip()
|
|
|
|
|
|
def jj_dirty_suffix() -> str:
|
|
# This `jj log` template returns "true" if the current working copy is
|
|
# empty, otherwise "false".
|
|
status = subprocess.check_output(
|
|
["jj", "log", "-r", "@", "--no-graph", "-T", "empty"], encoding="utf-8"
|
|
).strip()
|
|
return ".dirty" if status == "false" else ""
|
|
|
|
|
|
def main() -> None:
|
|
if use_jj():
|
|
print("STABLE_GIT_COMMIT_SHA " + jj_commit_sha())
|
|
print("STABLE_GIT_DIRTY_SUFFIX " + jj_dirty_suffix())
|
|
else:
|
|
print("STABLE_GIT_COMMIT_SHA " + git_commit_sha())
|
|
print("STABLE_GIT_DIRTY_SUFFIX " + git_dirty_suffix())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|