SF.net SVN: gar:[24754] csw/mgar/gar/v2/go/src/package-gar-status/ package-gar-status.go

wahwah at users.sourceforge.net wahwah at users.sourceforge.net
Sun Mar 22 23:43:21 CET 2015


Revision: 24754
          http://sourceforge.net/p/gar/code/24754
Author:   wahwah
Date:     2015-03-22 22:43:21 +0000 (Sun, 22 Mar 2015)
Log Message:
-----------
package-gar-status: Sort by catalogname

Modified Paths:
--------------
    csw/mgar/gar/v2/go/src/package-gar-status/package-gar-status.go

Modified: csw/mgar/gar/v2/go/src/package-gar-status/package-gar-status.go
===================================================================
--- csw/mgar/gar/v2/go/src/package-gar-status/package-gar-status.go	2015-03-22 18:25:43 UTC (rev 24753)
+++ csw/mgar/gar/v2/go/src/package-gar-status/package-gar-status.go	2015-03-22 22:43:21 UTC (rev 24754)
@@ -7,15 +7,16 @@
 package main
 
 import (
+  "encoding/json"
+  "flag"
   "fmt"
-  "flag"
+  "log"
   "net/http"
-  "encoding/json"
-  "log"
   "opencsw/diskformat"
+  "os"
+  "sort"
+  "text/template"
   "time"
-  "text/template"
-  "os"
 )
 
 var outputFile string
@@ -52,16 +53,16 @@
   Pkginfo map[string]string    `json:"pkginfo"`
 }
 
-func FindOutIfPackageIsInGar(md5 diskformat.Md5Sum) (bool, error) {
+func GetPkgstats(md5 diskformat.Md5Sum) (PackageStats, error) {
   url := fmt.Sprintf("%s/srv4/%s/pkg-stats/", diskformat.PkgdbUrl, md5)
   log.Println("Fetching", url)
   var resp *http.Response
   resp, err := http.Get(url)
   if err != nil {
-    // Try again maybe?
+    log.Println("HTTP GET failed: ", err, " but we'll try again.")
     resp, err = http.Get(url)
     if err != nil {
-      return false, err
+      return PackageStats{}, err
     }
   }
   defer resp.Body.Close()
@@ -70,10 +71,9 @@
   dec := json.NewDecoder(resp.Body)
   if err := dec.Decode(&stats); err != nil {
     log.Println("Failed to decode JSON from", url, ":", err)
-    return false, err
+    return PackageStats{}, err
   }
-  _, inGar := stats.Pkginfo["OPENCSW_REPOSITORY"]
-  return inGar, nil
+  return stats, nil
 }
 
 type PackageWithExtraData struct {
@@ -81,11 +81,19 @@
   InGar bool
 }
 
+type ByCatalogname []PackageWithExtraData
+
 type TemplateData struct {
-  Pkgs []PackageWithExtraData
+  Pkgs ByCatalogname
   Date time.Time
 }
 
+func (a ByCatalogname) Len() int { return len(a) }
+func (a ByCatalogname) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
+func (a ByCatalogname) Less(i, j int) bool {
+  return a[i].Pkg.Catalogname < a[j].Pkg.Catalogname
+}
+
 func main() {
   log.Println("Program start")
   // Without flag.Parse(), -h doesn't work.
@@ -108,15 +116,17 @@
     var count = 0
     for _, pkg := range cws.Pkgs {
       log.Println("Processing", fmt.Sprintf("%+v", pkg))
-      if inGar, err := FindOutIfPackageIsInGar(pkg.Md5_sum); err != nil {
+      stats, err := GetPkgstats(pkg.Md5_sum)
+      if err != nil {
         log.Fatalln("Failed to fetch: ", pkg.Md5_sum, " because ", err)
-      } else {
-        log.Println("Result: ", pkg.Md5_sum, "is", inGar)
-        var pwed PackageWithExtraData
-        pwed.Pkg = pkg
-        pwed.InGar = inGar
-        inGarByPkgname[pkg.Catalogname] = pwed
       }
+      _, inGar := stats.Pkginfo["OPENCSW_REPOSITORY"]
+      log.Println("Result: ", pkg.Md5_sum, "is", inGar)
+      var pwed PackageWithExtraData
+      pwed.Pkg = pkg
+      pwed.InGar = inGar
+      inGarByPkgname[pkg.Catalogname] = pwed
+
       count += 1
       if testingRun && count > 10 {
         break
@@ -133,6 +143,7 @@
   for _, pkg := range inGarByPkgname {
     td.Pkgs = append(td.Pkgs, pkg)
   }
+  sort.Sort(ByCatalogname(td.Pkgs))
   td.Date = time.Now()
   if err := t.Execute(f, td); err != nil {
     log.Fatal(err)

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.



More information about the devel mailing list