From f0a871dd91c272c2dada4c706c12bdfd7c381161 Mon Sep 17 00:00:00 2001 From: Reid 'arrdem' McKenzie Date: Tue, 6 Feb 2024 20:55:24 -0700 Subject: [PATCH] Finishing touches --- projects/bazelshim/src/bazelshim/__init__.py | 0 projects/bazelshim/src/bazelshim/__main__.py | 28 +++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 projects/bazelshim/src/bazelshim/__init__.py diff --git a/projects/bazelshim/src/bazelshim/__init__.py b/projects/bazelshim/src/bazelshim/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/projects/bazelshim/src/bazelshim/__main__.py b/projects/bazelshim/src/bazelshim/__main__.py index b953403..5942e78 100755 --- a/projects/bazelshim/src/bazelshim/__main__.py +++ b/projects/bazelshim/src/bazelshim/__main__.py @@ -6,10 +6,18 @@ # Bazel on behalf of the user. Consequently it has some magical (partial) knowledge of Bazel's CLI # options since it's really a CLI shim. +# Since this can't run under Bazel, we have to set up the sys.path ourselves +import os +import sys +from pathlib import Path + +if (p := str(Path(sys.argv[0]).absolute().parent.parent)) not in sys.path: + sys.path.pop(0) # Remove '.' / '' + sys.path.insert(0, p) # Insert the bazelshim root + from dataclasses import dataclass from shlex import quote, split as shlex import sys -import os from pathlib import Path from typing import List, Optional from itertools import chain @@ -65,6 +73,10 @@ def normalize_opts(args: List[str]) -> List[str]: # Convert --no args to --=no acc.append("--" + args.pop(0).lstrip("--no") + "=false") + elif args[0] == "--isatty=0": + acc.append("--isatty=false") + args.pop(0) + elif ( args[0].startswith("--") and not args[1].startswith("--") @@ -142,9 +154,11 @@ class BazelCli: elif self.command == "run": acc.append("--") acc.extend(self.subprocess_opts) + elif self.command and not self.subprocess_opts: + pass else: print( - f"Warning: {self.command} does not support -- args!", + f"Warning: {self.command} does not support -- args! {self.subprocess_opts!r}", file=sys.stderr, ) @@ -178,6 +192,7 @@ def middleware(cli): if __name__ == "__main__": + # This script has a magical flag to help with resolving bazel exclude = [] while len(sys.argv) > 1 and sys.argv[1].startswith("--bazelshim_exclude"): exclude.append(Path(sys.argv.pop(1).split("=")[1]).absolute()) @@ -188,8 +203,9 @@ if __name__ == "__main__": cli = BazelCli.parse_cli(["bazel"] + sys.argv[1:]) cli = middleware(cli) next = cli.executable(exclude=exclude) - print( - "Info: Executing\n" + cli.render_text(next), - file=sys.stderr, - ) + if sys.stderr.isatty() and not "--isatty=false" in cli.command_opts: + print( + "\u001b[33mInfo\u001b[0m: Executing\n" + cli.render_text(next), + file=sys.stderr, + ) os.execv(next, cli.render_cli())