Skip to content

Commit

Permalink
hookutils: remove requirements_for_package
Browse files Browse the repository at this point in the history
Remove `requirements_for_package` hook utility, which was
used by `collect_all`. Consequently, the latter does not include
the top-level modules of metadata-declared requirements among the
returned hidden imports anymore.
  • Loading branch information
rokm committed Sep 21, 2023
1 parent 984545a commit 962ac8f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 39 deletions.
41 changes: 2 additions & 39 deletions PyInstaller/utils/hooks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1039,37 +1039,6 @@ def get_installer(module: str):
return None


def requirements_for_package(package_name: str):
hiddenimports = []

# Helpers for retrieving top-level imports for a dist, based on `importlib.metadata` code.
def _top_level_declared(dist):
return (dist.read_text('top_level.txt') or '').split()

def _top_level_inferred(dist):
return [
f.parts[0] if len(f.parts) > 1 else f.with_suffix('').name for f in (dist.files or []) if f.suffix == ".py"
]

# Filter requirements for the given dist based on their markers
requirements = [packaging.requirements.Requirement(req) for req in importlib_metadata.requires(package_name) or []]
requirements = [req.name for req in requirements if req.marker is None or req.marker.evaluate()]

for requirement in requirements:
# Obtain distribution for the requirement
try:
requirement_dist = importlib_metadata.distribution(requirement)
except importlib_metadata.PackageNotFoundError:
continue

# Obtain top-level imports for the distribution - either declared (top_level.txt in metadata) or inferred.
top_level_imports = _top_level_declared(requirement_dist) or _top_level_inferred(requirement_dist)
hiddenimports += top_level_imports

# Return de-duplicated list
return list(set(hiddenimports))


def collect_all(
package_name: str,
include_py_files: bool = True,
Expand Down Expand Up @@ -1101,8 +1070,7 @@ def collect_all(
- All data files, raw Python files (if **include_py_files**), and distribution metadata directories (if
applicable).
- All dynamic libraries as returned by :func:`collect_dynamic_libs`.
- All submodules of **package_name** and top-level imports of dependencies declared in the metadata
(if applicable).
- All submodules of **package_name**.
Typical use::
Expand All @@ -1112,7 +1080,7 @@ def collect_all(
binaries = collect_dynamic_libs(package_name)
hiddenimports = collect_submodules(package_name, on_error=on_error, filter=filter_submodules)

# `copy_metadata` and `requirements_for_package` require a dist name instead of importable/package name.
# `copy_metadata` requires a dist name instead of importable/package name.
# A namespace package might belong to multiple distributions, so process all of them.
pkg_to_dist = importlib_metadata.packages_distributions()
dist_names = set(pkg_to_dist.get(package_name, []))
Expand All @@ -1122,11 +1090,6 @@ def collect_all(
datas += copy_metadata(dist_name)
except Exception:
pass
# Find top-level imports from distribution's requirements
try:
hiddenimports += requirements_for_package(dist_name)
except Exception:
pass

return datas, binaries, hiddenimports

Expand Down
4 changes: 4 additions & 0 deletions news/7943.breaking.3.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Removed the ``requirements_for_package`` hook utility function, which
was primarily used by :func:`collect_all`; the latter does not include
the top-level modules of metadata-declared requirements among the
returned hidden imports anymore.

0 comments on commit 962ac8f

Please sign in to comment.