Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for annotating rendered pip dependencies #589

Merged
merged 1 commit into from
Jan 18, 2022

Conversation

UebelAndre
Copy link
Contributor

@UebelAndre UebelAndre commented Dec 29, 2021

PR Checklist

Please check if your PR fulfills the following requirements:

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature (please, look at the "Scope of the project" section in the README.md file)
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

This change adds an annotations field to pip_parse and pip_install which allow users to make changes to generated python targets. The goal of this functionality is to allow users to filter out any non-determinsitic behavior they may find in their dependencies to resolve remote cache issues.

Issue Number: #571

What is the new behavior?

Below is an example of package_annotation. The rule allows us to add additional targets or set additional flags to packages like wheel in the example.

load("@rules_python//python:pip.bzl", "package_annotation", "pip_parse")

pip_parse(
    name = "pip_parsed",
    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"],
        ),
    },
    requirements_lock = "//:requirements.txt",
)

The annotations here introduce the following changes to the BUILD file

diff --git a/BUILD.pip_parsed_wheel.bazel b/BUILD.pip_parsed_wheel.bazel
index 74dbeb0..8e3f483 100644
--- a/BUILD.pip_parsed_wheel.bazel
+++ b/BUILD.pip_parsed_wheel.bazel
@@ -22,7 +22,7 @@ filegroup(
 py_library(
     name = "pkg",
     srcs = glob(["**/*.py"], exclude=[], allow_empty = True),
-    data = [] + glob(["**/*"], exclude=["**/* *", "**/*.py", "**/*.pyc", "**/__pycache__/**", "*.whl", "BUILD.bazel", "WORKSPACE", "rules_python_wheel_entry_point*.py"]),
+    data = [":generated_file", "copied_content/executable.py", "copied_content/file.txt"] + glob(["**/*"], exclude=["**/* *", "**/*.py", "**/*.pyc", "**/__pycache__/**", "*.dist-info/RECORD", "*.whl", "BUILD.bazel", "WORKSPACE", "rules_python_wheel_entry_point*.py"]),
     # This makes this directory a top-level in the python import
     # search path for anything that depends on this.
     imports = ["."],
@@ -38,3 +38,24 @@ py_binary(
     imports = ["."],
     deps = ["pkg"],
 )
+
+copy_file(
+    name = "copied_content/file.txt.copy",
+    src = "@pip_repository_annotations_example//:data/copy_file.txt",
+    out = "copied_content/file.txt",
+    is_executable = False,
+)
+
+copy_file(
+    name = "copied_content/executable.py.copy",
+    src = "@pip_repository_annotations_example//:data/copy_executable.py",
+    out = "copied_content/executable.py",
+    is_executable = True,
+)
+
+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"],
+)

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

python/pip_install/pip_repository.bzl Outdated Show resolved Hide resolved
python/pip_install/pip_repository.bzl Outdated Show resolved Hide resolved
docs/pip.md Show resolved Hide resolved
@UebelAndre
Copy link
Contributor Author

For better visibility, one issue came out of this PR: 7740b22#r64135831

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants