Finishing touches
This commit is contained in:
parent
ed0eb657e5
commit
f0a871dd91
2 changed files with 22 additions and 6 deletions
0
projects/bazelshim/src/bazelshim/__init__.py
Normal file
0
projects/bazelshim/src/bazelshim/__init__.py
Normal file
|
@ -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<foo> args to --<foo>=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)
|
||||
if sys.stderr.isatty() and not "--isatty=false" in cli.command_opts:
|
||||
print(
|
||||
"Info: Executing\n" + cli.render_text(next),
|
||||
"\u001b[33mInfo\u001b[0m: Executing\n" + cli.render_text(next),
|
||||
file=sys.stderr,
|
||||
)
|
||||
os.execv(next, cli.render_cli())
|
||||
|
|
Loading…
Reference in a new issue