Skip to content

Commit df2b0d4

Browse files
bazel: run Python script with runfiles access
1 parent 18a9588 commit df2b0d4

File tree

10 files changed

+114
-32
lines changed

10 files changed

+114
-32
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ jobs:
3333
- name: Test
3434
run: bazel test //...
3535

36+
- name: Runfiles
37+
run: bazel run //tools:archiver
38+
3639
lint-host:
3740
runs-on: ubuntu-24.04
3841

BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,6 @@ tar(
3838
name = "archive-lean",
3939
srcs = ["//:LICENSE"],
4040
)
41+
42+
# to access by a `py_binary` target under `data`
43+
exports_files(["adhoc/data.json"])

MODULE.bazel

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ module(
55

66
http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
77

8+
http_file = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
9+
810
# Bazel specific
911
bazel_dep(name = "platforms", version = "0.0.11")
1012
bazel_dep(name = "rules_shell", version = "0.5.0")
@@ -66,6 +68,20 @@ http_archive(
6668
urls = ["https://github.com/kthohr/gcem/archive/v1.18.0.tar.gz"],
6769
)
6870

71+
http_file(
72+
name = "zstd_cli",
73+
executable = True,
74+
sha256 = "190c196b46883a234238d9c52a211c1494df79bb170fbe30c6fc22639d5d4731",
75+
url = "https://github.com/aspect-build/zstd-prebuilt/releases/download/v1.5.6-bcr2/zstd_linux_amd64",
76+
)
77+
78+
http_archive(
79+
name = "yq_cli",
80+
build_file = "@//tools/yq:BUILD.bazel.gen",
81+
sha256 = "09a40787e527005fce869dac01146fcc2473d4e24074a841baf502b8c87fb6e2",
82+
url = "https://github.com/mikefarah/yq/releases/download/v4.48.1/yq_linux_amd64.tar.gz",
83+
)
84+
6985
toolchains = use_extension("@hermetic_cc_toolchain//toolchain:ext.bzl", "toolchains")
7086

7187
# cannot use this due to an error

MODULE.bazel.lock

Lines changed: 3 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

adhoc/data.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"a": "A", "b": "B"}

compiled/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ cc_import(
1717
# to be linked into any binary that depends on it
1818
alwayslink = True,
1919
)
20+
21+
# to access by a `py_binary` target under `data`
22+
exports_files(["dates.so"])

tools/BUILD.bazel

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("@rules_python//python:defs.bzl", "py_binary")
12
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
23

34
package(default_visibility = ["//visibility:public"])
@@ -19,3 +20,25 @@ sh_binary(
1920
"@oclint_prebuilt//:oclint_bundle",
2021
],
2122
)
23+
24+
py_binary(
25+
name = "archiver",
26+
srcs = ["archiver.py"],
27+
data = [
28+
# http_file
29+
"@zstd_cli//file",
30+
# http_archive
31+
"@yq_cli//:yq_linux_amd64",
32+
# local file (in a Bazel package)
33+
"//compiled:dates.so",
34+
# local file (not in a Bazel package)
35+
"//:adhoc/data.json",
36+
],
37+
env = {
38+
"ZSTD_COMPRESSION_LEVEL": "10",
39+
},
40+
tags = ["manual"],
41+
deps = [
42+
"@rules_python//python/runfiles",
43+
],
44+
)

tools/archiver.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
Use `import pdb; pdb.set_trace()` to pause and inspect the runfiles directory
3+
"""
4+
5+
import os
6+
import shutil
7+
import subprocess
8+
9+
from python.runfiles import Runfiles
10+
11+
runfiles = Runfiles.Create()
12+
13+
# http_file location
14+
ZSTD_CLI = runfiles.Rlocation("zstd_cli/file/downloaded")
15+
16+
# http_archive location
17+
YQ_CLI = runfiles.Rlocation("yq_cli/yq_linux_amd64")
18+
19+
# local workspace file location (already in a Bazel package)
20+
dates_so = runfiles.Rlocation("_main/compiled/dates.so")
21+
22+
# local workspace file location (not in a Bazel package)
23+
data_json = runfiles.Rlocation("_main/adhoc/data.json")
24+
25+
pairs = [
26+
("zstd", ZSTD_CLI),
27+
("yq", YQ_CLI),
28+
("Data file tracked by Bazel", dates_so),
29+
("Data file not tracked by Bazel", data_json),
30+
]
31+
for pair in pairs:
32+
print(pair)
33+
34+
print("Pretty print of JSON with yq:")
35+
_ = subprocess.run(
36+
args=[YQ_CLI, data_json],
37+
check=True,
38+
text=True,
39+
)
40+
41+
print(f"Zstd compression level from env var: {os.getenv('ZSTD_COMPRESSION_LEVEL')}")
42+
_ = subprocess.run(
43+
args=[
44+
ZSTD_CLI,
45+
dates_so,
46+
"-o",
47+
"archive.zst",
48+
"--force",
49+
],
50+
check=True,
51+
text=True,
52+
)
53+
54+
# need to copy the archive from `/home/%username%/.cache/bazel/_bazel_%username%/checksum/execroot/_main/bazel-out/k8-fastbuild/bin/tools/archiver.runfiles/_main`
55+
shutil.copyfile(
56+
"archive.zst",
57+
f"{os.getenv('BUILD_WORKSPACE_DIRECTORY')}/archive.zst",
58+
)

tools/yq/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# this file needs to exist to mark `tools/yq` as a package

tools/yq/BUILD.bazel.gen

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
exports_files(["yq_linux_amd64"])

0 commit comments

Comments
 (0)