]> xenbits.xensource.com Git - people/gdunlap/xsatool/commitdiff
XSAMeta: Keep SupportedVersions sorted in reverse order
authorGeorge Dunlap <george.dunlap@citrix.com>
Fri, 16 Jun 2017 10:20:27 +0000 (11:20 +0100)
committerGeorge Dunlap <george.dunlap@citrix.com>
Fri, 16 Jun 2017 10:20:27 +0000 (11:20 +0100)
There are many situations where we want to test things on master first
and then go backwards through supported versions (e.g., building,
looking for backports, &c).  Keep the SupportedVersions in this order.

To do this:
 - Refactor IsGreaterEqualThen to be usable by both it and IsGreaterThan
 - Make a XenVersionSlice type which implements sort.Interface, using the above
 - Re-sort XSAMeta.SupportedVersions on insert and load.

Also:

 - Have XSAMeta.NewRecipe() add the new SupportedVersions, rather
than having the caller add it manuall.

 - Keep XenVersion.PrevVersion() around, even though we're not using
 it for now, in case it becomes useful in the future.

Signed-off-by: George Dunlap <george.dunlap@citrix.com>
meta.go
xen.go
xsa.go

diff --git a/meta.go b/meta.go
index 9588b3240059a652af9a65857040b1b6284282ef..73f585130dba69aa7a2be061e65cac3bd49228e2 100644 (file)
--- a/meta.go
+++ b/meta.go
@@ -6,6 +6,7 @@ import (
        "io/ioutil"
        "os"
        "path"
+       "sort"
 )
 
 type Tree string
@@ -214,7 +215,7 @@ func (sm *SecurityMeta) AddXSA(xsa *XSAMeta) {
 
 type XSAMeta struct {
        XSA               int
-       SupportedVersions []XenVersion
+       SupportedVersions XenVersionSlice
        Trees             []Tree
        Recipes           map[XenVersion]*Recipe
        // Not exported
@@ -228,6 +229,11 @@ func (m *XSAMeta) GetRecipe(v XenVersion) *Recipe {
 func (m *XSAMeta) NewRecipe(v XenVersion) *Recipe {
        var r Recipe
 
+       // Insert v to SupportedVersions in-order
+       // Lazy version: Add it to the end then sort
+       m.SupportedVersions = append(m.SupportedVersions, v)
+       sort.Sort(m.SupportedVersions)
+       
        r.xsa = m.XSA
        r.XenVersion = v
        r.Recipes = make(map[Tree]*TreeRecipe)
@@ -283,6 +289,8 @@ func (xm *XSAMeta) Load(xsanum int) (err error) {
                xm.Recipes[i].xsa = xm.XSA
        }
 
+       sort.Sort(xm.SupportedVersions)
+
        return
 }
 
diff --git a/xen.go b/xen.go
index 91d4e5dea5a2a2d1686328a48e358369abc9bdc7..eaba4d64a4099725982ccfd9d690d1a61ef31382 100644 (file)
--- a/xen.go
+++ b/xen.go
@@ -44,8 +44,7 @@ func (v XenVersion) Check() bool {
        return err == nil
 }
 
-// a.IsGreaterEqualThan(b) returns a >= b
-func (v XenVersion) IsGreaterEqualThan(v2 XenVersion) bool {
+func (v XenVersion) gt(v2 XenVersion, orEqual bool) bool {
        majorA, minorA, err := v.parse()
        if err != nil {
                fmt.Printf("Parsing: %v\n", err)
@@ -66,13 +65,69 @@ func (v XenVersion) IsGreaterEqualThan(v2 XenVersion) bool {
                return false
        }
 
-       if minorA >= minorB {
+       // Now we know majors are equal
+
+       if minorA > minorB {
+               return true
+       }
+
+       if orEqual && minorA == minorB {
                return true
        }
 
        return false
 }
 
+// a.IsGreaterEqualThan(b) returns a >= b
+func (v XenVersion) IsGreaterEqualThan(v2 XenVersion) bool {
+       return v.gt(v2, true)
+}
+
+// a.IsGreaterEqualThan(b) returns a > b
+func (v XenVersion) IsGreaterThan(v2 XenVersion) bool {
+       return v.gt(v2, false)
+}
+
+func (v XenVersion) PrevVersion() (next XenVersion, err error) {
+       major, minor, err := v.parse()
+       if err != nil {
+               err = fmt.Errorf("Parsing version %v: %v\n", v, err)
+               return
+       }
+
+       if minor > 0 {
+               minor--
+               next = XenVersion(fmt.Sprintf("%d.%d\n", major, minor))
+               return
+       }
+
+       // FIXME If we ever move to 5.x, or need to extend this to
+       // 3.x, we'll have to sort something out here; probably a
+       // table mapping major version -> highest release minor
+       err = fmt.Errorf("Don't know how to roll back major number")
+       return
+}
+
+type XenVersionSlice []XenVersion
+
+// Len is the number of elements in the collection.
+func (vl XenVersionSlice) Len() int {
+       return len(vl)
+}
+
+// Less reports whether the element with
+// index i should sort before the element with index j.
+func (vl XenVersionSlice) Less(i, j int) bool {
+       return vl[i].IsGreaterThan(vl[j])
+}
+
+// Swap swaps the elements with indexes i and j.
+func (vl XenVersionSlice) Swap(i, j int) {
+       tmp := vl[j]
+       vl[j] = vl[i]
+       vl[i] = tmp
+}
+
 type XenVersionFull string
 
 func (v XenVersionFull) String() string {
diff --git a/xsa.go b/xsa.go
index 9069f93cb953592cc916b5c154e3ccda915f1423..5ea8b5e27d052e6a298a189e1e4b9b13d3bf6e7b 100644 (file)
--- a/xsa.go
+++ b/xsa.go
@@ -115,8 +115,7 @@ func MainInit(xsa *XSAMeta, args []string) (ret int) {
        for v := range sm.Versions {
                vm := sm.Versions[v]
 
-               xsa.SupportedVersions = append(xsa.SupportedVersions, v)
-
+               // This will add v to xsa.SupportedVersions
                r := xsa.NewRecipe(v)
 
                for _, t := range xsa.Trees {
@@ -145,7 +144,7 @@ func MainInit(xsa *XSAMeta, args []string) (ret int) {
                }
 
                if err := r.ApplyBaselines("xsa"); err != nil {
-                       fmt.Printf("Making recipe baseline for version %v\n", v)
+                       fmt.Printf("Making recipe baseline for version %v: %v\n", v, err)
                        return 1
                }