Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~~~~~~~~~~~

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

.. toctree::
Expand Down
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
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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]]:
Expand Down
Loading
Loading