Skip to content

Commit da410b3

Browse files
committed
feat: wheel publishing
Tested manually with: $ cd examples/wheel $ bazel run --stamp --embed_label=1.2.3 -- :customized.publish --repository testpypi The result was published here: https://test.pypi.org/project/example-customized/1.2.3/
1 parent 9960253 commit da410b3

16 files changed

+516
-93
lines changed

.bazelrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
# This lets us glob() up all the files inside the examples to make them inputs to tests
44
# (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it)
55
# To update these lines, run tools/bazel_integration_test/update_deleted_packages.sh
6-
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/get_url,examples/bzlmod,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/runfiles,examples/multi_python_versions,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_install,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_import,examples/relative_requirements,tests/compile_pip_requirements,tests/pip_repository_entry_points,tests/pip_deps
7-
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/get_url,examples/bzlmod,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/runfiles,examples/multi_python_versions,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_install,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_import,examples/relative_requirements,tests/compile_pip_requirements,tests/pip_repository_entry_points,tests/pip_deps
6+
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/runfiles,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_install,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/wheel,examples/wheel/lib,examples/wheel/private,tests/compile_pip_requirements,tests/load_from_macro,tests/pip_repository_entry_points,tests/runfiles
7+
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/runfiles,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_install,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/wheel,examples/wheel/lib,examples/wheel/private,tests/compile_pip_requirements,tests/load_from_macro,tests/pip_repository_entry_points,tests/runfiles
88

99
test --test_output=errors
1010

.gitignore

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@
3232
*~
3333

3434
# Bazel directories
35-
/bazel-*
36-
/bazel-bin
37-
/bazel-genfiles
38-
/bazel-out
39-
/bazel-testlogs
35+
bazel-*
4036
user.bazelrc
4137

4238
# vim swap files

examples/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ bazel_integration_test(
4848
timeout = "long",
4949
)
5050

51+
bazel_integration_test(
52+
name = "wheel_example",
53+
timeout = "long",
54+
)
55+
5156
bazel_integration_test(
5257
name = "bzlmod_example",
5358
bzlmod = True,

examples/wheel/BUILD.bazel

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,29 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
load("//examples/wheel/private:wheel_utils.bzl", "directory_writer")
16-
load("//python:defs.bzl", "py_library", "py_test")
17-
load("//python:packaging.bzl", "py_package", "py_wheel")
18-
load("//python:versions.bzl", "gen_python_config_settings")
15+
load("@rules_python//python:defs.bzl", "py_library", "py_test")
16+
load("@rules_python//python:packaging.bzl", "py_package", "py_wheel")
17+
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
18+
load("@rules_python//python:versions.bzl", "gen_python_config_settings")
19+
load("//private:wheel_utils.bzl", "directory_writer")
1920

2021
package(default_visibility = ["//visibility:public"])
2122

2223
licenses(["notice"]) # Apache 2.0
2324

25+
compile_pip_requirements(
26+
name = "requirements",
27+
)
28+
2429
py_library(
2530
name = "main",
2631
srcs = ["main.py"],
2732
deps = [
28-
"//examples/wheel/lib:simple_module",
29-
"//examples/wheel/lib:module_with_data",
33+
"//lib:simple_module",
34+
"//lib:module_with_data",
3035
# Example dependency which is not packaged in the wheel
3136
# due to "packages" filter on py_package rule.
32-
"//tests/load_from_macro:foo",
37+
"//other_lib:foo",
3338
],
3439
)
3540

@@ -55,8 +60,8 @@ py_wheel(
5560
python_tag = "py3",
5661
version = "0.0.1",
5762
deps = [
58-
"//examples/wheel/lib:module_with_data",
59-
"//examples/wheel/lib:simple_module",
63+
"//lib:module_with_data",
64+
"//lib:simple_module",
6065
],
6166
)
6267

@@ -69,8 +74,8 @@ py_wheel(
6974
stamp = 1,
7075
version = "0.1.{BUILD_TIMESTAMP}",
7176
deps = [
72-
"//examples/wheel/lib:module_with_data",
73-
"//examples/wheel/lib:simple_module",
77+
"//lib:module_with_data",
78+
"//lib:simple_module",
7479
],
7580
)
7681

@@ -121,16 +126,16 @@ py_wheel(
121126
],
122127
},
123128
extra_distinfo_files = {
124-
"//examples/wheel:NOTICE": "NOTICE",
129+
"//:NOTICE": "NOTICE",
125130
# Rename the file when packaging to show we can.
126-
"//examples/wheel:README.md": "README",
131+
"//:README.md": "README",
127132
},
128-
homepage = "www.example.com",
133+
homepage = "https://github.com/bazelbuild/rules_python",
129134
license = "Apache 2.0",
130135
python_tag = "py3",
131136
# Requirements embedded into the wheel metadata.
132137
requires = ["pytest"],
133-
version = "0.0.1",
138+
version = "{BUILD_EMBED_LABEL}",
134139
deps = [":example_pkg"],
135140
)
136141

@@ -158,8 +163,8 @@ py_wheel(
158163
distribution = "example_custom_package_root_multi_prefix",
159164
python_tag = "py3",
160165
strip_path_prefixes = [
161-
"examples/wheel/lib",
162-
"examples/wheel",
166+
"lib",
167+
"",
163168
],
164169
version = "0.0.1",
165170
deps = [
@@ -173,8 +178,8 @@ py_wheel(
173178
distribution = "example_custom_package_root_multi_prefix_reverse_order",
174179
python_tag = "py3",
175180
strip_path_prefixes = [
176-
"examples/wheel",
177-
"examples/wheel/lib", # this is not effective, because the first prefix takes priority
181+
"",
182+
"/lib", # this is not effective, because the first prefix takes priority
178183
],
179184
version = "0.0.1",
180185
deps = [

examples/wheel/WORKSPACE

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
workspace(name = "wheel_example")
2+
3+
local_repository(
4+
name = "rules_python",
5+
path = "../..",
6+
)
7+
8+
load("@rules_python//python:repositories.bzl", "py_repositories", "python_register_toolchains")
9+
10+
py_repositories()
11+
12+
python_register_toolchains(
13+
name = "python39",
14+
python_version = "3.9",
15+
)
16+
17+
load("@python39//:defs.bzl", "interpreter")
18+
load("@rules_python//python:pip.bzl", "pip_parse")
19+
20+
pip_parse(
21+
name = "pypi",
22+
python_interpreter_target = interpreter,
23+
requirements_lock = "//:requirements.txt",
24+
)
25+
26+
load("@pypi//:requirements.bzl", "install_deps")
27+
28+
install_deps()

examples/wheel/lib/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
load("//python:defs.bzl", "py_library")
15+
load("@rules_python//python:defs.bzl", "py_library")
1616

1717
package(default_visibility = ["//visibility:public"])
1818

examples/wheel/other_lib/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("@rules_python//python:defs.bzl", "py_library")
2+
3+
py_library(
4+
name = "foo",
5+
srcs = ["foo.py"],
6+
# Allow a test to verify an "outside package" doesn't get included
7+
visibility = ["//:__pkg__"],
8+
)

examples/wheel/other_lib/foo.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2019 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.

examples/wheel/private/directory_writer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python3
2-
"""The action executable of the `@rules_python//examples/wheel/private:wheel_utils.bzl%directory_writer` rule."""
2+
"""The action executable of the `//private:wheel_utils.bzl%directory_writer` rule."""
33

44
import argparse
55
import json

examples/wheel/private/wheel_utils.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ directory_writer = rule(
3636
"_writer": attr.label(
3737
executable = True,
3838
cfg = "exec",
39-
default = Label("//examples/wheel/private:directory_writer"),
39+
default = Label("//private:directory_writer"),
4040
),
4141
},
4242
)

examples/wheel/requirements.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
twine

0 commit comments

Comments
 (0)