-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbpfmt_wrapper.py
97 lines (72 loc) · 1.92 KB
/
bpfmt_wrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python3
from __future__ import annotations
import os
from pathlib import Path
from shlex import join
from sys import argv
from argparse import (
SUPPRESS,
REMAINDER,
ArgumentParser,
RawTextHelpFormatter,
)
from subprocess import CalledProcessError, run
from python.runfiles import Runfiles # type: ignore
class RunfileNotFoundError(FileNotFoundError):
pass
def parser() -> ArgumentParser:
p = ArgumentParser(
formatter_class=RawTextHelpFormatter,
argument_default=SUPPRESS,
description="bpftm wrapper",
fromfile_prefix_chars="@",
)
p.add_argument(
"--bpfmt",
required=True,
metavar="BPFMT",
help="bpfmt binary path",
)
return p
def runfile(path: str) -> Path:
runfiles = Runfiles.Create()
resolved = Path(runfiles.Rlocation(path))
if not resolved.exists():
raise RunfileNotFoundError(path)
return resolved
def exec(exec: Path, *args: str) -> int:
cmd = (
exec,
*args,
)
try:
result = run(
cmd,
capture_output=True,
encoding="utf8",
cwd=os.environ.get("BUILD_WORKSPACE_DIRECTORY", "."),
)
if result.returncode != 0:
print(result.stderr.strip())
else:
print(result.stdout.strip())
return result.returncode
except CalledProcessError as e:
print(
"Subprocess command failed:\n%s\n\nstdout:\n%s\n\nstderr:\n%s",
join(e.cmd),
e.stdout,
e.stderr,
)
return e.returncode
def main(*args: str) -> None:
p = parser()
print("args: ", args)
parsed, rest = p.parse_known_args()
bpfmt = runfile(parsed.bpfmt)
print("parsed: ", parsed)
print("rest: ", rest)
print("bpfmt: ", bpfmt)
return exec(bpfmt, *rest)
if __name__ == "__main__":
exit(main(*argv[1:]))