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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « vpython/spec/load_test.go ('k') | vpython/spec/spec_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: vpython/spec/spec.go
diff --git a/vpython/spec/spec.go b/vpython/spec/spec.go
new file mode 100644
index 0000000000000000000000000000000000000000..8fdc79e19a945fe05d67c9fe288c2a5ce7bd311e
--- /dev/null
+++ b/vpython/spec/spec.go
@@ -0,0 +1,60 @@
+// Copyright 2017 The LUCI Authors. All rights reserved.
+// Use of this source code is governed under the Apache License, Version 2.0
+// that can be found in the LICENSE file.
+
+package spec
+
+import (
+ "crypto/sha256"
+ "encoding/hex"
+ "fmt"
+ "sort"
+
+ "github.com/luci/luci-go/vpython/api/vpython"
+
+ "github.com/luci/luci-go/common/data/sortby"
+ "github.com/luci/luci-go/common/errors"
+
+ "github.com/golang/protobuf/proto"
+)
+
+// Normalize normalizes the specification Message such that two messages
+// with identical meaning will have identical representation.
+func Normalize(spec *vpython.Spec) error {
+ sort.Sort(specPackageSlice(spec.Wheel))
+
+ // No duplicate packages. Since we're sorted, we can just check for no
+ // immediate repetitions.
+ for i, pkg := range spec.Wheel {
+ if i > 0 && pkg.Path == spec.Wheel[i-1].Path {
+ return errors.Reason("duplicate spec entries for package %(path)q").
+ D("path", pkg.Path).
+ Err()
+ }
+ }
+
+ return nil
+}
+
+// Hash hashes the contents of the supplied "spec" and returns the result as
+// a hex-encoded string.
+func Hash(spec *vpython.Spec) string {
+ data, err := proto.Marshal(spec)
+ if err != nil {
+ panic(fmt.Errorf("failed to marshal proto: %v", err))
+ }
+ hash := sha256.Sum256(data)
+ return hex.EncodeToString(hash[:])
+}
+
+type specPackageSlice []*vpython.Spec_Package
+
+func (s specPackageSlice) Len() int { return len(s) }
+func (s specPackageSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
+
+func (s specPackageSlice) Less(i, j int) bool {
+ return sortby.Chain{
+ func(i, j int) bool { return s[i].Path < s[j].Path },
+ func(i, j int) bool { return s[i].Version < s[j].Version },
+ }.Use(i, j)
+}
« 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