Skip to content

Commit

Permalink
Fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanking13 committed Sep 22, 2023
1 parent c66d3f5 commit 89d9b95
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 27 deletions.
1 change: 0 additions & 1 deletion micropip/_compat_not_in_pyodide.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from io import BytesIO
from pathlib import Path
from typing import IO, Any
from zipfile import ZipFile

REPODATA_PACKAGES: dict[str, dict[str, Any]] = {}

Expand Down
33 changes: 18 additions & 15 deletions micropip/metadata.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""
This is a stripped down version of pip._vendor.pkg_resources.DistInfoDistribution
"""
import re
import zipfile
from collections.abc import Iterable
from pathlib import Path

from packaging.requirements import Requirement
from packaging.utils import canonicalize_name
import re
from collections.abc import Sequence
import zipfile


def safe_name(name):
Expand Down Expand Up @@ -69,6 +70,7 @@ class Metadata:
"""
Represents a metadata file in a wheel
"""

PKG_INFO = "METADATA"
REQUIRES_DIST = "Requires-Dist:"
PROVIDES_EXTRA = "Provides-Extra:"
Expand All @@ -78,46 +80,47 @@ def __init__(self, path: Path | zipfile.Path):
self.deps = self._compute_dependencies()

def _parse_requirement(self, line: str) -> Requirement:
line = line[len(self.REQUIRES_DIST):]
line = line[len(self.REQUIRES_DIST) :]
if " #" in line:
line = line[: line.find(" #")]

return Requirement(line.strip())

def _compute_dependencies(self) -> dict[str, frozenset[str]]:
def _compute_dependencies(self) -> dict[str | None, frozenset[Requirement]]:
"""
Compute the dependencies of the metadata file
"""
deps: dict[str, frozenset[str]] = {}
deps: dict[str | None, frozenset[Requirement]] = {}
reqs: list[Requirement] = []
extras: list[str] = []

def reqs_for_extra(extra: str) -> Requirement:
def reqs_for_extra(extra: str | None) -> Iterable[Requirement]:
environment = {"extra": extra} if extra else None
for req in reqs:
if not req.marker or req.marker.evaluate({"extra": extra}):
if not req.marker or req.marker.evaluate(environment):
yield req

lines = self.path.read_text(encoding="utf-8").splitlines()
for line in lines:
if line.startswith(self.REQUIRES_DIST):
reqs.append(self._parse_requirement(line))
elif line.startswith(self.PROVIDES_EXTRA):
extras.append(line[len(self.PROVIDES_EXTRA):].strip())
extras.append(line[len(self.PROVIDES_EXTRA) :].strip())

deps[None] = frozenset(reqs_for_extra(None))
for extra in extras:
deps[safe_extra(extra)] = frozenset(reqs_for_extra(extra)) - deps[None]

return deps
def requires(self, extras: Sequence[str]=()) -> list[Requirement]:

def requires(self, extras: Iterable[str] = ()) -> list[Requirement]:
"""List of Requirements needed for this distro if `extras` are used"""
deps = []
deps: list[Requirement] = []

deps.extend(self.deps.get(None, ()))
for ext in extras:
try:
deps.extend(self.deps[safe_extra(ext)])
except KeyError:
raise KeyError(f"Unknown extra {ext!r}")
raise KeyError(f"Unknown extra {ext!r}") from None
return deps

2 changes: 1 addition & 1 deletion micropip/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __post_init__(self):

async def gather_requirements(
self,
requirements: list[str],
requirements: list[str] | list[Requirement],
) -> None:
requirement_promises = []
for requirement in requirements:
Expand Down
9 changes: 7 additions & 2 deletions micropip/wheelinfo.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import asyncio
import hashlib
import json
import zipfile
from dataclasses import dataclass
from pathlib import Path
from typing import IO, Any
from urllib.parse import ParseResult, urlparse
import zipfile

from packaging.requirements import Requirement
from packaging.tags import Tag
Expand Down Expand Up @@ -123,10 +123,15 @@ async def download(self, fetch_kwargs: dict[str, Any]):
metadata_path = wheel_dist_info_dir(zf, self.name) + "/" + Metadata.PKG_INFO
self._metadata = Metadata(zipfile.Path(zf, metadata_path))

def requires(self, extras: set[str]) -> list[str]:
def requires(self, extras: set[str]) -> list[Requirement]:
"""
Get a list of requirements for the wheel.
"""
if self._metadata is None:
raise RuntimeError(
"Micropip internal error: attempted to get requirements before downloading the wheel?"
)

requires = self._metadata.requires(extras)
self._requires = requires
return requires
Expand Down
24 changes: 16 additions & 8 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
from pathlib import Path

import pytest

TEST_METADATA_DIR = Path(__file__).parent / "test_data" / "metadata"


@pytest.mark.parametrize(
"metadata_path, extras, expected",
[
(
"boto3-1.28.51-py3-none-any.whl.metadata",
(),
["botocore", "jmespath", "s3transfer"]
["botocore", "jmespath", "s3transfer"],
),
(
"requests-2.31.0-py3-none-any.whl.metadata",
(),
["certifi", "urllib3", "charset-normalizer", "idna"]
["certifi", "urllib3", "charset-normalizer", "idna"],
),
(
"requests-2.31.0-py3-none-any.whl.metadata",
("socks", "use_chardet_on_py3",),
["certifi", "urllib3", "charset-normalizer", "idna", "PySocks", "chardet"]
(
"socks",
"use_chardet_on_py3",
),
["certifi", "urllib3", "charset-normalizer", "idna", "PySocks", "chardet"],
),
]
],
)
def test_Metadata_requires(metadata_path, extras, expected):
from micropip.metadata import Metadata

m = Metadata(TEST_METADATA_DIR / metadata_path)

reqs = m.requires(extras)
reqs_set = set([r.name for r in reqs])
assert reqs_set == set(expected)
Expand All @@ -53,7 +58,10 @@ def test_Metadata_marker():
markers = {r.name: str(r.marker) for r in reqs}

assert "brotli" in markers
assert markers["brotli"] == 'platform_python_implementation == "CPython" and extra == "brotli"'
assert (
markers["brotli"]
== 'platform_python_implementation == "CPython" and extra == "brotli"'
)

assert "zstandard" in markers
assert markers["zstandard"] == 'extra == "zstd"'
Expand All @@ -69,4 +77,4 @@ def test_Metadata_extra_of_requires():
reqs_set = {r.name: r.extras for r in reqs}

assert "botocore" in reqs_set
assert reqs_set["botocore"] == {"crt"}
assert reqs_set["botocore"] == {"crt"}

0 comments on commit 89d9b95

Please sign in to comment.