forked from bazel-contrib/rules_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlock_copier.py
More file actions
69 lines (56 loc) · 1.53 KB
/
Copy pathlock_copier.py
File metadata and controls
69 lines (56 loc) · 1.53 KB
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
import sys
from difflib import unified_diff
from os import environ
from pathlib import Path
_LINE = "=" * 80
def main():
src = "{{src}}"
dst = "{{dst}}"
src = Path(src)
if not src.exists():
raise AssertionError(f"The {src} file does not exist")
if "TEST_SRCDIR" in environ:
# Running as a bazel test
dst = Path(dst)
a = dst.read_text() if dst.exists() else "\n"
b = src.read_text()
diff = unified_diff(
a.splitlines(),
b.splitlines(),
str(dst),
str(src),
lineterm="",
)
diff = "\n".join(list(diff))
if not diff:
print(
f"""\
{_LINE}
The in source file copy is up-to-date.
{_LINE}
"""
)
return 0
print(diff)
print(
f"""\
{_LINE}
The in source file copy is out of date, please run:
bazel run {{update_target}}
{_LINE}
"""
)
return 1
if "BUILD_WORKSPACE_DIRECTORY" not in environ:
raise RuntimeError(
"This must be either run as `bazel test` via a `native_test` or similar or via `bazel run`"
)
print(f"cp <bazel-sandbox>/{src} <workspace>/{dst}")
build_workspace = Path(environ["BUILD_WORKSPACE_DIRECTORY"])
dst_real_path = build_workspace / dst
dst_real_path.parent.mkdir(parents=True, exist_ok=True)
dst_real_path.write_text(src.read_text())
print(f"OK: updated {dst_real_path}")
return 0
if __name__ == "__main__":
sys.exit(main())