replace findFiles with findFile

This commit is contained in:
Denis Bilenko 2025-03-05 17:03:30 +01:00
parent 482bf34ff4
commit ca0f3a13eb
1 changed files with 6 additions and 21 deletions

View File

@ -20,27 +20,13 @@ import (
var versionKey []byte = []byte("Version:") var versionKey []byte = []byte("Version:")
// findFiles returns a slice with a *zip.File for every filename in the arguments slice. func findFile(r *zip.ReadCloser, filename string) *zip.File {
// The order of the return value matches the order of the arguments.
// If not found, the corresponding entry is nil.
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 {
for ind, prev := range result { if f.Name == filename {
if prev != nil { return f
continue
}
if f.Name == files[ind] {
result[ind] = f
found += 1
if found >= len(files) {
return result
}
}
} }
} }
return result return nil
} }
// 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
@ -152,13 +138,12 @@ 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/"
files := findFiles(r, oldDistInfoPrefix+"METADATA", oldDistInfoPrefix+"RECORD") metadataFile := findFile(r, oldDistInfoPrefix+"METADATA")
metadataFile, recordFile := files[0], files[1]
if metadataFile == nil { if metadataFile == nil {
return "", fmt.Errorf("wheel %s missing %sMETADATA", path, oldDistInfoPrefix) return "", fmt.Errorf("wheel %s missing %sMETADATA", path, oldDistInfoPrefix)
} }
recordFile := findFile(r, oldDistInfoPrefix+"RECORD")
if recordFile == nil { if recordFile == nil {
return "", fmt.Errorf("wheel %s missing %sRECORD file", path, oldDistInfoPrefix) return "", fmt.Errorf("wheel %s missing %sRECORD file", path, oldDistInfoPrefix)
} }