Skip to content
Closed
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
3 changes: 3 additions & 0 deletions news/py-info-packages.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(rules) Added {obj}`PyInfo.packages` with the normalized names of the PyPI
packages in the transitive closure of a target, e.g. so that a `py_test` can
tell whether it already depends on `coverage`.
6 changes: 5 additions & 1 deletion python/private/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,8 @@ def create_py_info(
implicit_pyc_files,
implicit_pyc_source_files,
imports,
venv_symlinks = []):
venv_symlinks = [],
packages = []):
"""Create PyInfo provider.

Args:
Expand All @@ -401,6 +402,8 @@ def create_py_info(
imports: depset of strings; the import path values to propagate.
venv_symlinks: {type}`list[VenvSymlinkEntry]` instances for
symlinks to create in the consuming binary's venv.
packages: {type}`list[str]` the normalized names of the PyPI packages
this target provides. See {obj}`PyInfo.packages`.

Returns:
A tuple of the PyInfo instance and a depset of the
Expand All @@ -409,6 +412,7 @@ def create_py_info(
"""
py_info = PyInfoBuilder.new()
py_info.venv_symlinks.add(venv_symlinks)
py_info.packages.add(packages)
py_info.direct_original_sources.add(original_sources)
py_info.direct_pyc_files.add(required_pyc_files)
py_info.direct_pyi_files.add(ctx.files.pyi_srcs)
Expand Down
30 changes: 29 additions & 1 deletion python/private/py_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,8 @@ def _PyInfo_init(
direct_pyi_files = depset(),
transitive_pyi_files = depset(),
type_checking_info = None,
venv_symlinks = depset()):
venv_symlinks = depset(),
packages = depset()):
_check_arg_type("transitive_sources", "depset", transitive_sources)

# Verify it's postorder compatible, but retain is original ordering.
Expand All @@ -399,6 +400,7 @@ def _PyInfo_init(

_check_arg_type("direct_pyi_files", "depset", direct_pyi_files)
_check_arg_type("transitive_pyi_files", "depset", transitive_pyi_files)
_check_arg_type("packages", "depset", packages)
if type_checking_info != None:
_check_arg_type("type_checking_info", "struct", type_checking_info)
return {
Expand All @@ -409,6 +411,7 @@ def _PyInfo_init(
"has_py2_only_sources": has_py2_only_sources,
"has_py3_only_sources": has_py3_only_sources,
"imports": imports,
"packages": packages,
"transitive_implicit_pyc_files": transitive_implicit_pyc_files,
"transitive_implicit_pyc_source_files": transitive_implicit_pyc_source_files,
"transitive_original_sources": transitive_original_sources,
Expand Down Expand Up @@ -494,6 +497,21 @@ A depset of import path strings to be added to the `PYTHONPATH` of executable
Python targets. These are accumulated from the transitive `deps`.
The order of the depset is not guaranteed and may be changed in the future. It
is recommended to use `default` order (the default).
""",
"packages": """
:type: depset[str]

The normalized names of the PyPI packages in the transitive closure of this
target, e.g. `google_cloud_storage`. The names are lowercased with runs of
`-`, `_` and `.` replaced by `_`, i.e. the same as the package name in the hub
repository that it would come from.

A {obj}`py_library` contributes its package name if its `data` contains a
`.dist-info/METADATA` file, which is the case for the targets created from
wheels by {obj}`pip.parse`.

:::{versionadded} VERSION_NEXT_FEATURE
:::
""",
"transitive_implicit_pyc_files": """
:type: depset[File]
Expand Down Expand Up @@ -634,6 +652,13 @@ def _PyInfoBuilder_typedef():
:type: DepsetBuilder[str]
:::

:::{field} packages
:type: DepsetBuilder[str]

::::{versionadded} VERSION_NEXT_FEATURE
::::
:::

:::{field} transitive_implicit_pyc_files
:type: DepsetBuilder[File]
:::
Expand Down Expand Up @@ -698,6 +723,7 @@ def _new_raw_py_info_builder(type_checking_info = None):
merge_targets = lambda *a, **k: _PyInfoBuilder_merge_targets(self, *a, **k),
merge_type_checking = lambda *a, **k: _PyInfoBuilder_merge_type_checking(self, *a, **k),
merge_uses_shared_libraries = lambda *a, **k: _PyInfoBuilder_merge_uses_shared_libraries(self, *a, **k),
packages = builders.DepsetBuilder(),
set_has_py2_only_sources = lambda *a, **k: _PyInfoBuilder_set_has_py2_only_sources(self, *a, **k),
set_has_py3_only_sources = lambda *a, **k: _PyInfoBuilder_set_has_py3_only_sources(self, *a, **k),
set_uses_shared_libraries = lambda *a, **k: _PyInfoBuilder_set_uses_shared_libraries(self, *a, **k),
Expand Down Expand Up @@ -890,6 +916,7 @@ def _merge_py_info_fields(self, transitive, *, direct = []):
self.transitive_pyc_files.add(info.transitive_pyc_files)
self.transitive_pyi_files.add(info.transitive_pyi_files)
self.venv_symlinks.add(info.venv_symlinks)
self.packages.add(info.packages)

def _PyInfoBuilder_merge_all(self, transitive, *, direct = []):
"""Merge other PyInfos into this PyInfo.
Expand Down Expand Up @@ -1009,6 +1036,7 @@ def _build_py_info_fields(self, type_checking_info = None):
has_py2_only_sources = self._has_py2_only_sources[0],
has_py3_only_sources = self._has_py3_only_sources[0],
imports = self.imports.build(),
packages = self.packages.build(),
transitive_sources = self.transitive_sources.build(),
uses_shared_libraries = self._uses_shared_libraries[0],
direct_implicit_pyc_files = self.direct_implicit_pyc_files.build(),
Expand Down
15 changes: 8 additions & 7 deletions python/private/py_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,12 @@ def py_library_impl(ctx):
runfiles.add(collect_runfiles(ctx))
runfiles = runfiles.build(ctx)

imports = []
venv_symlinks = []

imports, venv_symlinks = _get_imports_and_venv_symlinks(ctx)
package, version_str = get_package_and_version(ctx.files.data)
imports, venv_symlinks = _get_imports_and_venv_symlinks(
ctx,
package = package,
version_str = version_str,
)

cc_info = collect_cc_info(ctx)
py_info, builtins_py_info = create_py_info(
Expand All @@ -194,6 +196,7 @@ def py_library_impl(ctx):
implicit_pyc_source_files = implicit_pyc_source_files,
imports = imports,
venv_symlinks = venv_symlinks,
packages = [package] if package else [],
)

providers = [
Expand Down Expand Up @@ -263,12 +266,10 @@ def get_package_and_version(data_files):
version.normalize(version_str), # will have no dashes either
)

def _get_imports_and_venv_symlinks(ctx):
def _get_imports_and_venv_symlinks(ctx, *, package, version_str):
imports = depset()
venv_symlinks = []
if VenvsSitePackages.is_enabled(ctx):
package, version_str = get_package_and_version(ctx.files.data)

# NOTE: Already a list, but buildifier thinks its a depset and
# adds to_list() calls later.
imports = list(ctx.attr.imports)
Expand Down
54 changes: 54 additions & 0 deletions tests/base_rules/py_library/py_library_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,60 @@ def _test_pyi_srcs_propagated_via_deps_impl(env, target):

_tests.append(_test_pyi_srcs_propagated_via_deps)

def _test_packages_empty_without_dist_info(name, config):
rt_util.helper_target(
config.rule,
name = name + "_subject",
srcs = ["lib.py"],
data = ["data.txt"],
)
analysis_test(
name = name,
target = name + "_subject",
impl = _test_packages_empty_without_dist_info_impl,
)

def _test_packages_empty_without_dist_info_impl(env, target):
info = env.expect.that_target(target).provider(
PyInfo,
factory = py_info_subject,
)
info.packages().contains_exactly([])

_tests.append(_test_packages_empty_without_dist_info)

def _test_packages_from_dist_info(name, config):
# Like the targets that `pip.parse` creates from wheels.
rt_util.helper_target(
config.rule,
name = name + "_subject",
srcs = ["site-packages/foo/__init__.py"],
data = ["site-packages/Foo.Bar-1.0.dist-info/METADATA"],
imports = ["site-packages"],
deps = [name + "_dep"],
)
rt_util.helper_target(
config.rule,
name = name + "_dep",
srcs = ["dep/site-packages/dep/__init__.py"],
data = ["dep/site-packages/dep_pkg-2.0.dist-info/METADATA"],
imports = ["dep/site-packages"],
)
analysis_test(
name = name,
target = name + "_subject",
impl = _test_packages_from_dist_info_impl,
)

def _test_packages_from_dist_info_impl(env, target):
info = env.expect.that_target(target).provider(
PyInfo,
factory = py_info_subject,
)
info.packages().contains_exactly(["foo_bar", "dep_pkg"])

_tests.append(_test_packages_from_dist_info)

def py_library_test_suite(name):
config = struct(rule = py_library, base_test_rule = py_library)
native.test_suite(
Expand Down
11 changes: 11 additions & 0 deletions tests/support/py_info_subject.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def _new_py_info_subject(info, *, meta):
has_py2_only_sources = lambda *a, **k: _py_info_subject_has_py2_only_sources(self, *a, **k),
has_py3_only_sources = lambda *a, **k: _py_info_subject_has_py3_only_sources(self, *a, **k),
imports = lambda *a, **k: _py_info_subject_imports(self, *a, **k),
packages = lambda *a, **k: _py_info_subject_packages(self, *a, **k),
transitive_original_sources = lambda *a, **k: _py_info_subject_transitive_original_sources(self, *a, **k),
transitive_pyc_files = lambda *a, **k: _py_info_subject_transitive_pyc_files(self, *a, **k),
transitive_pyi_files = lambda *a, **k: _py_info_subject_transitive_pyi_files(self, *a, **k),
Expand Down Expand Up @@ -120,6 +121,16 @@ def _py_info_subject_imports(self):
meta = self.meta.derive("imports()"),
)

def _py_info_subject_packages(self):
"""Returns a `CollectionSubject` for the `packages` attribute.

Method: PyInfoSubject.packages
"""
return subjects.collection(
self.actual.packages.to_list(),
meta = self.meta.derive("packages()"),
)

def _py_info_subject_transitive_original_sources(self):
"""Returns a `DepsetFileSubject` for the `transitive_original_sources` attribute.

Expand Down
Loading