remove glob matching in readMetadataAndRecord, rename to findMetadataAndRecord

This commit is contained in:
Denis Bilenko 2025-03-04 18:16:43 +01:00
parent 138ced54eb
commit ca55524f94
1 changed files with 10 additions and 26 deletions

View File

@ -11,7 +11,6 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"path"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
@ -22,38 +21,22 @@ const (
nameKey = "Name:" nameKey = "Name:"
) )
// readMetadataAndRecord scans the zip file for files matching the patterns func findMetadataAndRecord(r *zip.ReadCloser, oldDistInfoPrefix string) (metadataFile, recordFile *zip.File) {
// "*.dist-info/METADATA" and "*.dist-info/RECORD". If multiple are found, it picks
// the first one encountered.
func readMetadataAndRecord(r *zip.ReadCloser) (metadataFile, recordFile *zip.File, oldDistInfoPrefix string) {
for _, f := range r.File { for _, f := range r.File {
if metadataFile == nil { if metadataFile == nil && f.Name == oldDistInfoPrefix+"METADATA" {
matched, _ := path.Match("*.dist-info/METADATA", f.Name) metadataFile = f
if matched {
metadataFile = f
oldDistInfoPrefix = path.Dir(f.Name) + "/"
if recordFile != nil {
break
}
continue
}
} }
if recordFile == nil { if recordFile == nil && f.Name == oldDistInfoPrefix+"RECORD" {
matched, _ := path.Match("*.dist-info/RECORD", f.Name) recordFile = f
if matched {
recordFile = f
if metadataFile != nil { if metadataFile != nil {
break break
}
} }
} }
} }
return metadataFile, recordFile, oldDistInfoPrefix return metadataFile, recordFile
} }
// patchMetadata returns new METADATA content with an updated "Version:" field and validates that previous version matches oldVersion // patchMetadata returns new METADATA content with an updated "Version:" field and validates that previous version matches oldVersion
@ -155,7 +138,8 @@ func PatchWheel(ctx context.Context, path, outputDir string) (string, error) {
} }
defer r.Close() defer r.Close()
metadataFile, recordFile, oldDistInfoPrefix := readMetadataAndRecord(r) oldDistInfoPrefix := wheelInfo.Distribution + "-" + wheelInfo.Version + ".dist-info/"
metadataFile, recordFile := findMetadataAndRecord(r, oldDistInfoPrefix)
if metadataFile == nil { if metadataFile == nil {
return "", fmt.Errorf("wheel %s missing METADATA file", path) return "", fmt.Errorf("wheel %s missing METADATA file", path)
} }