Skip to content

Commit

Permalink
Stop using undecorated python scripts from bash.
Browse files Browse the repository at this point in the history
scion.sh was still using togen.py, topodot.py and set_ipv6_addr.py
directly; with the consequence that any significant deviation between the
python environment in the build tree and that of the global environment could
cause these tools to fail.

Instead, we now produce py_binaries for these three tools, install them in
bin/, and use them from there.

To make them work when not invoked from bazel, we have to package them as self
contained executables, which is achieved by adding the --build_python_zip
command line flag. Suprisingly, this is the only method available. There isn't
even an equivalent parameter to the py_binary rule. All other workarounds turn
out to do crazy things (e.g. pkg_tar with the "include_run_files" option
produces non-sense). The tools are requested as part of make all.

Another case of calling python scripts outside bazel was
router_benchmark/benchmark.py. For that one, I arranged so it can be invoked
through bazel (like its twin brother test.py) instead of only from the command
line.
  • Loading branch information
jiceatscion committed Oct 29, 2024
1 parent 5bc8049 commit 572cbc3
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 15 deletions.
1 change: 1 addition & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ build --incompatible_default_to_explicit_init_py
# Enable resolution of cc toolchain by go toolchain
build --incompatible_enable_cc_toolchain_resolution
build --flag_alias=file_name_version=//:file_name_version
build --build_python_zip

# include one of "--define gotags=sqlite_mattn" or "--define gotags=sqlite_modernc"
# cannot be in common, because query chokes on it.
Expand Down
5 changes: 3 additions & 2 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,11 @@ pkg_tar(
pkg_tar(
name = "scion-topo",
srcs = [
"//scion-pki/cmd/scion-pki",
"//tools:set_ipv6_addr",
"//tools:topodot",
"//tools:topogen",
],
mode = "0755",
package_dir = "",
)

# Nogo - Go code analysis tool
Expand Down
7 changes: 2 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

build-dev:
rm -f bin/*
bazel build //:scion //:scion-ci
bazel build //:scion //:scion-ci //:scion-topo
tar -kxf bazel-bin/scion.tar -C bin
tar -kxf bazel-bin/scion-ci.tar -C bin
tar -kxf bazel-bin/scion-topo.tar -C bin

build:
rm -f bin/*
Expand Down Expand Up @@ -75,10 +76,6 @@ docker-images:
@echo "Load images"
@bazel cquery '//docker:prod union //docker:test' --output=files 2>/dev/null | xargs -I{} docker load --input {}

scion-topo:
bazel build //:scion-topo
tar --overwrite -xf bazel-bin/scion-topo.tar -C bin

protobuf:
rm -rf bazel-bin/pkg/proto/*/go_default_library_/github.com/scionproto/scion/pkg/proto/*
bazel build --output_groups=go_generated_srcs //pkg/proto/...
Expand Down
7 changes: 7 additions & 0 deletions acceptance/router_benchmark/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ py_library(
py_binary(
name = "benchmark",
srcs = ["benchmark.py"],
args = [
"--brload",
"$(location //acceptance/router_benchmark/brload:brload)",
],
data = [
"//acceptance/router_benchmark/brload",
],
visibility = ["//visibility:public"],
deps = [
"benchmarklib",
Expand Down
18 changes: 12 additions & 6 deletions acceptance/router_benchmark/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,22 @@ class RouterBMTool(cli.Application, RouterBM):
log_level = cli.SwitchAttr(["l", "loglevel"], str, default='warning', help="Logging level")

doit = cli.Flag(["r", "run"],
help="Run the benchmark, as opposed to seeing the instructions.")
help="Run the benchmark, as opposed to seeing the instructions")
json = cli.Flag(["j", "json"],
help="Output the report in json format.")
help="Output the report in json format")

# Used by the RouterBM mixin:
coremark = cli.SwitchAttr(["c", "coremark"], int, default=0,
help="The coremark score of the subject machine.")
help="The coremark score of the subject machine")
mmbm = cli.SwitchAttr(["m", "mmbm"], int, default=0,
help="The mmbm score of the subject machine.")
help="The mmbm score of the subject machine")
packet_size = cli.SwitchAttr(["s", "size"], int, default=172,
help="Test packet size (includes all headers - floored at 154).")
help="Test packet size (includes all headers - floored at 154)")
brload_path = cli.SwitchAttr(["b", "brload"], str, default="bin/brload",
help="Relative path to the brload tool")

intf_map: dict[str, Intf] = {}
brload: LocalCommand = local["./bin/brload"]
brload: LocalCommand = None
brload_cpus: list[int] = []
artifacts = f"{os.getcwd()}/acceptance/router_benchmark"
prom_address: str = "localhost:9090"
Expand Down Expand Up @@ -332,6 +335,9 @@ def instructions(self):
""")

def main(self, *interfaces: str):
# brload cannot be set statically. It need the cli arguments to be
# processed.
self.brload = local[self.brload_path]
status = 1
try:
logging.basicConfig(level=self.log_level.upper())
Expand Down
4 changes: 2 additions & 2 deletions scion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ cmd_topology() {
cmd_topo-clean

echo "Create topology, configuration, and execution files."
tools/topogen.py "$@"
./bin/topogen "$@"
}

cmd_topodot() {
./tools/topodot.py "$@"
./bin/topodot "$@"
}

start_scion() {
Expand Down
31 changes: 31 additions & 0 deletions tools/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,34 @@ py_binary(
requirement("pyyaml"),
],
)

py_binary(
name = "topodot",
srcs = ["topodot.py"],
data = [
],
main = "topodot.py",
python_version = "PY3",
srcs_version = "PY3",
visibility = ["//visibility:public"],
deps = [
"//tools/topology:py_default_library",
"@bazel_tools//tools/python/runfiles",
requirement("plumbum"),
],
)

py_binary(
name = "set_ipv6_addr",
srcs = ["set_ipv6_addr.py"],
data = [
],
main = "set_ipv6_addr.py",
python_version = "PY3",
srcs_version = "PY3",
visibility = ["//visibility:public"],
deps = [
"//tools/topology:py_default_library",
"@bazel_tools//tools/python/runfiles",
],
)

0 comments on commit 572cbc3

Please sign in to comment.