-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
a (subjectively) improved API for bazel runfiles in Python
- Loading branch information
Showing
4 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
load("@rules_python//python:defs.bzl", "py_library", "py_test") | ||
|
||
py_library( | ||
name = "runfiles", | ||
srcs = ["__init__.py"], | ||
visibility = ["//visibility:public"], | ||
deps = ["@rules_python//python/runfiles"], | ||
) | ||
|
||
py_test( | ||
name = "runfiles_test", | ||
size = "small", | ||
srcs = ["runfiles_test.py"], | ||
data = ["runfiles_test_data.txt"], | ||
deps = [ | ||
":runfiles", | ||
"@pip//pytest", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from pathlib import Path | ||
|
||
from python.runfiles import Runfiles | ||
|
||
# FIXME: https://github.com/bazelbuild/rules_python/issues/1679 | ||
# from rules_python.python.runfiles import runfiles | ||
|
||
_RUNFILES = Runfiles.Create() | ||
|
||
|
||
def locate(path: str | Path, workspace: str = "__main__") -> Path: | ||
if not isinstance(path, Path): | ||
path = Path(path) | ||
|
||
if path.is_absolute(): | ||
msg = f"{path=} must be a relative path" | ||
raise ValueError(msg) | ||
|
||
located = _RUNFILES.Rlocation(f"{workspace}/{path}") | ||
if located is None: | ||
msg = f"Failed to locate data-dependency. {path=} {workspace=}" | ||
raise ValueError(msg) | ||
|
||
pth = Path(located) | ||
if not pth.exists(): | ||
msg = f"Located data-dependency does not exist. {located=} {path=} {workspace=}" | ||
raise ValueError(msg) | ||
|
||
return pth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import sys | ||
|
||
import pytest | ||
|
||
from tools import runfiles | ||
|
||
|
||
def test_runfiles_happy(): | ||
data = runfiles.locate("tools/runfiles/runfiles_test_data.txt") | ||
assert data.read_text() == "Hello, world!\n" | ||
|
||
|
||
def test_runfiles_error(): | ||
to_locate = "tools/runfiles/does_not_exist.txt" | ||
with pytest.raises( | ||
ValueError, | ||
match=r"^Located data-dependency does not exist.", | ||
): | ||
runfiles.locate(to_locate) | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(pytest.main(sys.argv)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello, world! |