clean up and simplify parsing

This commit is contained in:
Denis Bilenko 2025-03-04 18:02:18 +01:00
parent 653ca59edf
commit 5abb022506
2 changed files with 33 additions and 293 deletions

View File

@ -39,30 +39,20 @@ func ParseWheelFilename(filename string) (*WheelInfo, error) {
if len(parts) < 5 { if len(parts) < 5 {
return nil, fmt.Errorf("invalid wheel filename format: not enough parts in %s", filename) return nil, fmt.Errorf("invalid wheel filename format: not enough parts in %s", filename)
} }
if len(parts) > 6 {
return nil, fmt.Errorf("invalid wheel filename format: too many parts in %s", filename)
}
if !strings.HasSuffix(parts[len(parts)-1], ".whl") { if !strings.HasSuffix(parts[len(parts)-1], ".whl") {
return nil, fmt.Errorf("invalid wheel filename format: missing .whl extension in %s", filename) return nil, fmt.Errorf("invalid wheel filename format: missing .whl extension in %s", filename)
} }
// The last three parts are always the python, abi and platform tags parts[len(parts)-1] = strings.TrimSuffix(parts[len(parts)-1], ".whl")
tagStartIdx := len(parts) - 3
tags := parts[tagStartIdx:]
tags[2] = strings.TrimSuffix(tags[2], ".whl")
// The distribution is always the first part
distribution := parts[0]
// The version is the second part - don't include build tags
version := parts[1]
// If there are build tags between version and python tag, include them in tags
if tagStartIdx > 2 {
buildTags := parts[2:tagStartIdx]
tags = append(buildTags, tags...)
}
return &WheelInfo{ return &WheelInfo{
Distribution: distribution, Distribution: parts[0],
Version: version, Version: parts[1],
Tags: tags, Tags: parts[2:],
}, nil }, nil
} }

View File

@ -7,274 +7,6 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
/*
AI TODO: incorporate this test cases into TestParseWheelFilename
@pytest.mark.parametrize(
"filename,expected",
[
(
"astrocats-0.3.2-universal-none-any.whl",
ParsedWheelFilename(
project="astrocats",
version="0.3.2",
build=None,
python_tags=["universal"],
abi_tags=["none"],
platform_tags=["any"],
),
),
(
"bencoder.pyx-1.1.2-pp226-pp226-win32.whl",
ParsedWheelFilename(
project="bencoder.pyx",
version="1.1.2",
build=None,
python_tags=["pp226"],
abi_tags=["pp226"],
platform_tags=["win32"],
),
),
(
"brotlipy-0.1.2-pp27-none-macosx_10_10_x86_64.whl",
ParsedWheelFilename(
project="brotlipy",
version="0.1.2",
build=None,
python_tags=["pp27"],
abi_tags=["none"],
platform_tags=["macosx_10_10_x86_64"],
),
),
(
"brotlipy-0.3.0-pp226-pp226u-macosx_10_10_x86_64.whl",
ParsedWheelFilename(
project="brotlipy",
version="0.3.0",
build=None,
python_tags=["pp226"],
abi_tags=["pp226u"],
platform_tags=["macosx_10_10_x86_64"],
),
),
(
"carbonara_archinfo-7.7.9.14.post1-py2-none-any.whl",
ParsedWheelFilename(
project="carbonara_archinfo",
version="7.7.9.14.post1",
build=None,
python_tags=["py2"],
abi_tags=["none"],
platform_tags=["any"],
),
),
(
"coremltools-0.3.0-py2.7-none-any.whl",
ParsedWheelFilename(
project="coremltools",
version="0.3.0",
build=None,
python_tags=["py2", "7"],
abi_tags=["none"],
platform_tags=["any"],
),
),
(
"cvxopt-1.2.0-001-cp34-cp34m-macosx_10_6_intel.macosx_10_9_intel"
".macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl",
ParsedWheelFilename(
project="cvxopt",
version="1.2.0",
build="001",
python_tags=["cp34"],
abi_tags=["cp34m"],
platform_tags=[
"macosx_10_6_intel",
"macosx_10_9_intel",
"macosx_10_9_x86_64",
"macosx_10_10_intel",
"macosx_10_10_x86_64",
],
),
),
(
"django_mbrowse-0.0.1-10-py2-none-any.whl",
ParsedWheelFilename(
project="django_mbrowse",
version="0.0.1",
build="10",
python_tags=["py2"],
abi_tags=["none"],
platform_tags=["any"],
),
),
(
"efilter-1!1.2-py2-none-any.whl",
ParsedWheelFilename(
project="efilter",
version="1!1.2",
build=None,
python_tags=["py2"],
abi_tags=["none"],
platform_tags=["any"],
),
),
(
"line.sep-0.2.0.dev1-py2.py3-none-any.whl",
ParsedWheelFilename(
project="line.sep",
version="0.2.0.dev1",
build=None,
python_tags=["py2", "py3"],
abi_tags=["none"],
platform_tags=["any"],
),
),
(
"mayan_edms-1.1.0-1502100955-py2-none-any.whl",
ParsedWheelFilename(
project="mayan_edms",
version="1.1.0",
build="1502100955",
python_tags=["py2"],
abi_tags=["none"],
platform_tags=["any"],
),
),
(
"mxnet_model_server-1.0a5-20180816-py2.py3-none-any.whl",
ParsedWheelFilename(
project="mxnet_model_server",
version="1.0a5",
build="20180816",
python_tags=["py2", "py3"],
abi_tags=["none"],
platform_tags=["any"],
),
),
(
"pip-18.0-py2.py3-none-any.whl",
ParsedWheelFilename(
project="pip",
version="18.0",
build=None,
python_tags=["py2", "py3"],
abi_tags=["none"],
platform_tags=["any"],
),
),
(
"polarTransform-2-1.0.0-py3-none-any.whl",
ParsedWheelFilename(
project="polarTransform",
version="2",
build="1.0.0",
python_tags=["py3"],
abi_tags=["none"],
platform_tags=["any"],
),
),
(
"psycopg2-2.7.5-cp37-cp37m-macosx_10_6_intel.macosx_10_9_intel"
".macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl",
ParsedWheelFilename(
project="psycopg2",
version="2.7.5",
build=None,
python_tags=["cp37"],
abi_tags=["cp37m"],
platform_tags=[
"macosx_10_6_intel",
"macosx_10_9_intel",
"macosx_10_9_x86_64",
"macosx_10_10_intel",
"macosx_10_10_x86_64",
],
),
),
(
"pyinterval-1.0.0-0-cp27-none-win32.whl",
ParsedWheelFilename(
project="pyinterval",
version="1.0.0",
build="0",
python_tags=["cp27"],
abi_tags=["none"],
platform_tags=["win32"],
),
),
(
"pypi_simple-0.1.0.dev1-py2.py3-none-any.whl",
ParsedWheelFilename(
project="pypi_simple",
version="0.1.0.dev1",
build=None,
python_tags=["py2", "py3"],
abi_tags=["none"],
platform_tags=["any"],
),
),
(
"PyQt3D-5.7.1-5.7.1-cp34.cp35.cp36-abi3-macosx_10_6_intel.whl",
ParsedWheelFilename(
project="PyQt3D",
version="5.7.1",
build="5.7.1",
python_tags=["cp34", "cp35", "cp36"],
abi_tags=["abi3"],
platform_tags=["macosx_10_6_intel"],
),
),
(
"qypi-0.4.1-py3-none-any.whl",
ParsedWheelFilename(
project="qypi",
version="0.4.1",
build=None,
python_tags=["py3"],
abi_tags=["none"],
platform_tags=["any"],
),
),
(
"SimpleSteem-1.1.9-3.0-none-any.whl",
ParsedWheelFilename(
project="SimpleSteem",
version="1.1.9",
build=None,
python_tags=["3", "0"],
abi_tags=["none"],
platform_tags=["any"],
),
),
(
"simple_workflow-0.1.47-pypy-none-any.whl",
ParsedWheelFilename(
project="simple_workflow",
version="0.1.47",
build=None,
python_tags=["pypy"],
abi_tags=["none"],
platform_tags=["any"],
),
),
(
"tables-3.4.2-3-cp27-cp27m-manylinux1_i686.whl",
ParsedWheelFilename(
project="tables",
version="3.4.2",
build="3",
python_tags=["cp27"],
abi_tags=["cp27m"],
platform_tags=["manylinux1_i686"],
),
),
],
)
*/
func TestCalculateNewVersion(t *testing.T) { func TestCalculateNewVersion(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@ -354,13 +86,15 @@ func TestParseWheelFilename(t *testing.T) {
wantErr: false, wantErr: false,
}, },
{ {
filename: "my-proj-with-hyphens-0.1.0-py3-none-any.whl", filename: "my_proj_with_parts-0.1.0-py3-none-any.whl",
wantDistribution: "my-proj-with-hyphens", wantDistribution: "my_proj_with_parts",
wantVersion: "0.1.0", wantVersion: "0.1.0",
wantTags: []string{"py3", "none", "any"}, wantTags: []string{"py3", "none", "any"},
wantErr: false, wantErr: false,
}, },
// Test cases from the AI TODO // Test cases adapted from wheelodex/wheel-filename
// https://github.com/wheelodex/wheel-filename/blob/f5a72b560d016cc9663c8b5a094c96dc338a2209/test/test_parse.py
// MIT License: https://github.com/wheelodex/wheel-filename/blob/f5a72b560d016cc9663c8b5a094c96dc338a2209/LICENSE
{ {
filename: "astrocats-0.3.2-universal-none-any.whl", filename: "astrocats-0.3.2-universal-none-any.whl",
wantDistribution: "astrocats", wantDistribution: "astrocats",
@ -538,10 +272,26 @@ func TestParseWheelFilename(t *testing.T) {
require.Error(t, err) require.Error(t, err)
} else { } else {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, tt.wantDistribution, info.Distribution) require.Equal(t, tt.wantDistribution, info.Distribution, "distribution mismatch")
require.Equal(t, tt.wantVersion, info.Version) require.Equal(t, tt.wantVersion, info.Version, "version mismatch")
require.Equal(t, tt.wantTags, info.Tags) require.Equal(t, tt.wantTags, info.Tags, "tags mismatch")
} }
}) })
} }
} }
func TestParseError(t *testing.T) {
tests := []string{
"",
"a-b",
"a-b-c.whl",
"a-b-c-d.whl",
"a-b-c-d-e-f-g.whl",
}
for _, tt := range tests {
t.Run(tt, func(t *testing.T) {
info, err := ParseWheelFilename(tt)
require.Error(t, err, "info=%v", info)
})
}
}