| OLD | NEW |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. | 1 // Copyright 2017 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package spec | 5 package spec |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "crypto/sha256" | 8 "crypto/sha256" |
| 9 "encoding/hex" | 9 "encoding/hex" |
| 10 "fmt" | 10 "fmt" |
| 11 "sort" | 11 "sort" |
| 12 | 12 |
| 13 "github.com/luci/luci-go/vpython/api/vpython" | 13 "github.com/luci/luci-go/vpython/api/vpython" |
| 14 | 14 |
| 15 "github.com/luci/luci-go/common/data/sortby" | 15 "github.com/luci/luci-go/common/data/sortby" |
| 16 "github.com/luci/luci-go/common/errors" | 16 "github.com/luci/luci-go/common/errors" |
| 17 | 17 |
| 18 "github.com/golang/protobuf/proto" | 18 "github.com/golang/protobuf/proto" |
| 19 ) | 19 ) |
| 20 | 20 |
| 21 // Render creates a human-readable string from spec. | 21 // Render creates a human-readable string from spec. |
| 22 func Render(spec *vpython.Spec) string { return proto.MarshalTextString(spec) } | 22 func Render(spec *vpython.Spec) string { return proto.MarshalTextString(spec) } |
| 23 | 23 |
| 24 // NormalizeEnvironment normalizes the supplied Environment such that two | 24 // NormalizeEnvironment normalizes the supplied Environment such that two |
| 25 // messages with identical meaning will have identical representation. | 25 // messages with identical meaning will have identical representation. |
| 26 func NormalizeEnvironment(env *vpython.Environment) error { | 26 func NormalizeEnvironment(env *vpython.Environment) error { |
| 27 if env.Spec == nil { | 27 if env.Spec == nil { |
| 28 env.Spec = &vpython.Spec{} | 28 env.Spec = &vpython.Spec{} |
| 29 } | 29 } |
| 30 » if err := NormalizeSpec(env.Spec); err != nil { | 30 » if err := NormalizeSpec(env.Spec, env.Pep425Tag); err != nil { |
| 31 return err | 31 return err |
| 32 } | 32 } |
| 33 | 33 |
| 34 if env.Runtime == nil { | 34 if env.Runtime == nil { |
| 35 env.Runtime = &vpython.Runtime{} | 35 env.Runtime = &vpython.Runtime{} |
| 36 } | 36 } |
| 37 | 37 |
| 38 sort.Sort(pep425TagSlice(env.Pep425Tag)) | 38 sort.Sort(pep425TagSlice(env.Pep425Tag)) |
| 39 return nil | 39 return nil |
| 40 } | 40 } |
| 41 | 41 |
| 42 // NormalizeSpec normalizes the specification Message such that two messages | 42 // NormalizeSpec normalizes the specification Message such that two messages |
| 43 // with identical meaning will have identical representation. | 43 // with identical meaning will have identical representation. |
| 44 func NormalizeSpec(spec *vpython.Spec) error { | 44 // |
| 45 // NormalizeSpec will prune any Wheel entries that don't match the specified |
| 46 // tags, and will remove the match entries from any remaining Wheel entries. |
| 47 func NormalizeSpec(spec *vpython.Spec, tags []*vpython.Pep425Tag) error { |
| 48 » if spec.Virtualenv != nil && len(spec.Virtualenv.MatchTag) > 0 { |
| 49 » » // The VirtualEnv package may not specify a match tag. |
| 50 » » spec.Virtualenv.MatchTag = nil |
| 51 » } |
| 52 |
| 53 » // Apply match filters, prune any entries that don't match, and clear th
e |
| 54 » // MatchTag entries for those that do. |
| 55 » pos := 0 |
| 56 » for _, wheel := range spec.Wheel { |
| 57 » » if !PackageMatches(wheel, tags) { |
| 58 » » » continue |
| 59 » » } |
| 60 |
| 61 » » wheel.MatchTag = nil |
| 62 » » spec.Wheel[pos] = wheel |
| 63 » » pos++ |
| 64 » } |
| 65 » spec.Wheel = spec.Wheel[:pos] |
| 66 |
| 45 sort.Sort(specPackageSlice(spec.Wheel)) | 67 sort.Sort(specPackageSlice(spec.Wheel)) |
| 46 | 68 |
| 47 // No duplicate packages. Since we're sorted, we can just check for no | 69 // No duplicate packages. Since we're sorted, we can just check for no |
| 48 // immediate repetitions. | 70 // immediate repetitions. |
| 49 for i, pkg := range spec.Wheel { | 71 for i, pkg := range spec.Wheel { |
| 50 if i > 0 && pkg.Name == spec.Wheel[i-1].Name { | 72 if i > 0 && pkg.Name == spec.Wheel[i-1].Name { |
| 51 return errors.Reason("duplicate spec entries for package
%(path)q"). | 73 return errors.Reason("duplicate spec entries for package
%(path)q"). |
| 52 D("name", pkg.Name). | 74 D("name", pkg.Name). |
| 53 Err() | 75 Err() |
| 54 } | 76 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 func (s pep425TagSlice) Len() int { return len(s) } | 129 func (s pep425TagSlice) Len() int { return len(s) } |
| 108 func (s pep425TagSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } | 130 func (s pep425TagSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
| 109 | 131 |
| 110 func (s pep425TagSlice) Less(i, j int) bool { | 132 func (s pep425TagSlice) Less(i, j int) bool { |
| 111 return sortby.Chain{ | 133 return sortby.Chain{ |
| 112 func(i, j int) bool { return s[i].Version < s[j].Version }, | 134 func(i, j int) bool { return s[i].Version < s[j].Version }, |
| 113 func(i, j int) bool { return s[i].Abi < s[j].Abi }, | 135 func(i, j int) bool { return s[i].Abi < s[j].Abi }, |
| 114 func(i, j int) bool { return s[i].Arch < s[j].Arch }, | 136 func(i, j int) bool { return s[i].Arch < s[j].Arch }, |
| 115 }.Use(i, j) | 137 }.Use(i, j) |
| 116 } | 138 } |
| OLD | NEW |