mongo/bazel/wrapper_hook/wrapper_hook.py

99 lines
3.7 KiB
Python

#!/usr/bin/env python3
import os
import sys
from pathlib import Path
REPO_ROOT = Path(__file__).parent.parent.parent
sys.path.append(str(REPO_ROOT))
# This script should be careful not to disrupt automatic mechanism which
# may be expecting certain stdout, always print to stderr.
sys.stdout = sys.stderr
from bazel.wrapper_hook.install_modules import install_modules
from bazel.wrapper_hook.wrapper_debug import wrapper_debug
wrapper_debug(f"wrapper hook script is using {sys.executable}")
def main():
install_modules(sys.argv[1], sys.argv[1:])
from bazel.auto_header.gen_all_headers import spawn_all_headers_thread
from bazel.wrapper_hook.autogenerated_targets import autogenerate_targets
from bazel.wrapper_hook.engflow_check import engflow_auth
from bazel.wrapper_hook.generate_common_bes_bazelrc import write_workstation_bazelrc
from bazel.wrapper_hook.lint import LinterFail
from bazel.wrapper_hook.plus_interface import check_bazel_command_type, test_runner_interface
from bazel.wrapper_hook.set_mongo_variables import write_mongo_variables_bazelrc
# Kick off the fast-path generator BEFORE autogenerate_targets.
th, hdr_state = spawn_all_headers_thread(REPO_ROOT)
# This is used to autogenerate a BUILD.bazel that creates
# Filegroups for select tags - used to group targets for installing
autogenerate_targets(sys.argv, sys.argv[1])
enterprise = True
if check_bazel_command_type(sys.argv[1:]) not in ["clean", "shutdown", "version", None]:
args = sys.argv
enterprise_mod = REPO_ROOT / "src" / "mongo" / "db" / "modules" / "enterprise"
if not enterprise_mod.exists():
enterprise = False
print(
f"{enterprise_mod.relative_to(REPO_ROOT).as_posix()} missing, defaulting to local non-enterprise build (--config=local --//bazel/config:build_enterprise=False). Add the directory to not automatically add these options."
)
args += ["--//bazel/config:build_enterprise=False", "--config=local"]
atlas_mod = REPO_ROOT / "src" / "mongo" / "db" / "modules" / "atlas"
if not atlas_mod.exists():
args += ["--//bazel/config:build_atlas=False"]
if any(arg.startswith("--include_mongot") for arg in args):
os.makedirs("mongot-localdev", exist_ok=True)
engflow_auth(args)
write_workstation_bazelrc(args)
write_mongo_variables_bazelrc(args)
try:
args = test_runner_interface(
sys.argv[1:],
autocomplete_query=os.environ.get("MONGO_AUTOCOMPLETE_QUERY") == "1",
enterprise=enterprise,
)
except LinterFail:
# Linter fails preempt bazel run.
# Ensure the header thread is finished before exiting.
th.join()
if hdr_state["ok"]:
wrapper_debug(
f'[all_headers] done in {hdr_state["t_ms"]:.1f} ms '
f'({"wrote" if hdr_state["wrote"] else "nochange"})'
)
else:
print(f'[all_headers] failed: {hdr_state["err"]!r}')
sys.exit(3)
else:
args = sys.argv[2:]
# Join the header generator before finalizing args.
th.join()
if hdr_state["ok"]:
wrapper_debug(
f'[all_headers] done in {hdr_state["t_ms"]:.1f} ms '
f'({"wrote" if hdr_state["wrote"] else "nochange"})'
)
else:
print(f'[all_headers] failed: {hdr_state["err"]!r}')
os.chmod(os.environ.get("MONGO_BAZEL_WRAPPER_ARGS"), 0o644)
with open(os.environ.get("MONGO_BAZEL_WRAPPER_ARGS"), "w") as f:
f.write("\n".join(args))
f.write("\n")
if __name__ == "__main__":
main()