Skip to content

Commit 8e5751d

Browse files
committed
examples: add example of local package with pip parse
Signed-off-by: Amaury Pouly <[email protected]>
1 parent 81639ca commit 8e5751d

File tree

7 files changed

+87
-2
lines changed

7 files changed

+87
-2
lines changed

.bazelrc

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
# (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it)
55
# To update these lines, execute
66
# `bazel run @rules_bazel_integration_test//tools:update_deleted_packages`
7-
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered
8-
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered
7+
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_local_package,examples/pip_parse_local_package/hello,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered
8+
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_local_package,examples/pip_parse_local_package/hello,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered
99

1010
test --test_output=errors
1111

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
load("@rules_python//python:defs.bzl", "py_test")
2+
load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary")
3+
4+
py_test(
5+
name = "test_hello",
6+
srcs = ["test_hello.py"],
7+
deps = [
8+
"@pypi//hello",
9+
],
10+
)
11+
12+
py_console_script_binary(
13+
name = "hello_parse",
14+
pkg = "@pypi//hello",
15+
script = "parse",
16+
)
17+
18+
sh_test(
19+
name = "test_hello_script",
20+
srcs = ["test_hello_script.sh"],
21+
data = [":hello_parse"],
22+
env = {
23+
"HELLO_PARSE": "$(rootpaths :hello_parse)",
24+
},
25+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module(name = "rules_python_pip_parse_local_package_example")
2+
3+
bazel_dep(name = "rules_python", version = "0.0.0")
4+
local_path_override(
5+
module_name = "rules_python",
6+
path = "../..",
7+
)
8+
9+
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
10+
python.toolchain(
11+
# We can specify the exact version.
12+
python_version = "3.9.13",
13+
)
14+
15+
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
16+
pip.parse(
17+
hub_name = "pypi",
18+
# If we want to expand the PROJECT_ROOT variable in the requirement lock file,
19+
# we need to pass a label to the file defining the project root.
20+
project_root = "//:pyproject.toml",
21+
# We need to use the same version here as in the `python.toolchain` call.
22+
python_version = "3.9.13",
23+
requirements_lock = "//:requirements_lock.txt",
24+
)
25+
use_repo(pip, "pypi", "pypi_39_hello")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[project]
2+
name = "test"
3+
version = "0.0.0"
4+
dependencies = [
5+
# Test
6+
"hello @ file://${PROJECT_ROOT}/hello",
7+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# This file was autogenerated by uv via the following command:
2+
# uv pip compile pyproject.toml -o requirements_lock.txt
3+
hello @ file://${PROJECT_ROOT}/hello
4+
# via test (pyproject.toml)
5+
hjson==3.1.0
6+
# via hello
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import hello
2+
3+
EXAMPLE_HJSON = """
4+
{
5+
# TL;DR
6+
human: Hjson
7+
machine: JSON
8+
}
9+
"""
10+
11+
res = hello.parse(EXAMPLE_HJSON)
12+
assert res["human"] == "Hjson"
13+
assert res["machine"] == "JSON"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
TEST_HJSON=$(mktemp)
2+
3+
echo "{
4+
# TL;DR
5+
human: Hjson
6+
machine: JSON
7+
}" > $TEST_HJSON
8+
9+
$HELLO_PARSE $TEST_HJSON

0 commit comments

Comments
 (0)