Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions python/private/pypi/namespace_pkgs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,32 @@ def get_files(*, srcs, ignored_dirnames = [], root = None):

return sorted([d for d in dirs if d not in ignored])

def create_inits(**kwargs):
def create_inits(*, srcs, ignored_dirnames = [], root = None, copy_file = copy_file, **kwargs):
"""Create init files and return the list to be included `py_library` srcs.

Args:
**kwargs: passed to {obj}`get_files`.
srcs: {type}`src` a list of files to be passed to {bzl:obj}`py_library`
as `srcs` and `data`. This is usually a result of a {obj}`glob`.
ignored_dirnames: {type}`str` a list of patterns to ignore.
root: {type}`str` the prefix to use as the root.
copy_file: the `copy_file` rule to copy files in build context.
**kwargs: passed to {obj}`copy_file`.

Returns:
{type}`list[str]` to be included as part of `py_library`.
"""
srcs = []
for out in get_files(**kwargs):
ret = []
for i, out in enumerate(get_files(srcs = srcs, ignored_dirnames = ignored_dirnames, root = root)):
src = "{}/__init__.py".format(out)
srcs.append(srcs)
ret.append(src)

copy_file(
name = "_cp_{}_namespace".format(out),
# For the target name, use a number instead of trying to convert an output
# path into a valid label.
name = "_cp_{}_namespace".format(i),
src = _TEMPLATE,
out = src,
**kwargs
)

return srcs
return ret
41 changes: 40 additions & 1 deletion tests/pypi/namespace_pkgs/namespace_pkgs_tests.bzl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
""

load("@rules_testing//lib:analysis_test.bzl", "test_suite")
load("//python/private/pypi:namespace_pkgs.bzl", "get_files") # buildifier: disable=bzl-visibility
load("//python/private/pypi:namespace_pkgs.bzl", "create_inits", "get_files") # buildifier: disable=bzl-visibility

_tests = []

Expand Down Expand Up @@ -160,6 +160,45 @@ def test_skips_ignored_directories(env):

_tests.append(test_skips_ignored_directories)

def _test_create_inits(env):
srcs = [
"nested/root/foo/bar/biz.py",
"nested/root/foo/bee/boo.py",
"nested/root/foo/buu/__init__.py",
"nested/root/foo/buu/bii.py",
]
copy_file_calls = []
template = Label("//python/private/pypi:namespace_pkg_tmpl.py")

got = create_inits(
srcs = srcs,
root = "nested/root",
copy_file = lambda **kwargs: copy_file_calls.append(kwargs),
)
env.expect.that_collection(got).contains_exactly([
call["out"]
for call in copy_file_calls
])
env.expect.that_collection(copy_file_calls).contains_exactly([
{
"name": "_cp_0_namespace",
"out": "nested/root/foo/__init__.py",
"src": template,
},
{
"name": "_cp_1_namespace",
"out": "nested/root/foo/bar/__init__.py",
"src": template,
},
{
"name": "_cp_2_namespace",
"out": "nested/root/foo/bee/__init__.py",
"src": template,
},
])

_tests.append(_test_create_inits)

def namespace_pkgs_test_suite(name):
test_suite(
name = name,
Expand Down