Похоже, штамповка может быть не включена для py_binary
.Это должно работать для genrule
, поэтому должно работать что-то простое:
py_binary(
name = "foo",
srcs = ["foo.py"],
data = [":stable-status.txt"],
)
genrule(
name = "copy_stable-status.txt",
outs = ["stable-status.txt"],
cmd = "cp bazel-out/stable-status.txt $@",
stamp = 1,
)
foo.py
:
build_info = {}
with open("stable-status.txt") as stable_status:
for line in stable_status.readlines():
key, val = line.split(" ", 1)
build_info[key] = val.strip()
print("Build label is:")
print(build_info['BUILD_EMBED_LABEL'])
затем:
$ bazel run foo --embed_label=foobar
Starting local Bazel server and connecting to it...
INFO: Analyzed target //:foo (17 packages loaded, 102 targets configured).
INFO: Found 1 target...
Target //:foo up-to-date:
bazel-bin/foo
INFO: Elapsed time: 2.985s, Critical Path: 0.11s
INFO: 1 process: 1 linux-sandbox.
INFO: Build completed successfully, 6 total actions
INFO: Build completed successfully, 6 total actions
Build label is:
foobar
Или что-то более необычное:
py_binary(
name = "bar",
srcs = ["bar.py"],
deps = [":buildinfo"],
)
py_library(
name = "buildinfo",
srcs = [":buildinfo.py"],
)
genrule(
name = "gen_buildinfo",
outs = ["buildinfo.py"],
cmd = r"""sed -E 's/(.*) (.*)/\1 = "\2"/' bazel-out/stable-status.txt > $@""",
stamp = 1,
)
bar.py
:
import buildinfo
print("Build label is:")
print(buildinfo.BUILD_EMBED_LABEL)