python.pypi

PyPI package dependency resolution and closure computation.

Module Contents

Classes

PyPILink

Link returned by PyPI simple API.

PyPILinksParser

HTML parser to parse links from the PyPI simple API.

PyPI

Interface to PyPI Simple API for package discovery and download.

PyPICandidate

Represent a PyPI package candidate for installation.

PyPIProvider

Class that should declared to instanciate a resolver (see resolvelib doc).

PyPIClosure

Represent a closure of Python package from PyPI.

Functions

get_pip_env(→ dict[str, str])

Return an environment used by pip to match requirements markers.

fetch_from_registry(→ dict[str, PyPILink])

Fetch packages currently in a registry.

Attributes

logger

HTTP_NOT_FOUND

python.pypi.logger
python.pypi.HTTP_NOT_FOUND = 404
exception python.pypi.PyPIError(message: str | list[str], origin: str | None = None)

Bases: e3.error.E3Error

Exception raised for PyPI operations errors.

python.pypi.get_pip_env(platform: str, python_version: packaging.version.Version) dict[str, str]

Return an environment used by pip to match requirements markers.

Parameters:
  • platform – a platform (e3 format)

  • python_version – the Python version to consider

Link returned by PyPI simple API.

property is_yanked: bool

Return True if the package is yanked.

property metadata_url: str

Return the metadata url.

as_dict() dict[str, None | bool | str]

Serialize the a PyPILink into a Python dict that can be dump as json.

classmethod from_dict(data: dict) PyPILink

Transform a generic dict into a PyPILink.

Parameters:

data – the dict to read

class python.pypi.PyPILinksParser(identifier: str, *, ignore_errors: bool = False)

Bases: html.parser.HTMLParser

HTML parser to parse links from the PyPI simple API.

handle_starttag(tag: str, attrs: list[tuple[str, str | None]]) None

See HTMLParser doc.

Parameters:
  • tag – the tag name

  • attrs – the tag attributes

class python.pypi.PyPI(pypi_url: str = 'https://pypi.org/', allowed_yanked: list[str] | None = None, allowed_prerelease: list[str] | None = None, cache_dir: str = 'pypi.cache')

Interface to PyPI Simple API for package discovery and download.

property pypi_cache_file: str

Get location of file containing result of pypi requests.

Fetch list of resource for a given Python package.

Parameters:
  • name – Python package name

  • ignore_errors – If True, ignore errors while parsing PyPi link.

Returns:

a list of dict containing the link to each resource along with some metadata

fetch_candidates(identifier: str, env: dict[str, str], extras: set[str]) list[PyPICandidate]

Return a list of candidates for a given package, env and list of extras.

Parameters:
  • identifier – a normalized python package name or internal identifier computed by the provider

  • env – the pip environment required

  • extras – set of needed extras

save_cache() None

Dump cache to disk.

load_cache() None

Load cache from disk.

class python.pypi.PyPICandidate(identifier: str, link: PyPILink, extras: set[str], cache_dir: str)

Represent a PyPI package candidate for installation.

property is_wheel: bool

Check if resource is a wheel.

property is_generic_wheel: bool

Return whether the package is a generic package.

If True then the wheel can be used on any Python 3.x version and on any platform.

download() str

Download the file in the PyPI cache.

Returns:

the location of the file

requirements(env: dict[str, str]) set[packaging.requirements.Requirement]

Return the list of requirements for the package.

Parameters:

env – the environment used to evaluate requirements markers

Returns:

a set of Requirement

is_compatible_with_platforms(platform_list: list[str]) bool

Check if the package is compatible with a list of platform.

Parameters:

platform_list – a list of platform name in e3 format (x86_64-linux, …)

Returns:

True if the package is compatible with any of the platform

is_compatible_with_cpython3(minor_version: int) bool

Check whether the package is compatible with a given python 3 version.

Parameters:

minor_version – to check compatibility with 3.10 set this param to 10

Returns:

True if compatible

__repr__() str

Return representation of Python package.

__str__() str

Return string representation of Python package.

class python.pypi.PyPIProvider(env: dict[str, str], pypi: PyPI)

Bases: resolvelib.providers.AbstractProvider

Class that should declared to instanciate a resolver (see resolvelib doc).

identify(requirement_or_candidate: packaging.requirements.Requirement | PyPICandidate) str

See resolvelib documentation.

Parameters:

requirement_or_candidate – a requirement or candidate to identify

get_preference(identifier: str, resolutions: collections.abc.Mapping[str, PyPICandidate], candidates: collections.abc.Mapping[str, collections.abc.Iterator[PyPICandidate]], information: collections.abc.Mapping[Any, collections.abc.Iterator[resolvelib.resolvers.RequirementInformation[Any, Any]]], backtrack_causes: collections.abc.Sequence[resolvelib.resolvers.RequirementInformation]) resolvelib.providers.Preference

See resolvelib documentation.

Parameters:
  • identifier – package identifier

  • resolutions – current resolved packages

  • candidates – available candidates

  • information – requirement information

  • backtrack_causes – causes for backtracking

find_matches(identifier: str, requirements: collections.abc.Mapping[str, collections.abc.Iterator[packaging.requirements.Requirement]], incompatibilities: collections.abc.Mapping[str, collections.abc.Iterator[PyPICandidate]]) resolvelib.structs.Matches

Return the list of candidates that match a given list of requirements.

Parameters:
  • identifier – package identifier

  • requirements – requirements to satisfy

  • incompatibilities – incompatible candidates

is_satisfied_by(requirement: packaging.requirements.Requirement, candidate: PyPICandidate) bool

See resolvelib documentation.

Parameters:
  • requirement – the requirement to check

  • candidate – the candidate to check

get_dependencies(candidate: PyPICandidate) collections.abc.Iterable[packaging.requirements.Requirement]

See resolvelib documentation.

Parameters:

candidate – the candidate to get dependencies for

class python.pypi.PyPIClosure(*, python3_version: str, platforms: list[str], cache_dir: str, pypi_url: str = 'https://pypi.org/', allowed_prerelease: list[str] | None = None, allowed_yanked: list[str] | None = None)

Represent a closure of Python package from PyPI.

add_wheel(filename: str) None

Introduce a local wheel into the closure.

Parameters:

filename – path to the wheel file

add_requirement(req: str | packaging.requirements.Requirement) None

Add a requirement in the closure.

Parameters:

req – requirement to add

file_closure() list[str]

File closure.

_requirements_closure() dict
requirements_closure() list[packaging.requirements.Requirement]

Get the closure of requirements.

Returns:

return a list of requirement that can be used as a lock file

__enter__() typing_extensions.Self

Enter context manager for PyPI closure.

__exit__(_type: type[BaseException] | None, _val: BaseException | None, _tb: types.TracebackType | None) None

Exit context manager.

Parameters:
  • _type – exception type

  • _val – exception value

  • _tb – exception traceback

python.pypi.fetch_from_registry(packages: collections.abc.Iterable[str], registry_url: str, *, log_missing_packages: bool = False, sanitize_packages: collections.abc.Callable[[list[PyPILink]], list[PyPILink]] = lambda x: ...) dict[str, PyPILink]

Fetch packages currently in a registry.

Parameters:
  • packages – The list of packages to look for.

  • registry_url – The URL to a python registry to use. If the protocol is not defined on the URL, https will be used by default.

  • log_missing_packages – if True, log missing packages as errors

  • sanitize_packages – function to sanitize the list of package links

Returns:

A filename to link mapping (dict).