From 8294feef2d8a3d501b81571dd39c0cf59899c5ca Mon Sep 17 00:00:00 2001 From: Jonathan Yoder Date: Wed, 8 Jul 2026 15:01:02 -0400 Subject: [PATCH] feat(wheelname): PEP 427 wheel-filename parsing (#18631) --- NOTICE | 7 +++ README.md | 2 +- wheelname/doc.go | 8 +++- wheelname/wheelname.go | 91 +++++++++++++++++++++++++++++++++++++ wheelname/wheelname_test.go | 54 ++++++++++++++++++++++ 5 files changed, 160 insertions(+), 2 deletions(-) create mode 100644 wheelname/wheelname.go create mode 100644 wheelname/wheelname_test.go diff --git a/NOTICE b/NOTICE index fed3c72..a39dd38 100644 --- a/NOTICE +++ b/NOTICE @@ -25,3 +25,10 @@ packaging/tags.py, packaging/_manylinux.py, and packaging/_musllinux.py, licensed under the Apache License, Version 2.0; and from Astral's uv (https://github.com/astral-sh/uv), specifically the uv-platform-tags crate, dual-licensed under the Apache License, Version 2.0, or the MIT license. + +The wheelname/ package adapts wheel-filename parsing from pypa/packaging +(https://github.com/pypa/packaging), specifically +packaging/utils.py's `parse_wheel_filename`, licensed under the Apache +License, Version 2.0; and from Astral's uv (https://github.com/astral-sh/uv), +specifically the uv-distribution-filename crate, dual-licensed under the +Apache License, Version 2.0, or the MIT license. diff --git a/README.md b/README.md index caa6a4f..491a06b 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Part of [RFD 0001 — Native Go PyPI Dependency Resolution](https://github.com/r | `extras/` | PEP 685 extra-name normalization | | `reqtxt/` | pip `requirements.txt` format | | [`distribution/`](https://github.com/rstudio/python-distribution-parser) | METADATA / wheel parsing | -| `wheelname/` | wheel filename → structured fields | +| `wheelname/` | PEP 427 wheel filename → name/version/build/tags (implemented) | | `tags/` | PEP 425/600/656 compatibility tags (server-side, target-parameterized; no host detection) | ## License diff --git a/wheelname/doc.go b/wheelname/doc.go index 33ed7e9..cdce852 100644 --- a/wheelname/doc.go +++ b/wheelname/doc.go @@ -2,5 +2,11 @@ // Package wheelname decomposes a wheel filename into its structured fields. // -// TODO(#18631): implement wheel-filename parsing. +// Parse splits a PEP 427 wheel filename (name-version[-build]-tags.whl) into +// its name, version, optional build tag, and expanded compatibility tags. +// The version field is parsed as-is via the version package; PEP 427's +// filename-safe "_" is not un-escaped to "+", so filenames that encode a +// local version segment (e.g. "torch-2.4.0+cu121-...") must already carry +// the literal "+". CompareBuildTags totally orders PEP 427 build tags, +// treating an absent build tag as ordering before any present tag. package wheelname diff --git a/wheelname/wheelname.go b/wheelname/wheelname.go new file mode 100644 index 0000000..0efd1d5 --- /dev/null +++ b/wheelname/wheelname.go @@ -0,0 +1,91 @@ +// SPDX-License-Identifier: Apache-2.0 OR MIT +package wheelname + +import ( + "errors" + "fmt" + "strconv" + "strings" + + "github.com/posit-dev/go-python-packaging/tags" + "github.com/posit-dev/go-python-packaging/version" +) + +var ErrInvalidWheelFilename = errors.New("invalid wheel filename") + +type WheelFilename struct { + Name string + Version version.Version + Build string + Tags []tags.Tag +} + +func Parse(filename string) (WheelFilename, error) { + base, ok := strings.CutSuffix(filename, ".whl") + if !ok { + return WheelFilename{}, fmt.Errorf("%w: missing .whl suffix", ErrInvalidWheelFilename) + } + parts := strings.Split(base, "-") + if len(parts) != 5 && len(parts) != 6 { + return WheelFilename{}, fmt.Errorf("%w: expected 5 or 6 dash fields, got %d", ErrInvalidWheelFilename, len(parts)) + } + name := strings.ToLower(parts[0]) + verStr := parts[1] + var build string + tagStart := 2 + if len(parts) == 6 { + build = parts[2] + if build == "" || build[0] < '0' || build[0] > '9' { + return WheelFilename{}, fmt.Errorf("%w: build tag %q must start with a digit", ErrInvalidWheelFilename, build) + } + tagStart = 3 + } + ver, err := version.Parse(verStr) // as-is; no _->+ un-escape + if err != nil { + return WheelFilename{}, fmt.Errorf("wheel %q: version: %w", filename, err) + } + tagComponent := strings.Join(parts[tagStart:tagStart+3], "-") + tg, err := tags.ParseTag(tagComponent) + if err != nil { + return WheelFilename{}, fmt.Errorf("wheel %q: tag: %w", filename, err) + } + return WheelFilename{Name: name, Version: ver, Build: build, Tags: tg}, nil +} + +// CompareBuildTags orders PEP 427 build tags; absent ("") sorts first; present +// tags compare by (leading-int, remaining-string). Non-digit-led input is +// treated as numeric part 0 (total, never panics). +func CompareBuildTags(a, b string) int { + if a == b { + return 0 + } + if a == "" { + return -1 + } + if b == "" { + return 1 + } + an, ar := splitBuild(a) + bn, br := splitBuild(b) + switch { + case an < bn: + return -1 + case an > bn: + return 1 + case ar < br: + return -1 + case ar > br: + return 1 + default: + return 0 + } +} + +func splitBuild(s string) (int, string) { + i := 0 + for i < len(s) && s[i] >= '0' && s[i] <= '9' { + i++ + } + n, _ := strconv.Atoi(s[:i]) // "" -> 0 + return n, s[i:] +} diff --git a/wheelname/wheelname_test.go b/wheelname/wheelname_test.go new file mode 100644 index 0000000..5a6b20c --- /dev/null +++ b/wheelname/wheelname_test.go @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: Apache-2.0 OR MIT +package wheelname + +import ( + "testing" + + "github.com/posit-dev/go-python-packaging/tags" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestParse_Valid(t *testing.T) { + w, err := Parse("numpy-1.26.0-cp311-cp311-manylinux_2_17_x86_64.whl") + require.NoError(t, err) + assert.Equal(t, "numpy", w.Name) + assert.Equal(t, "1.26.0", w.Version.String()) + assert.Equal(t, "", w.Build) + assert.Equal(t, []tags.Tag{{Interpreter: "cp311", ABI: "cp311", Platform: "manylinux_2_17_x86_64"}}, w.Tags) +} + +func TestParse_BuildTagAndCompressed(t *testing.T) { + w, err := Parse("foo-1.0-1-py2.py3-none-any.whl") + require.NoError(t, err) + assert.Equal(t, "1", w.Build) + assert.ElementsMatch(t, []tags.Tag{ + {Interpreter: "py2", ABI: "none", Platform: "any"}, + {Interpreter: "py3", ABI: "none", Platform: "any"}, + }, w.Tags) +} + +func TestParse_LocalVersionKeepsPlus(t *testing.T) { + w, err := Parse("torch-2.4.0+cu121-cp311-cp311-linux_x86_64.whl") + require.NoError(t, err) + assert.Equal(t, "2.4.0+cu121", w.Version.String()) +} + +func TestParse_Malformed(t *testing.T) { + for _, s := range []string{ + "nowhlsuffix-1.0-py3-none-any", // no .whl + "too-few-py3-none-any.whl", // wrong field count vs name-with-dash + "foo-1.0-x-py3-none-any.whl", // build tag not digit-led + "foo-notaversion-py3-none-any.whl", // bad version + } { + _, err := Parse(s) + require.Error(t, err, s) + } +} + +func TestCompareBuildTags(t *testing.T) { + assert.Negative(t, CompareBuildTags("", "1")) // absent < present + assert.Negative(t, CompareBuildTags("2", "10")) // numeric, not lexical + assert.Negative(t, CompareBuildTags("1abc", "1abd")) + assert.Zero(t, CompareBuildTags("3x", "3x")) +}