repomatic.pypi module¶
PyPI API client for package metadata lookups.
Domain-specific query functions, over the shared client of repomatic.http,
used by repomatic.lint_changelog (release dates, yanked status),
repomatic.deps.pins (release candidates),
repomatic.deps.report (source repository discovery for release notes)
and repomatic.init.local_edits (the wheel of a release).
Also the home of what counts as the public index at all
(PYPI_INDEX_HOSTS), which the shippability gate reads.
- repomatic.pypi.PYPI_INDEX_HOSTS = frozenset({'pypi.org', 'www.pypi.org'})¶
Hosts a package may be resolved from and still count as published.
Anything else is a private index, a staging index (TestPyPI lives on
test.pypi.org, deliberately absent) or a proxy: a user runningpip installoruvxagainst the default index reaches none of them, so a dependency pinned there is no more installable than one pinned to a git branch.
- repomatic.pypi.is_pypi_url(url)[source]¶
Whether url points at the public Python Package Index.
- Parameters:
url (
str) – An index or registry URL.- Return type:
- Returns:
Truewhen its host is one ofPYPI_INDEX_HOSTS.
- repomatic.pypi.PYPI_API_URL = 'https://pypi.org/pypi/{package}/json'¶
PyPI JSON API URL for fetching all release metadata for a package.
- repomatic.pypi.PYPI_PACKAGE_URL = 'https://pypi.org/project/{package}/'¶
PyPI project homepage URL for a package (no version pinned).
- repomatic.pypi.PYPI_PROJECT_URL = 'https://pypi.org/project/{package}/{version}/'¶
PyPI project page URL for a specific version.
- repomatic.pypi.PYPI_VERSION_API_URL = 'https://pypi.org/pypi/{package}/{version}/json'¶
PyPI JSON API URL for the metadata of one version of a package.
- repomatic.pypi.SDIST_SUFFIX = '.tar.gz'¶
Extension of a source distribution, after its
{name}-{version}stem.
- repomatic.pypi.PROPAGATION_ATTEMPTS = 20¶
Lookups
wait_for_release()makes before it gives up.
- repomatic.pypi.PROPAGATION_DELAY = 15¶
Seconds
wait_for_release()waits between two lookups.With
PROPAGATION_ATTEMPTS, the wait lasts about five minutes, which covers the propagation delay of PyPI’s index and CDN.
- repomatic.pypi.PYPI_SIMPLE_URL = 'https://pypi.org/simple/{package}/'¶
PyPI Simple API page of a package, which lists every file of every release.
- repomatic.pypi.PYPI_SIMPLE_JSON = 'application/vnd.pypi.simple.v1+json'¶
Media type of the JSON form of the Simple API, from PEP 691.
In that form, each file carries a
provenancekey, from PEP 740: thePYPI_PROVENANCE_URLof its attestations, ornullfor a file uploaded without any. The JSON API has no such key, so this is the one request that answers for every file of a release.
- repomatic.pypi.PYPI_PROVENANCE_URL = 'https://pypi.org/integrity/{package}/{version}/{filename}/provenance'¶
PyPI integrity API endpoint exposing PEP 740 attestation bundles for a file.
The response includes a
publisherobject per bundle that names the OIDC identity used to upload (kind, repository, workflow filename, environment). This is the only public surface where the OIDCjob_workflow_refclaim is observable: project-level Trusted Publisher settings live behind the owner-only/manage/project/<name>/settings/publishing/page.
- repomatic.pypi.PYPI_TRUSTED_PUBLISHER_SETTINGS_URL = 'https://pypi.org/manage/project/{package}/settings/publishing/'¶
Owner-only page where Trusted Publisher entries are registered.
- repomatic.pypi.PYPI_TRUSTED_PUBLISHER_WORKFLOW = 'release.yaml'¶
Workflow filename each downstream registers as the Trusted Publisher.
The caller-side
publish-pypijob is appended torelease.yamlin every downstream repo (reshaped from the canonical entry byrepomatic.github.workflow_sync._render_publish_pypi_job), and the composite action it invokes inherits the calling job’s OIDC context. The OIDCjob_workflow_refclaim therefore names this file: that is what the PyPI Trusted Publisher entry must match.
- repomatic.pypi.pypi_trusted_publisher_settings_url(package, *, owner=None, repository=None, workflow_filename=None, environment=None)[source]¶
Build the PyPI Trusted Publisher settings page URL for a project.
Without keyword arguments, returns the bare settings URL. When any GitHub publisher field is provided, appends the query string PyPI’s settings page consumes to activate the GitHub tab and pre-populate the form: see the
manage_project_oidc_publishers_prefillview in pypi/warehouse.- Parameters:
package (
str) – PyPI project name.owner (
str|None) – GitHub owner (user or org) prefilled in the form.repository (
str|None) – GitHub repository name prefilled in the form.workflow_filename (
str|None) – Workflow filename prefilled in the form (e.g.,PYPI_TRUSTED_PUBLISHER_WORKFLOW).environment (
str|None) – GitHub Actions environment name prefilled in the form.
- Return type:
- Returns:
The settings URL, optionally with a
?provider=github&…suffix.
- class repomatic.pypi.PyPIRelease(date, yanked, package, yanked_reason='', uploaded_at=None, requires_python='')[source]¶
Bases:
NamedTupleRelease metadata for a single version from PyPI.
Create new instance of PyPIRelease(date, yanked, package, yanked_reason, uploaded_at, requires_python)
- package: str¶
PyPI package name this release was fetched from.
Needed for projects that were renamed: older versions live under a former package name and their PyPI URLs must point to that name, not the current one.
- yanked_reason: str¶
Why the release was yanked, empty when PyPI records no reason.
PyPI stores the reason per file and accepts a yank with none at all, so this carries the first non-empty one across the version’s files.
- repomatic.pypi.pypi_release_dates(package, *, force_refresh=False)[source]¶
Get upload dates, yanked status and
requires-pythonfor all versions.Fetches the package metadata in a single API call. For each version, selects the earliest upload time across all distribution files as the canonical release date. A version is considered yanked only if all of its files are yanked, and carries the first yank reason any of them records, and the first
requires-pythonany of them declares.- Parameters:
- Return type:
- Returns:
Dict mapping version strings to
PyPIReleasetuples. Empty dict if the package is not found or the request fails.
- repomatic.pypi.get_source_url(package)[source]¶
Discover the GitHub repository URL for a PyPI package.
Queries the PyPI JSON API and scans
project_urlsfor keys that typically point to a source repository on GitHub, then reduces the winner to itshttps://github.com/owner/reporoot. An entry often points inside the repository (/issues,/releases,/blob/main/changelog.md), and a caller deriving an API slug from it would otherwise ask the releases API forrepo/issues, which answers 404.
- class repomatic.pypi.TrustedPublisher(kind, repository, workflow, environment)[source]¶
Bases:
NamedTupleOIDC publisher metadata extracted from a PyPI provenance bundle.
Create new instance of TrustedPublisher(kind, repository, workflow, environment)
- repomatic.pypi.get_latest_release_file(package)[source]¶
Return
(version, filename)for the latest non-yanked release on PyPI.Picks the version with the most recent earliest-upload time and returns a representative distribution file from that version. Wheels are preferred over sdists since wheels are guaranteed to exist for any package built with modern tooling.
Two releases uploaded on the same day are ordered by PEP 440, not by the version string: a raw string comparison sorts
1.9.0above1.10.0and would return the older of the two as the latest. Versions PEP 440 cannot parse are skipped, since nothing can rank them.
- repomatic.pypi.get_trusted_publishers(package, version, filename)[source]¶
Fetch PEP 740 provenance for a file and extract publisher entries.
Calls
PYPI_PROVENANCE_URLand parses theattestation_bundlesarray. Each bundle’spublisherobject names the OIDC identity that uploaded the file.- Parameters:
- Return type:
- Returns:
List of
TrustedPublisherentries (possibly empty when provenance exists but no bundles are present), orNonewhen the endpoint returns 404 or any network/parse error occurs (signal that no provenance is available rather than that none was registered).
- class repomatic.pypi.ReleaseProvenance(known, files=0, attested_files=0, publisher='', workflow='')[source]¶
Bases:
objectWhat PyPI records about how the files of one release were published.
A file uploaded through trusted publishing carries PEP 740 provenance: attestations, signed through Sigstore, naming the repository and the workflow that uploaded it. A file uploaded with an API token, by hand or by a token thief, carries none, even beside attested files of the same release.
- known: bool¶
Whether PyPI answered.
Falsewhen a lookup failed: the release may or may not carry provenance.
- publisher: str = ''¶
Repository of the trusted publisher, like
pypa/packaging. Empty when no file carries provenance, or when its provenance names no publisher.
- repomatic.pypi.file_provenance(package)[source]¶
Read which files of package carry PEP 740 provenance.
One read of the Simple API answers for every file of every release, so a caller that looks up several releases of a package reads it once, and hands it to each
release_provenance()call.
- repomatic.pypi.release_provenance(package, version, *, provenance_urls=None)[source]¶
Look up the PEP 740 provenance of every file of one release of package.
Every file counts: a token thief can add a file to a release that trusted publishing uploaded, and the files already there keep their provenance. The JSON API lists the files of the release, and the Simple API says which of them carry provenance. The publisher comes from one attested file, preferring a wheel.
A failed lookup, or a Simple API page that misses a file of the release, gives an unknown provenance, which a caller must not read as an absence. A failed publisher lookup leaves the publisher empty and keeps the counts.
- Parameters:
- Return type:
- Returns:
The provenance, or an unknown one when PyPI could not be read.
- repomatic.pypi.release_wheel(package, version)[source]¶
Locate the wheel of one release of package on PyPI.
- Parameters:
- Return type:
- Returns:
The download URL of the first wheel the release lists, and the SHA-256 hex digest PyPI records for it.
Nonewhen PyPI could not be read, or lists no wheel with both.
- repomatic.pypi.get_changelog_url(package)[source]¶
Discover the changelog URL for a PyPI package.
Queries the PyPI JSON API and scans
project_urlsfor keys that typically point to a changelog or release notes page. Keys are matched case-insensitively, for the reason spelled out on_SOURCE_URL_KEYS: PyPI preserves whatever spelling the project wrote, soChangelog,changelogandCHANGELOGall occur in the wild.
- repomatic.pypi.sdist_name_version(dist_dir)[source]¶
Read the project name and version off the sdist in dist_dir.
An sdist is named
{name}-{version}.tar.gz, with a name normalized to hold no hyphen, so the last hyphen of the stem splits the two.- Parameters:
dist_dir (
Path) – Directory holding the distributions of one release.- Return type:
- Returns:
The normalized project name and the version.
- Raises:
ValueError – When dist_dir holds no sdist, or one whose name does not split.
- repomatic.pypi.wait_for_release(package, version, *, attempts=20, delay=15)[source]¶
Poll PyPI until version of package answers on its JSON API.
uv publishreturns once PyPI accepts the upload, but its index and CDN can take minutes to serve the release everywhere. Automation that reacts to the release at once (a downstream repository bumping its pin, or this project syncing its own workflow references against itself) would race that window and fail to resolve the version. The release lane waits here instead, so nothing after it inherits the race.Each lookup reads the API afresh: the cached reads of this module would serve the snapshot taken before the upload.
- Parameters:
- Return type:
- Returns:
The number of the lookup that found the version, or
Nonewhen none did.