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

MAINT passthrough the lockfile object from the manager into freeze function #212

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
78 changes: 0 additions & 78 deletions micropip/_utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import functools
import json
from importlib.metadata import Distribution
from pathlib import Path
from sysconfig import get_config_var, get_platform

from ._compat import LOCKFILE_PACKAGES
from ._vendored.packaging.src.packaging.requirements import (
InvalidRequirement,
Requirement,
Expand Down Expand Up @@ -202,82 +200,6 @@ def platform_to_version(platform: str) -> str:
raise ValueError(f"Wheel interpreter version '{tag.interpreter}' is not supported.")


def fix_package_dependencies(
package_name: str, *, extras: list[str | None] | None = None
) -> None:
"""Check and 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 will
add missing dependencies.

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

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

"""
if package_name in LOCKFILE_PACKAGES:
# don't check things that are in original repository
return

dist = Distribution.from_name(package_name)

package_requires = dist.requires
if package_requires is None:
# no dependencies - we're good to go
return

url = dist.read_text("PYODIDE_URL")

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

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

if requires:
depends = json.loads(requires)
else:
depends = []

if extras is None:
extras = [None]
else:
extras = extras + [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:
fix_package_dependencies(req_name, extras=list(req_extras))

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

# write updated depends to PYODIDE_DEPENDS
(get_dist_info(dist) / "PYODIDE_REQUIRES").write_text(
json.dumps(sorted(x for x in depends))
)


def validate_constraints(
constraints: list[str] | None,
environment: dict[str, str] | None = None,
Expand Down
107 changes: 98 additions & 9 deletions micropip/freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import json
from collections.abc import Iterator
from copy import deepcopy
from importlib.metadata import Distribution
from typing import Any

from ._utils import fix_package_dependencies
from ._utils import get_dist_info
from ._vendored.packaging.src.packaging.requirements import Requirement
from ._vendored.packaging.src.packaging.utils import canonicalize_name


Expand All @@ -18,7 +20,7 @@
lockfile_packages: dict[str, dict[str, Any]], lockfile_info: dict[str, str]
) -> dict[str, Any]:
packages = deepcopy(lockfile_packages)
packages.update(load_pip_packages())
packages.update(load_pip_packages(lockfile_packages))

# Sort
packages = dict(sorted(packages.items()))
Expand All @@ -28,11 +30,13 @@
}


def load_pip_packages() -> Iterator[tuple[str, dict[str, Any]]]:
return map(
package_item,
filter(is_valid, map(load_pip_package, importlib.metadata.distributions())),
)
def load_pip_packages(
lockfile_packages: dict[str, dict[str, Any]]
) -> Iterator[tuple[str, dict[str, Any]]]:
distributions = importlib.metadata.distributions()
pip_packages = [load_pip_package(dist, lockfile_packages) for dist in distributions]

return (package_item(pkg) for pkg in pip_packages if is_valid(pkg))


def package_item(entry: dict[str, Any]) -> tuple[str, dict[str, Any]]:
Expand All @@ -43,15 +47,18 @@
return entry["file_name"] is not None


def load_pip_package(dist: importlib.metadata.Distribution) -> dict[str, Any]:
def load_pip_package(
dist: importlib.metadata.Distribution,
lockfile_packages: dict[str, dict[str, Any]],
) -> dict[str, Any]:
name = dist.name
version = dist.version
url = dist.read_text("PYODIDE_URL")
sha256 = dist.read_text("PYODIDE_SHA256")
imports = (dist.read_text("top_level.txt") or "").split()
requires = dist.read_text("PYODIDE_REQUIRES")
if not requires:
fix_package_dependencies(name)
fix_package_dependencies(name, lockfile_packages)
requires = dist.read_text("PYODIDE_REQUIRES")
depends = json.loads(requires or "[]")

Expand All @@ -64,3 +71,85 @@
imports=imports,
depends=depends,
)


def fix_package_dependencies(
package_name: str,
lockfile_packages: dict[str, dict[str, Any]] | None = None,
*,
extras: list[str | None] | None = None,
) -> None:
"""Check and 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 will
add missing dependencies.

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

lockfile_packages (dict):
The lockfile packages to check against.

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

"""
if lockfile_packages and package_name in lockfile_packages:
# don't check things that are in original repository
return

Check warning on line 104 in micropip/freeze.py

View check run for this annotation

Codecov / codecov/patch

micropip/freeze.py#L104

Added line #L104 was not covered by tests

dist = Distribution.from_name(package_name)

package_requires = dist.requires
if package_requires is None:
# no dependencies - we're good to go
return

url = dist.read_text("PYODIDE_URL")

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

Check warning on line 118 in micropip/freeze.py

View check run for this annotation

Codecov / codecov/patch

micropip/freeze.py#L118

Added line #L118 was not covered by tests

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

if requires:
depends = json.loads(requires)

Check warning on line 124 in micropip/freeze.py

View check run for this annotation

Codecov / codecov/patch

micropip/freeze.py#L124

Added line #L124 was not covered by tests
else:
depends = []

if extras is None:
extras = [None]
else:
extras = extras + [None]

Check warning on line 131 in micropip/freeze.py

View check run for this annotation

Codecov / codecov/patch

micropip/freeze.py#L131

Added line #L131 was not covered by tests
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

Check warning on line 142 in micropip/freeze.py

View check run for this annotation

Codecov / codecov/patch

micropip/freeze.py#L139-L142

Added lines #L139 - L142 were not covered by tests
else:
needs_requirement = True

if needs_requirement:
fix_package_dependencies(req_name, extras=list(req_extras))

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

# write updated depends to PYODIDE_DEPENDS
(get_dist_info(dist) / "PYODIDE_REQUIRES").write_text(
json.dumps(sorted(x for x in depends))
)