| 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 // Normalize normalizes the specification Message such that two messages | 21 // Normalize normalizes the specification Message such that two messages |
| 22 // with identical meaning will have identical representation. | 22 // with identical meaning will have identical representation. |
| 23 func Normalize(spec *vpython.Spec) error { | 23 func Normalize(spec *vpython.Spec) error { |
| 24 sort.Sort(specPackageSlice(spec.Wheel)) | 24 sort.Sort(specPackageSlice(spec.Wheel)) |
| 25 | 25 |
| 26 // No duplicate packages. Since we're sorted, we can just check for no | 26 // No duplicate packages. Since we're sorted, we can just check for no |
| 27 // immediate repetitions. | 27 // immediate repetitions. |
| 28 for i, pkg := range spec.Wheel { | 28 for i, pkg := range spec.Wheel { |
| 29 » » if i > 0 && pkg.Path == spec.Wheel[i-1].Path { | 29 » » if i > 0 && pkg.Name == spec.Wheel[i-1].Name { |
| 30 return errors.Reason("duplicate spec entries for package
%(path)q"). | 30 return errors.Reason("duplicate spec entries for package
%(path)q"). |
| 31 » » » » D("path", pkg.Path). | 31 » » » » D("name", pkg.Name). |
| 32 Err() | 32 Err() |
| 33 } | 33 } |
| 34 } | 34 } |
| 35 | 35 |
| 36 return nil | 36 return nil |
| 37 } | 37 } |
| 38 | 38 |
| 39 // Hash hashes the contents of the supplied "spec" and returns the result as | 39 // Hash hashes the contents of the supplied "spec" and returns the result as |
| 40 // a hex-encoded string. | 40 // a hex-encoded string. |
| 41 func Hash(spec *vpython.Spec) string { | 41 func Hash(spec *vpython.Spec) string { |
| 42 data, err := proto.Marshal(spec) | 42 data, err := proto.Marshal(spec) |
| 43 if err != nil { | 43 if err != nil { |
| 44 panic(fmt.Errorf("failed to marshal proto: %v", err)) | 44 panic(fmt.Errorf("failed to marshal proto: %v", err)) |
| 45 } | 45 } |
| 46 hash := sha256.Sum256(data) | 46 hash := sha256.Sum256(data) |
| 47 return hex.EncodeToString(hash[:]) | 47 return hex.EncodeToString(hash[:]) |
| 48 } | 48 } |
| 49 | 49 |
| 50 type specPackageSlice []*vpython.Spec_Package | 50 type specPackageSlice []*vpython.Spec_Package |
| 51 | 51 |
| 52 func (s specPackageSlice) Len() int { return len(s) } | 52 func (s specPackageSlice) Len() int { return len(s) } |
| 53 func (s specPackageSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } | 53 func (s specPackageSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
| 54 | 54 |
| 55 func (s specPackageSlice) Less(i, j int) bool { | 55 func (s specPackageSlice) Less(i, j int) bool { |
| 56 return sortby.Chain{ | 56 return sortby.Chain{ |
| 57 » » func(i, j int) bool { return s[i].Path < s[j].Path }, | 57 » » func(i, j int) bool { return s[i].Name < s[j].Name }, |
| 58 func(i, j int) bool { return s[i].Version < s[j].Version }, | 58 func(i, j int) bool { return s[i].Version < s[j].Version }, |
| 59 }.Use(i, j) | 59 }.Use(i, j) |
| 60 } | 60 } |
| OLD | NEW |