replace findMetadataAndRecord with more general findFiles

This commit is contained in:
Denis Bilenko 2025-03-05 10:44:32 +01:00
parent b9eeecd5a1
commit d7daebfba0
1 changed files with 19 additions and 15 deletions

View File

@ -23,22 +23,24 @@ const (
nameKey = "Name:" nameKey = "Name:"
) )
func findMetadataAndRecord(r *zip.ReadCloser, oldDistInfoPrefix string) (metadataFile, recordFile *zip.File) { func findFiles(r *zip.ReadCloser, files ...string) []*zip.File {
found := 0
result := make([]*zip.File, len(files))
for _, f := range r.File { for _, f := range r.File {
if metadataFile == nil && f.Name == oldDistInfoPrefix+"METADATA" { for ind, prev := range result {
metadataFile = f if prev != nil {
continue
} }
if f.Name == files[ind] {
if recordFile == nil && f.Name == oldDistInfoPrefix+"RECORD" { result[ind] = f
recordFile = f found += 1
if found >= len(files) {
if metadataFile != nil { return result
break
} }
} }
} }
}
return metadataFile, recordFile return result
} }
// 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
@ -141,13 +143,15 @@ func PatchWheel(ctx context.Context, path, outputDir string) (string, error) {
defer r.Close() defer r.Close()
oldDistInfoPrefix := wheelInfo.Distribution + "-" + wheelInfo.Version + ".dist-info/" oldDistInfoPrefix := wheelInfo.Distribution + "-" + wheelInfo.Version + ".dist-info/"
metadataFile, recordFile := findMetadataAndRecord(r, oldDistInfoPrefix) files := findFiles(r, oldDistInfoPrefix+"METADATA", oldDistInfoPrefix+"RECORD")
metadataFile, recordFile := files[0], files[1]
if metadataFile == nil { if metadataFile == nil {
return "", fmt.Errorf("wheel %s missing METADATA file", path) return "", fmt.Errorf("wheel %s missing %sMETADATA", path, oldDistInfoPrefix)
} }
if recordFile == nil { if recordFile == nil {
return "", fmt.Errorf("wheel %s missing RECORD file", path) return "", fmt.Errorf("wheel %s missing %sRECORD file", path, oldDistInfoPrefix)
} }
metadataReader, err := metadataFile.Open() metadataReader, err := metadataFile.Open()