Chromium Code Reviews| 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/env" | |
| 14 | |
| 15 "github.com/luci/luci-go/common/errors" | |
| 16 | |
| 17 "github.com/golang/protobuf/proto" | |
| 18 ) | |
| 19 | |
| 20 // Normalize normalizes the specification Message such that two messages | |
| 21 // with identical meaning will have the same identical representation. | |
|
iannucci
2017/02/21 09:09:55
"same identical"
dnj
2017/02/22 07:37:19
Done.
| |
| 22 func Normalize(spec *env.Spec) error { | |
| 23 sort.Sort(envSpecPackageSlice(spec.Wheel)) | |
| 24 | |
| 25 // No duplicate packages. Since we're sorted, we can just check for no | |
| 26 // immediate repetitions. | |
| 27 for i, pkg := range spec.Wheel { | |
| 28 if i > 0 && pkg.Path == spec.Wheel[i-1].Path { | |
| 29 return errors.Reason("duplicate spec entries for package %(path)q"). | |
| 30 D("path", pkg.Path). | |
| 31 Err() | |
| 32 } | |
| 33 } | |
| 34 | |
| 35 return nil | |
| 36 } | |
| 37 | |
| 38 // Hash hashes the contents of the supplied "spec" and returns the result as | |
| 39 // a hex-encoded string. | |
| 40 func Hash(spec *env.Spec) string { | |
| 41 data, err := proto.Marshal(spec) | |
| 42 if err != nil { | |
| 43 panic(fmt.Errorf("failed to marshal proto: %v", err)) | |
| 44 } | |
| 45 hash := sha256.Sum256(data) | |
| 46 return hex.EncodeToString(hash[:]) | |
| 47 } | |
| 48 | |
| 49 type envSpecPackageSlice []*env.Spec_Package | |
| 50 | |
| 51 func (s envSpecPackageSlice) Len() int { return len(s) } | |
| 52 func (s envSpecPackageSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } | |
| 53 func (s envSpecPackageSlice) Less(i, j int) bool { | |
|
iannucci
2017/02/21 09:09:55
try sortby! https://github.com/luci/luci-go/tree/m
dnj
2017/02/22 07:37:19
Oh cool.
| |
| 54 // Sort by Path, then Version. | |
| 55 if s[i].Path >= s[j].Path { | |
| 56 return false | |
| 57 } | |
| 58 return s[i].Version >= s[j].Version | |
| 59 } | |
| OLD | NEW |