Skip to content

Commit

Permalink
chore: fix python test by using normal pip.parse
Browse files Browse the repository at this point in the history
I'd rather have rules_py just unpack a wheel, but it's broken
  • Loading branch information
alexeagle committed Mar 19, 2024
1 parent 33a20a3 commit 4d62f0b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
16 changes: 14 additions & 2 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ bazel_dep(name = "rules_proto", version = "6.0.0-rc2")
bazel_dep(name = "platforms", version = "0.0.8")

protoc = use_extension("//protoc:extensions.bzl", "protoc")
protoc.toolchain(google_protobuf = "com_google_protobuf", version = "v25.3")
protoc.toolchain(
google_protobuf = "com_google_protobuf",
version = "v25.3",
)
use_repo(protoc, "com_google_protobuf", "toolchains_protoc_hub")

register_toolchains("@toolchains_protoc_hub//:all")
Expand All @@ -22,7 +25,7 @@ bazel_dep(name = "aspect_rules_py", version = "0.7.1", dev_dependency = True)
bazel_dep(name = "rules_java", version = "7.4.0", dev_dependency = True)
bazel_dep(name = "rules_python", version = "0.31.0", dev_dependency = True)

# Update to include
# # Update to include
# https://github.com/bazelbuild/rules_python/pull/1577
git_override(
module_name = "rules_python",
Expand All @@ -34,3 +37,12 @@ register_toolchains(
"//examples/lang_toolchains:all",
dev_dependency = True,
)

# Shows how a typical Python user fetches all the dependencies of their app, including the protobuf runtime
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
pip.parse(
hub_name = "pypi",
python_version = "3.11",
requirements_lock = "//examples/lang_toolchains:requirements.txt",
)
use_repo(pip, "pypi")
14 changes: 3 additions & 11 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
# Marker that this is the root of a Bazel workspace.

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file", "http_jar")

# Fetch the runtime package that applications need to marshal proto files.
# This does NOT do any code generation of .pb2.py files, we still use protoc for that.
# From https://pypi.org/project/protobuf/4.25.3/
http_file(
name = "protobuf_4_25_3",
downloaded_file_path = "protobuf-4.25.3-py3-none-any.whl",
sha256 = "f0700d54bcf45424477e46a9f0944155b46fb0639d69728739c0e47bab83f2b9",
urls = ["https://files.pythonhosted.org/packages/f4/d5/db585a5e8d64af6b384c7b3a63da13df2ff86933e486ba78431736c67c25/protobuf-4.25.3-py3-none-any.whl"],
)
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_jar")

# Note: this is simpler than using rules_jvm_external with a maven installation,
# but users would probably get this JAR file that way.
http_jar(
name = "protobuf-java_3_25_3",
sha256 = "e90d8ddb963b20a972a6a59b5093ade2b07cbe546cab3279aaf4383260385f58",
Expand Down
12 changes: 1 addition & 11 deletions examples/lang_toolchains/BUILD
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
load("@aspect_rules_py//py:defs.bzl", "py_unpacked_wheel")
load("@rules_proto//proto:defs.bzl", "proto_lang_toolchain")

# See WORKSPACE.bazel for fetch instructions of "protobuf_4_25_3".
# Turn the downloaded .whl file into a py_library-shape (provides PyInfo)
# Avoids the need for any pip, requirements files, python interpreter for wheel unpacking, etc
py_unpacked_wheel(
name = "protobuf_wheel",
src = "@protobuf_4_25_3//file",
py_package_name = "protobuf",
)

# Configure protoc to have the right arguments for generating Python stubs.
proto_lang_toolchain(
name = "protoc_py_toolchain",
command_line = "--python_out=%s",
progress_message = "Generating Python proto_library %{label}",
runtime = ":protobuf_wheel",
runtime = "@pypi//protobuf",
)

proto_lang_toolchain(
Expand Down
19 changes: 19 additions & 0 deletions examples/lang_toolchains/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# bazel run //examples/lang_toolchains:requirements.update
#
protobuf==5.26.0 \
--hash=sha256:2c334550e1cb4efac5c8a3987384bf13a4334abaf5ab59e40479e7b70ecd6b19 \
--hash=sha256:6ee9d1aa02f951c5ce10bf8c6cfb7604133773038e33f913183c8b5201350600 \
--hash=sha256:7e47c57303466c867374a17b2b5e99c5a7c8b72a94118e2f28efb599f19b4069 \
--hash=sha256:82f5870d74c99addfe4152777bdf8168244b9cf0ac65f8eccf045ddfa9d80d9b \
--hash=sha256:8eef61a90631c21b06b4f492a27e199a269827f046de3bb68b61aa84fcf50905 \
--hash=sha256:a49b6c5359bf34fb7bf965bf21abfab4476e4527d822ab5289ee3bf73f291159 \
--hash=sha256:ca825f4eecb8c342d2ec581e6a5ad1ad1a47bededaecd768e0d3451ae4aaac2b \
--hash=sha256:dfd29f6eb34107dccf289a93d44fb6b131e68888d090b784b691775ac84e8213 \
--hash=sha256:e184175276edc222e2d5e314a72521e10049938a9a4961fe4bea9b25d073c03f \
--hash=sha256:efd4f5894c50bd76cbcfdd668cd941021333861ed0f441c78a83d8254a01cc9f \
--hash=sha256:f9ecc8eb6f18037e0cbf43256db0325d4723f429bca7ef5cd358b7c29d65f628
# via -r examples/lang_toolchains/requirements.in

0 comments on commit 4d62f0b

Please sign in to comment.