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

Unified Diff: vpython/spec/spec.go

Issue 2705623003: vpython: Add environment spec package. (Closed)
Patch Set: 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
Index: vpython/spec/spec.go
diff --git a/vpython/spec/spec.go b/vpython/spec/spec.go
new file mode 100644
index 0000000000000000000000000000000000000000..20392518d7d2991168bd7c472fc9e2cdac5a21f1
--- /dev/null
+++ b/vpython/spec/spec.go
@@ -0,0 +1,59 @@
+// 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/env"
+
+ "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 the same identical representation.
iannucci 2017/02/21 09:09:55 "same identical"
dnj 2017/02/22 07:37:19 Done.
+func Normalize(spec *env.Spec) error {
+ sort.Sort(envSpecPackageSlice(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 *env.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 envSpecPackageSlice []*env.Spec_Package
+
+func (s envSpecPackageSlice) Len() int { return len(s) }
+func (s envSpecPackageSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
+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.
+ // Sort by Path, then Version.
+ if s[i].Path >= s[j].Path {
+ return false
+ }
+ return s[i].Version >= s[j].Version
+}
« vpython/spec/load.go ('K') | « 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