Skip to content
Merged
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
7 changes: 7 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion wheelname/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
91 changes: 91 additions & 0 deletions wheelname/wheelname.go
Original file line number Diff line number Diff line change
@@ -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:]
}
54 changes: 54 additions & 0 deletions wheelname/wheelname_test.go
Original file line number Diff line number Diff line change
@@ -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"))
}