Merge branch 'snapshot-api' of https://github.com/lebauce/aptly into lebauce-snapshot-api

Conflicts:
	api/router.go
	system/t12_api/__init__.py
This commit is contained in:
Andrey Smirnov
2015-01-22 21:29:58 +03:00
10 changed files with 768 additions and 78 deletions
+8 -2
View File
@@ -9,6 +9,8 @@ import (
"strings"
)
type Hash uint64
// Package is single instance of Debian package
type Package struct {
// Basic package properties
@@ -27,7 +29,7 @@ type Package struct {
// Is this udeb package
IsUdeb bool
// Hash of files section
FilesHash uint64
FilesHash Hash
// Is this >= 0.6 package?
V06Plus bool
// Offload fields
@@ -38,6 +40,10 @@ type Package struct {
collection *PackageCollection
}
func (h *Hash) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("\"%08x\"", *h)), nil
}
// NewPackageFromControlFile creates Package from parsed Debian control file
func NewPackageFromControlFile(input Stanza) *Package {
result := &Package{
@@ -398,7 +404,7 @@ func (p *Package) Files() PackageFiles {
// UpdateFiles saves new state of files
func (p *Package) UpdateFiles(files PackageFiles) {
p.files = &files
p.FilesHash = files.Hash()
p.FilesHash = Hash(files.Hash())
}
// Stanza creates original stanza from package
+56
View File
@@ -9,6 +9,7 @@ import (
"github.com/smira/aptly/utils"
"github.com/ugorji/go/codec"
"log"
"sort"
"strings"
"sync"
"time"
@@ -327,3 +328,58 @@ func (collection *SnapshotCollection) Drop(snapshot *Snapshot) error {
return collection.db.Delete(snapshot.RefKey())
}
// Snapshot sorting methods
const (
SortName = iota
SortTime
)
type snapshotListToSort struct {
list []*Snapshot
sortMethod int
}
func parseSortMethod(sortMethod string) (int, error) {
switch sortMethod {
case "time", "Time":
return SortTime, nil
case "name", "Name":
return SortName, nil
}
return -1, fmt.Errorf("sorting method \"%s\" unknown", sortMethod)
}
func (s snapshotListToSort) Swap(i, j int) {
s.list[i], s.list[j] = s.list[j], s.list[i]
}
func (s snapshotListToSort) Less(i, j int) bool {
switch s.sortMethod {
case SortName:
return s.list[i].Name < s.list[j].Name
case SortTime:
return s.list[i].CreatedAt.Before(s.list[j].CreatedAt)
}
panic("unknown sort method")
}
func (s snapshotListToSort) Len() int {
return len(s.list)
}
func (collection *SnapshotCollection) Sort(sortMethodString string) error {
var err error
snapshotsToSort := &snapshotListToSort{}
snapshotsToSort.list = collection.list
snapshotsToSort.sortMethod, err = parseSortMethod(sortMethodString)
if err != nil {
return err
}
sort.Sort(snapshotsToSort)
collection.list = snapshotsToSort.list
return err
}