Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(411)

Side by Side Diff: vpython/spec/spec.go

Issue 2705623003: vpython: Add environment spec package. (Closed)
Patch Set: env to vpython, comments Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « vpython/spec/load_test.go ('k') | vpython/spec/spec_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 }
OLDNEW
« no previous file with comments | « vpython/spec/load_test.go ('k') | vpython/spec/spec_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698