databricks-cli/libs/patchwheel/parse.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

69 lines
2.2 KiB
Go
Raw Normal View History

2025-03-03 20:00:50 +00:00
package patchwheel
import (
"fmt"
"strings"
2025-03-04 12:08:28 +00:00
"time"
2025-03-03 20:00:50 +00:00
)
// WheelInfo contains information extracted from a wheel filename
type WheelInfo struct {
Distribution string // Package distribution name
Version string // Package version
Tags []string // Python tags (python_tag, abi_tag, platform_tag)
}
// CalculateNewVersion generates a new version string and filename based on the wheel info and modification time.
// The version is updated according to the following rules:
// - if there is an existing part after + it is dropped
// - append +<mtime of the original wheel> to version
func CalculateNewVersion(info *WheelInfo, mtime time.Time) (newVersion, newFilename string) {
baseVersion := strings.SplitN(info.Version, "+", 2)[0]
2025-03-04 12:08:28 +00:00
dt := strings.Replace(mtime.Format("20060102150405.00"), ".", "", 1)
dt = strings.Replace(dt, ".", "", 1)
newVersion = baseVersion + "+" + dt
2025-03-04 12:08:28 +00:00
newFilename = fmt.Sprintf("%s-%s-%s.whl",
info.Distribution,
newVersion,
strings.Join(info.Tags, "-"))
2025-03-04 12:08:28 +00:00
return newVersion, newFilename
}
2025-03-03 20:00:50 +00:00
// ParseWheelFilename parses a wheel filename and extracts its components.
// Wheel filenames follow the pattern: {distribution}-{version}(-{build tag})?-{python_tag}-{abi_tag}-{platform_tag}.whl
2025-03-03 20:00:50 +00:00
func ParseWheelFilename(filename string) (*WheelInfo, error) {
parts := strings.Split(filename, "-")
if len(parts) < 5 {
return nil, fmt.Errorf("invalid wheel filename format: not enough parts in %s", filename)
}
if !strings.HasSuffix(parts[len(parts)-1], ".whl") {
return nil, fmt.Errorf("invalid wheel filename format: missing .whl extension in %s", filename)
2025-03-03 20:00:50 +00:00
}
2025-03-04 16:48:57 +00:00
// The last three parts are always the python, abi and platform tags
2025-03-03 20:00:50 +00:00
tagStartIdx := len(parts) - 3
2025-03-04 09:01:45 +00:00
tags := parts[tagStartIdx:]
tags[2] = strings.TrimSuffix(tags[2], ".whl")
2025-03-03 20:00:50 +00:00
// The distribution is always the first part
distribution := parts[0]
// The version is the second part - don't include build tags
version := parts[1]
2025-03-03 20:00:50 +00:00
2025-03-04 16:48:57 +00:00
// 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...)
}
2025-03-03 20:00:50 +00:00
return &WheelInfo{
Distribution: distribution,
Version: version,
Tags: tags,
}, nil
}