Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Options to fix dependencies of manually loaded packages #79

Merged
merged 14 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
105 changes: 105 additions & 0 deletions micropip/_commands/check_package_dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import importlib.metadata
import json

from packaging.requirements import Requirement
from packaging.utils import canonicalize_name

from .._compat import REPODATA_PACKAGES


def check_package_dependencies(
package_name, *, extras=None, fix_deps=False, recursive=True
):
"""Check and optionally fix the list of dependencies for this package

If you have manually installed a package and dependencies from wheels,
the dependencies will not be correctly setup in the package list
or the pyodide lockfile generated by freezing. This method checks
if the dependencies are correctly set in the package list, and if
fix_deps is True, it will add missing dependencies.

Parameters
----------
package_name (string):
The name of the package to check.

extras (list):
List of extras for this package.

fix_deps (boolean):
If this is True, any missing dependencies that are in
the current package list will be added to the current
package.

recursive (boolean):
If this is True, dependencies of dependencies will be checked.
joemarshall marked this conversation as resolved.
Show resolved Hide resolved

Returns
-------
dict:
Dictionary mapping package name to a list of missing dependencies.
If the package has no missing dependencies, an empty list is returned.
"""
if package_name in REPODATA_PACKAGES:
# don't check things that are in original repository
return {}

dist = importlib.metadata.Distribution.from_name(package_name)
url = dist.read_text("PYODIDE_URL")

# If it wasn't installed with micropip / pyodide, then we
# can't do anything with it.
assert url is not None

# Get current list of pyodide requirements
requires = dist.read_text("PYODIDE_REQUIRES")

if requires:
depends = json.loads(requires)
else:
depends = []
joemarshall marked this conversation as resolved.
Show resolved Hide resolved

missing = []
all_missing = {}
joemarshall marked this conversation as resolved.
Show resolved Hide resolved

package_requires = dist.requires
if package_requires is None:
# no dependencies - we're good to go
return {}
joemarshall marked this conversation as resolved.
Show resolved Hide resolved

if extras is None:
extras = [None]
else:
extras.append(None)
for r in package_requires:
req = Requirement(r)
req_extras = req.extras
req_marker = req.marker
req_name = canonicalize_name(req.name)
needs_requirement = False
if req_marker is not None:
for e in extras:
if req_marker.evaluate(None if e is None else {"extra": e}):
needs_requirement = True
break
else:
needs_requirement = True

if needs_requirement:
if recursive:
all_missing = all_missing | check_package_dependencies(
req_name, extras=list(req_extras), fix_deps=fix_deps
)

if req_name not in depends:
missing.append(req_name)
depends.append(req_name)

if fix_deps and len(missing) > 0:
# write updated depends to PYODIDE_DEPENDS
(dist._path / "PYODIDE_REQUIRES").write_text(
json.dumps(sorted(x for x in depends))
)
if len(missing) > 0:
all_missing[package_name] = missing
return all_missing
10 changes: 8 additions & 2 deletions micropip/_commands/freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from packaging.utils import canonicalize_name

from .._compat import REPODATA_INFO, REPODATA_PACKAGES
from .check_package_dependencies import check_package_dependencies


def freeze() -> str:
Expand All @@ -20,7 +21,6 @@ def freeze() -> str:
You can use your custom lock file by passing an appropriate url to the
``lockFileURL`` of :js:func:`~globalThis.loadPyodide`.
"""

packages = deepcopy(REPODATA_PACKAGES)
for dist in importlib.metadata.distributions():
name = dist.name
Expand All @@ -37,7 +37,13 @@ def freeze() -> str:
depends = json.loads(requires)
else:
depends = []

if dist.requires is not None and len(dist.requires) > 0:
joemarshall marked this conversation as resolved.
Show resolved Hide resolved
# no calculated dependencies yet - fix them before outputting
# the final frozen list
check_package_dependencies(name, fix_deps=True)
requires = dist.read_text("PYODIDE_REQUIRES")
if requires is not None:
depends = json.loads(requires)
pkg_entry: dict[str, Any] = dict(
name=name,
version=version,
Expand Down