| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 package spec |
| 6 |
| 7 import ( |
| 8 "crypto/sha256" |
| 9 "encoding/hex" |
| 10 "fmt" |
| 11 "sort" |
| 12 |
| 13 "github.com/luci/luci-go/vpython/api/vpython" |
| 14 |
| 15 "github.com/luci/luci-go/common/data/sortby" |
| 16 "github.com/luci/luci-go/common/errors" |
| 17 |
| 18 "github.com/golang/protobuf/proto" |
| 19 ) |
| 20 |
| 21 // Normalize normalizes the specification Message such that two messages |
| 22 // with identical meaning will have identical representation. |
| 23 func Normalize(spec *vpython.Spec) error { |
| 24 sort.Sort(specPackageSlice(spec.Wheel)) |
| 25 |
| 26 // No duplicate packages. Since we're sorted, we can just check for no |
| 27 // immediate repetitions. |
| 28 for i, pkg := range spec.Wheel { |
| 29 if i > 0 && pkg.Path == spec.Wheel[i-1].Path { |
| 30 return errors.Reason("duplicate spec entries for package
%(path)q"). |
| 31 D("path", pkg.Path). |
| 32 Err() |
| 33 } |
| 34 } |
| 35 |
| 36 return nil |
| 37 } |
| 38 |
| 39 // Hash hashes the contents of the supplied "spec" and returns the result as |
| 40 // a hex-encoded string. |
| 41 func Hash(spec *vpython.Spec) string { |
| 42 data, err := proto.Marshal(spec) |
| 43 if err != nil { |
| 44 panic(fmt.Errorf("failed to marshal proto: %v", err)) |
| 45 } |
| 46 hash := sha256.Sum256(data) |
| 47 return hex.EncodeToString(hash[:]) |
| 48 } |
| 49 |
| 50 type specPackageSlice []*vpython.Spec_Package |
| 51 |
| 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] } |
| 54 |
| 55 func (s specPackageSlice) Less(i, j int) bool { |
| 56 return sortby.Chain{ |
| 57 func(i, j int) bool { return s[i].Path < s[j].Path }, |
| 58 func(i, j int) bool { return s[i].Version < s[j].Version }, |
| 59 }.Use(i, j) |
| 60 } |
| OLD | NEW |