-
Notifications
You must be signed in to change notification settings - Fork 544
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for annotating rendered pip dependencies
- Loading branch information
1 parent
b842276
commit 4590796
Showing
27 changed files
with
986 additions
and
67 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
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
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
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
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,2 @@ | ||
# https://docs.bazel.build/versions/main/best-practices.html#using-the-bazelrc-file | ||
try-import %workspace%/user.bazelrc |
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,30 @@ | ||
load("@pip_installed//:requirements.bzl", "requirement") | ||
load("@rules_python//python:defs.bzl", "py_test") | ||
load("@rules_python//python:pip.bzl", "compile_pip_requirements") | ||
|
||
exports_files( | ||
glob(["data/**"]), | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
# This rule adds a convenient way to update the requirements file. | ||
compile_pip_requirements( | ||
name = "requirements", | ||
extra_args = ["--allow-unsafe"], | ||
) | ||
|
||
py_test( | ||
name = "pip_parse_annotations_test", | ||
srcs = ["pip_repository_annotations_test.py"], | ||
env = {"WHEEL_PKG_DIR": "pip_parsed_wheel"}, | ||
main = "pip_repository_annotations_test.py", | ||
deps = ["@pip_parsed_wheel//:pkg"], | ||
) | ||
|
||
py_test( | ||
name = "pip_install_annotations_test", | ||
srcs = ["pip_repository_annotations_test.py"], | ||
env = {"WHEEL_PKG_DIR": "pip_installed/pypi__wheel"}, | ||
main = "pip_repository_annotations_test.py", | ||
deps = [requirement("wheel")], | ||
) |
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,58 @@ | ||
workspace(name = "pip_repository_annotations_example") | ||
|
||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") | ||
|
||
http_archive( | ||
name = "rules_python", | ||
sha256 = "cd6730ed53a002c56ce4e2f396ba3b3be262fd7cb68339f0377a45e8227fe332", | ||
url = "https://github.com/bazelbuild/rules_python/releases/download/0.5.0/rules_python-0.5.0.tar.gz", | ||
) | ||
|
||
http_archive( | ||
name = "bazel_skylib", | ||
sha256 = "c6966ec828da198c5d9adbaa94c05e3a1c7f21bd012a0b29ba8ddbccb2c93b0d", | ||
urls = [ | ||
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz", | ||
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz", | ||
], | ||
) | ||
|
||
load("@rules_python//python:pip.bzl", "package_annotation", "pip_install", "pip_parse") | ||
|
||
# Here we can see an example of annotations being applied to an arbitrary | ||
# package. For details on `package_annotation` and it's uses, see the | ||
# docs at @rules_python//docs:pip.md`. | ||
ANNOTATIONS = { | ||
"wheel": package_annotation( | ||
build_content = """\ | ||
load("@bazel_skylib//rules:write_file.bzl", "write_file") | ||
write_file( | ||
name = "generated_file", | ||
out = "generated_file.txt", | ||
content = ["Hello world from build content file"], | ||
) | ||
""", | ||
copy_executables = {"@pip_repository_annotations_example//:data/copy_executable.py": "copied_content/executable.py"}, | ||
copy_files = {"@pip_repository_annotations_example//:data/copy_file.txt": "copied_content/file.txt"}, | ||
data = [":generated_file"], | ||
data_exclude_glob = ["*.dist-info/RECORD"], | ||
), | ||
} | ||
|
||
# For a more thorough example of `pip_parse`. See `@rules_python//examples/pip_parse` | ||
pip_parse( | ||
name = "pip_parsed", | ||
annotations = ANNOTATIONS, | ||
requirements_lock = "//:requirements.txt", | ||
) | ||
|
||
load("@pip_parsed//:requirements.bzl", "install_deps") | ||
|
||
install_deps() | ||
|
||
# For a more thorough example of `pip_install`. See `@rules_python//examples/pip_install` | ||
pip_install( | ||
name = "pip_installed", | ||
annotations = ANNOTATIONS, | ||
requirements = "//:requirements.txt", | ||
) |
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,4 @@ | ||
#!/usr/bin/env python | ||
|
||
if __name__ == "__main__": | ||
print("Hello world from copied executable") |
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 from copied file |
59 changes: 59 additions & 0 deletions
59
examples/pip_repository_annotations/pip_repository_annotations_test.py
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,59 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import os | ||
import subprocess | ||
import unittest | ||
from glob import glob | ||
from pathlib import Path | ||
|
||
|
||
class PipRepositoryAnnotationsTest(unittest.TestCase): | ||
maxDiff = None | ||
|
||
def wheel_pkg_dir(self) -> str: | ||
env = os.environ.get("WHEEL_PKG_DIR") | ||
self.assertIsNotNone(env) | ||
return env | ||
|
||
def test_build_content_and_data(self): | ||
generated_file = ( | ||
Path.cwd() / "external" / self.wheel_pkg_dir() / "generated_file.txt" | ||
) | ||
self.assertTrue(generated_file.exists()) | ||
|
||
content = generated_file.read_text().rstrip() | ||
self.assertEqual(content, "Hello world from build content file") | ||
|
||
def test_copy_files(self): | ||
copied_file = ( | ||
Path.cwd() / "external" / self.wheel_pkg_dir() / "copied_content/file.txt" | ||
) | ||
self.assertTrue(copied_file.exists()) | ||
|
||
content = copied_file.read_text().rstrip() | ||
self.assertEqual(content, "Hello world from copied file") | ||
|
||
def test_copy_executables(self): | ||
executable = ( | ||
Path.cwd() | ||
/ "external" | ||
/ self.wheel_pkg_dir() | ||
/ "copied_content/executable.py" | ||
) | ||
self.assertTrue(executable.exists()) | ||
|
||
proc = subprocess.run( | ||
[executable], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE | ||
) | ||
stdout = proc.stdout.decode("utf-8").strip() | ||
self.assertEqual(stdout, "Hello world from copied executable") | ||
|
||
def test_data_exclude_glob(self): | ||
files = glob("external/" + self.wheel_pkg_dir() + "/wheel-*.dist-info/*") | ||
basenames = [Path(path).name for path in files] | ||
self.assertIn("WHEEL", basenames) | ||
self.assertNotIn("RECORD", basenames) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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 @@ | ||
wheel |
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,10 @@ | ||
# | ||
# This file is autogenerated by pip-compile | ||
# To update, run: | ||
# | ||
# bazel run //:requirements.update | ||
# | ||
wheel==0.37.1 \ | ||
--hash=sha256:4bdcd7d840138086126cd09254dc6195fb4fc6f01c050a1d7236f2630db1d22a \ | ||
--hash=sha256:e9a504e793efbca1b8e0e9cb979a249cf4a0a7b5b8c9e8b65a5e39d49529c1c4 | ||
# via -r requirements.in |
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
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
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
Oops, something went wrong.