| 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 // Normalize normalizes the specification Message such that two messages | 24 // NormalizeEnvironment normalizes the supplied Environment such that two |
| 25 // with identical meaning will have identical representation. | 25 // messages with identical meaning will have identical representation. |
| 26 func Normalize(spec *vpython.Spec, defaultVENVPackage *vpython.Spec_Package) err
or { | 26 func NormalizeEnvironment(env *vpython.Environment) error { |
| 27 » if spec.Virtualenv == nil { | 27 » if env.Spec == nil { |
| 28 » » spec.Virtualenv = defaultVENVPackage | 28 » » env.Spec = &vpython.Spec{} |
| 29 » } |
| 30 » if err := NormalizeSpec(env.Spec); err != nil { |
| 31 » » return err |
| 29 } | 32 } |
| 30 | 33 |
| 34 if env.Runtime == nil { |
| 35 env.Runtime = &vpython.Runtime{} |
| 36 } |
| 37 |
| 38 sort.Sort(pep425TagSlice(env.Pep425Tag)) |
| 39 return nil |
| 40 } |
| 41 |
| 42 // NormalizeSpec normalizes the specification Message such that two messages |
| 43 // with identical meaning will have identical representation. |
| 44 func NormalizeSpec(spec *vpython.Spec) error { |
| 31 sort.Sort(specPackageSlice(spec.Wheel)) | 45 sort.Sort(specPackageSlice(spec.Wheel)) |
| 32 | 46 |
| 33 // No duplicate packages. Since we're sorted, we can just check for no | 47 // No duplicate packages. Since we're sorted, we can just check for no |
| 34 // immediate repetitions. | 48 // immediate repetitions. |
| 35 for i, pkg := range spec.Wheel { | 49 for i, pkg := range spec.Wheel { |
| 36 if i > 0 && pkg.Name == spec.Wheel[i-1].Name { | 50 if i > 0 && pkg.Name == spec.Wheel[i-1].Name { |
| 37 return errors.Reason("duplicate spec entries for package
%(path)q"). | 51 return errors.Reason("duplicate spec entries for package
%(path)q"). |
| 38 D("name", pkg.Name). | 52 D("name", pkg.Name). |
| 39 Err() | 53 Err() |
| 40 } | 54 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 | 94 |
| 81 func (s specPackageSlice) Len() int { return len(s) } | 95 func (s specPackageSlice) Len() int { return len(s) } |
| 82 func (s specPackageSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } | 96 func (s specPackageSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
| 83 | 97 |
| 84 func (s specPackageSlice) Less(i, j int) bool { | 98 func (s specPackageSlice) Less(i, j int) bool { |
| 85 return sortby.Chain{ | 99 return sortby.Chain{ |
| 86 func(i, j int) bool { return s[i].Name < s[j].Name }, | 100 func(i, j int) bool { return s[i].Name < s[j].Name }, |
| 87 func(i, j int) bool { return s[i].Version < s[j].Version }, | 101 func(i, j int) bool { return s[i].Version < s[j].Version }, |
| 88 }.Use(i, j) | 102 }.Use(i, j) |
| 89 } | 103 } |
| 104 |
| 105 type pep425TagSlice []*vpython.Pep425Tag |
| 106 |
| 107 func (s pep425TagSlice) Len() int { return len(s) } |
| 108 func (s pep425TagSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
| 109 |
| 110 func (s pep425TagSlice) Less(i, j int) bool { |
| 111 return sortby.Chain{ |
| 112 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 }, |
| 114 func(i, j int) bool { return s[i].Arch < s[j].Arch }, |
| 115 }.Use(i, j) |
| 116 } |
| OLD | NEW |