| 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" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 if i > 0 && pkg.Name == spec.Wheel[i-1].Name { | 36 if i > 0 && pkg.Name == spec.Wheel[i-1].Name { |
| 37 return errors.Reason("duplicate spec entries for package
%(path)q"). | 37 return errors.Reason("duplicate spec entries for package
%(path)q"). |
| 38 D("name", pkg.Name). | 38 D("name", pkg.Name). |
| 39 Err() | 39 Err() |
| 40 } | 40 } |
| 41 } | 41 } |
| 42 | 42 |
| 43 return nil | 43 return nil |
| 44 } | 44 } |
| 45 | 45 |
| 46 // Hash hashes the contents of the supplied "spec" and returns the result as | 46 // Hash hashes the contents of the supplied "spec" and "rt" and returns the |
| 47 // a hex-encoded string. | 47 // result as a hex-encoded string. |
| 48 // | 48 // |
| 49 // If not empty, the contents of extra are prefixed to hash string. This can | 49 // If not empty, the contents of extra are prefixed to hash string. This can |
| 50 // be used to factor additional influences into the spec hash. | 50 // be used to factor additional influences into the spec hash. |
| 51 func Hash(spec *vpython.Spec, extra string) string { | 51 func Hash(spec *vpython.Spec, rt *vpython.Runtime, extra string) string { |
| 52 » data, err := proto.Marshal(spec) | 52 » mustMarshal := func(msg proto.Message) []byte { |
| 53 » if err != nil { | 53 » » data, err := proto.Marshal(msg) |
| 54 » » panic(fmt.Errorf("failed to marshal proto: %v", err)) | 54 » » if err != nil { |
| 55 » » » panic(fmt.Errorf("failed to marshal proto: %v", err)) |
| 56 » » } |
| 57 » » return data |
| 55 } | 58 } |
| 59 specData := mustMarshal(spec) |
| 60 rtData := mustMarshal(rt) |
| 56 | 61 |
| 57 mustWrite := func(v int, err error) { | 62 mustWrite := func(v int, err error) { |
| 58 if err != nil { | 63 if err != nil { |
| 59 panic(fmt.Errorf("impossible: %s", err)) | 64 panic(fmt.Errorf("impossible: %s", err)) |
| 60 } | 65 } |
| 61 } | 66 } |
| 62 | 67 |
| 63 hash := sha256.New() | 68 hash := sha256.New() |
| 64 if extra != "" { | 69 if extra != "" { |
| 65 mustWrite(fmt.Fprintf(hash, "%s:", extra)) | 70 mustWrite(fmt.Fprintf(hash, "%s:", extra)) |
| 66 } | 71 } |
| 67 mustWrite(fmt.Fprintf(hash, "%s:", vpython.Version)) | 72 mustWrite(fmt.Fprintf(hash, "%s:", vpython.Version)) |
| 68 » mustWrite(hash.Write(data)) | 73 » mustWrite(hash.Write(specData)) |
| 74 » mustWrite(hash.Write([]byte(":"))) |
| 75 » mustWrite(hash.Write(rtData)) |
| 69 return hex.EncodeToString(hash.Sum(nil)) | 76 return hex.EncodeToString(hash.Sum(nil)) |
| 70 } | 77 } |
| 71 | 78 |
| 72 type specPackageSlice []*vpython.Spec_Package | 79 type specPackageSlice []*vpython.Spec_Package |
| 73 | 80 |
| 74 func (s specPackageSlice) Len() int { return len(s) } | 81 func (s specPackageSlice) Len() int { return len(s) } |
| 75 func (s specPackageSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } | 82 func (s specPackageSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
| 76 | 83 |
| 77 func (s specPackageSlice) Less(i, j int) bool { | 84 func (s specPackageSlice) Less(i, j int) bool { |
| 78 return sortby.Chain{ | 85 return sortby.Chain{ |
| 79 func(i, j int) bool { return s[i].Name < s[j].Name }, | 86 func(i, j int) bool { return s[i].Name < s[j].Name }, |
| 80 func(i, j int) bool { return s[i].Version < s[j].Version }, | 87 func(i, j int) bool { return s[i].Version < s[j].Version }, |
| 81 }.Use(i, j) | 88 }.Use(i, j) |
| 82 } | 89 } |
| OLD | NEW |