shippinglabel_pypi

Shippinglabel extension for interacting with the Python Package Index (PyPI)..

See also

pypi-json, which provides some of the functionality from this module but with a reusable HTTP session and support for authentication with other endpoints (such as a private package repository).

This extension must be installed separately:

python3 -m pip install shippinglabel-pypi --user

__version__ = '0.4.0'

The version number of this extension.

Classes:

WheelPythonVersions(wheel_tag_map, …)

Return type of wheel_python_versions().

Functions:

bind_requirements(filename[, specifier, …])

Bind unbound requirements in the given file to the latest version on PyPI, and any later versions.

get_file_from_pypi(url, tmpdir)

Download the file with the given URL into the given (temporary) directory.

get_latest(pypi_name[, minimum_py_version])

Returns the version number of the latest (compatible) release on PyPI for the given project.

get_metadata(pypi_name)

Returns metadata for the given project on PyPI.

get_project_links(project_name)

Returns the web links for the given project.

get_pypi_releases(pypi_name)

Returns a dictionary mapping PyPI release versions to download URLs.

get_releases_with_digests(pypi_name)

Returns a dictionary mapping PyPI release versions to download URLs and the sha256sum of the file contents.

get_sdist_url(name, version[, strict])

Returns the URL of the project’s source distribution on PyPI.

get_wheel_tag_mapping(name, version)

Constructs a mapping of wheel tags to the PyPI download URL of the wheel with relevant tag.

get_wheel_url(name, version[, strict])

Returns the URL of one of the project’s wheels on PyPI.

wheel_python_versions(pypi_name)

Returns mappings given the supported Python versions for each version and wheel tag of the given project.

namedtuple WheelPythonVersions(wheel_tag_map, wheel_version_map)[source]

Bases: NamedTuple

Return type of wheel_python_versions().

Fields
  1.  wheel_tag_map (Dict[Tag, Set[Version]]) – Mapping of tags to a set of Versions

  2.  wheel_version_map (Dict[Optional[SpecifierSet], Set[Version]]) – Mapping of Python versions to a set of wheel Versions

__repr__()

Return a nicely formatted representation string

bind_requirements(filename, specifier='>=', normalize_func=<function 'normalize'>, minimum_py_version=None)[source]

Bind unbound requirements in the given file to the latest version on PyPI, and any later versions.

Parameters
  • filename (Union[str, Path, PathLike]) – The requirements.txt file to bind requirements in.

  • specifier (str) – The requirement specifier symbol to use. Default '>='.

  • normalize_func (Callable[[str], str]) – Function to use to normalize the names of requirements. Default shippinglabel.normalize().

  • minimum_py_version (Union[str, Version, None]) – Optionally, a minimum Python version that must be supported. Default None.

Return type

int

Returns

1 if the file was changed; 0 otherwise.

Changed in version 0.4.0: Added minimum_py_version argument.

get_file_from_pypi(url, tmpdir)[source]

Download the file with the given URL into the given (temporary) directory.

Parameters
  • url (Union[URL, str]) – The URL to download the file from.

  • tmpdir (Union[str, Path, PathLike]) – The (temporary) directory to store the downloaded file in.

get_latest(pypi_name, minimum_py_version=None)[source]

Returns the version number of the latest (compatible) release on PyPI for the given project.

Parameters
  • pypi_name (str)

  • minimum_py_version (Union[str, Version, None]) – Optionally, a minimum Python version that must be supported. Default None.

Changed in version 0.3.0: Added minimum_py_version option.

Raises
Return type

str

get_metadata(pypi_name)[source]

Returns metadata for the given project on PyPI.

Parameters

pypi_name (str)

Raises
Return type

Dict[str, Any]

Returns the web links for the given project.

The exact keys vary, but common keys include “Documentation” and “Issue Tracker”.

Note

The Home-Page field from Python core metadata is included under the Homepage key, if present. This matches the output parsed from PyPI for packages which are not installed.

Parameters

project_name (str)

Return type

MetadataMapping

get_pypi_releases(pypi_name)[source]

Returns a dictionary mapping PyPI release versions to download URLs.

Parameters

pypi_name (str) – The name of the project on PyPI.

Raises
Return type

Dict[str, List[str]]

get_releases_with_digests(pypi_name)[source]

Returns a dictionary mapping PyPI release versions to download URLs and the sha256sum of the file contents.

Parameters

pypi_name (str) – The name of the project on PyPI.

Raises
Return type

Dict[str, List[FileURL]]

get_sdist_url(name, version, strict=False)[source]

Returns the URL of the project’s source distribution on PyPI.

Parameters
  • name (str) – The name of the project on PyPI.

  • version (Union[str, int, Version])

  • strict (bool) – Causes a ValueError to be raised if no sdist is found, rather than retuning a wheel. Default False.

Attention

If no source distribution is found this function may return a wheel or “zip” sdist unless strict is True.

Return type

str

get_wheel_tag_mapping(name, version)[source]

Constructs a mapping of wheel tags to the PyPI download URL of the wheel with relevant tag.

This can be used alongside packaging.tags.sys_tags() to select the best wheel for the current platform.

Parameters
Return type

Tuple[Dict[Tag, URL], List[URL]]

Returns

A tuple containing the tag: url mapping, and a list of download URLs for non-wheel artifacts (e.g. sdists).

get_wheel_url(name, version, strict=False)[source]

Returns the URL of one of the project’s wheels on PyPI.

For finer control over which wheel the URL is for see the get_wheel_tag_mapping() function.

Parameters
  • name (str) – The name of the project on PyPI.

  • version (Union[str, int, Version])

  • strict (bool) – Causes a ValueError to be raised if no wheels are found, rather than retuning a wheel. Default False.

Attention

If no wheels are found this function may return an sdist unless strict is True.

Return type

str

wheel_python_versions(pypi_name)[source]

Returns mappings given the supported Python versions for each version and wheel tag of the given project.

Parameters

pypi_name (str)

Return type

WheelPythonVersions

New in version 0.3.0.