Skip to content

Commit a284239

Browse files
committed
one-shot attempt
1 parent 0cd9bfa commit a284239

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

python/private/pypi/generate_whl_library_build_bazel.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ def generate_whl_library_build_bazel(
109109
kwargs["copy_executables"] = annotation.copy_executables
110110
kwargs["data_exclude"] = kwargs.get("data_exclude", []) + annotation.data_exclude_glob
111111
kwargs["srcs_exclude"] = annotation.srcs_exclude_glob
112+
if annotation.enable_implicit_namespace_pkgs != None:
113+
kwargs["enable_implicit_namespace_pkgs"] = annotation.enable_implicit_namespace_pkgs
112114
if annotation.additive_build_content:
113115
additional_content.append(annotation.additive_build_content)
114116
if default_python_version:

python/private/pypi/package_annotation.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ def package_annotation(
2020
copy_executables = {},
2121
data = [],
2222
data_exclude_glob = [],
23-
srcs_exclude_glob = []):
23+
srcs_exclude_glob = [],
24+
enable_implicit_namespace_pkgs = None):
2425
"""Annotations to apply to the BUILD file content from package generated from a `pip_repository` rule.
2526
2627
[cf]: https://github.com/bazelbuild/bazel-skylib/blob/main/docs/copy_file_doc.md
@@ -35,6 +36,8 @@ def package_annotation(
3536
data_exclude_glob (list, optional): A list of exclude glob patterns to add as `data` to the generated
3637
`py_library` target.
3738
srcs_exclude_glob (list, optional): A list of labels to add as `srcs` to the generated `py_library` target.
39+
enable_implicit_namespace_pkgs (bool, optional): Override the global setting for generating __init__.py
40+
files for namespace packages. If None, uses the repository-level setting.
3841
3942
Returns:
4043
str: A json encoded string of the provided content.
@@ -46,4 +49,5 @@ def package_annotation(
4649
data = data,
4750
data_exclude_glob = data_exclude_glob,
4851
srcs_exclude_glob = srcs_exclude_glob,
52+
enable_implicit_namespace_pkgs = enable_implicit_namespace_pkgs,
4953
))

tests/pypi/generate_whl_library_build_bazel/generate_whl_library_build_bazel_tests.bzl

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,80 @@ whl_library_targets_from_requires(
211211

212212
_tests.append(_test_all_with_loads)
213213

214+
def _test_enable_implicit_namespace_pkgs_annotation(env):
215+
want = """\
216+
load("@rules_python//python/private/pypi:whl_library_targets.bzl", "whl_library_targets")
217+
218+
whl_library_targets(
219+
dep_template = "@pypi//{name}:{target}",
220+
enable_implicit_namespace_pkgs = True,
221+
name = "foo.whl",
222+
dependencies = ["foo"],
223+
dependencies_by_platform = {"baz": ["bar"]},
224+
entry_points = {
225+
"foo": "bar.py",
226+
},
227+
group_deps = ["foo", "fox", "qux"],
228+
group_name = "qux",
229+
tags = ["tag1"],
230+
)
231+
"""
232+
actual = generate_whl_library_build_bazel(
233+
dep_template = "@pypi//{name}:{target}",
234+
name = "foo.whl",
235+
dependencies = ["foo"],
236+
dependencies_by_platform = {"baz": ["bar"]},
237+
entry_points = {
238+
"foo": "bar.py",
239+
},
240+
annotation = struct(
241+
enable_implicit_namespace_pkgs = True,
242+
),
243+
group_name = "qux",
244+
group_deps = ["foo", "fox", "qux"],
245+
tags = ["tag1"],
246+
)
247+
env.expect.that_str(actual.replace("@@", "@")).equals(want)
248+
249+
_tests.append(_test_enable_implicit_namespace_pkgs_annotation)
250+
251+
def _test_enable_implicit_namespace_pkgs_annotation_false(env):
252+
want = """\
253+
load("@rules_python//python/private/pypi:whl_library_targets.bzl", "whl_library_targets")
254+
255+
whl_library_targets(
256+
dep_template = "@pypi//{name}:{target}",
257+
enable_implicit_namespace_pkgs = False,
258+
name = "foo.whl",
259+
dependencies = ["foo"],
260+
dependencies_by_platform = {"baz": ["bar"]},
261+
entry_points = {
262+
"foo": "bar.py",
263+
},
264+
group_deps = ["foo", "fox", "qux"],
265+
group_name = "qux",
266+
tags = ["tag1"],
267+
)
268+
"""
269+
actual = generate_whl_library_build_bazel(
270+
dep_template = "@pypi//{name}:{target}",
271+
name = "foo.whl",
272+
dependencies = ["foo"],
273+
dependencies_by_platform = {"baz": ["bar"]},
274+
entry_points = {
275+
"foo": "bar.py",
276+
},
277+
annotation = struct(
278+
enable_implicit_namespace_pkgs = False,
279+
),
280+
group_name = "qux",
281+
group_deps = ["foo", "fox", "qux"],
282+
tags = ["tag1"],
283+
)
284+
env.expect.that_str(actual.replace("@@", "@")).equals(want)
285+
286+
_tests.append(_test_enable_implicit_namespace_pkgs_annotation_false)
287+
214288
def generate_whl_library_build_bazel_test_suite(name):
215289
"""Create the test suite.
216290

0 commit comments

Comments
 (0)