Skip to content

Added an API for working with wheel files #805

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

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -4,9 +4,10 @@ Changelog
*unreleased*
~~~~~~~~~~~~

* Add the ``packaging.wheelfile`` module for reading and creating wheel files
(:issue:`697`)
* Change project license metadata to use an SPDX license expression.


25.0 - 2025-04-19
~~~~~~~~~~~~~~~~~

1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@ The ``packaging`` library uses calendar-based versioning (``YY.N``).
requirements
metadata
tags
wheelfile
utils

.. toctree::
4 changes: 4 additions & 0 deletions docs/wheelfile.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Wheel Files
===========

.. currentmodule:: packaging.wheelfile
23 changes: 23 additions & 0 deletions src/packaging/utils.py
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@

import functools
import re
from collections.abc import Collection
from typing import NewType, Tuple, Union, cast

from .tags import Tag, parse_tag
@@ -41,6 +42,7 @@ class InvalidSdistFilename(ValueError):
_normalized_regex = re.compile(r"^([a-z0-9]|[a-z0-9]([a-z0-9-](?!--))*[a-z0-9])$")
# PEP 427: The build number must start with a digit.
_build_tag_regex = re.compile(r"(\d+)(.*)")
_dist_name_re = re.compile(r"[^a-z0-9.]+", re.IGNORECASE)


def canonicalize_name(name: str, *, validate: bool = False) -> NormalizedName:
@@ -91,6 +93,27 @@ def _(version: str, *, strip_trailing_zero: bool = True) -> str:
return canonicalize_version(parsed, strip_trailing_zero=strip_trailing_zero)


def make_wheel_filename(
name: str,
version: str | Version,
tags: Collection[Tag],
*,
build_tag: BuildTag | None = None,
) -> str:
if not tags:
raise ValueError("At least one tag is required")

name = canonicalize_name(name).replace("-", "_").lower()
filename = f"{name}-{version}"
if build_tag:
filename = f"{filename}-{build_tag[0]}{build_tag[1]}"

interpreter_tags = ".".join(tag.interpreter for tag in tags)
abi_tags = ".".join(tag.abi for tag in tags)
platform_tags = ".".join(tag.platform for tag in tags)
return f"{filename}-{interpreter_tags}-{abi_tags}-{platform_tags}.whl"


def parse_wheel_filename(
filename: str,
) -> tuple[NormalizedName, Version, BuildTag, frozenset[Tag]]:
Loading