mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-05-08 22:30:41 +00:00
Merge branch 'simonaquino-sort_snapshots_time'
This commit is contained in:
+56
-12
@@ -7,6 +7,46 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Snapshot sorting methods
|
||||||
|
const (
|
||||||
|
SortName = iota
|
||||||
|
SortTime
|
||||||
|
)
|
||||||
|
|
||||||
|
type snapshotListToSort struct {
|
||||||
|
list []*deb.Snapshot
|
||||||
|
sortMethod int
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseSortMethod(sortMethod_string string) (int, error) {
|
||||||
|
switch sortMethod_string {
|
||||||
|
case "time", "Time":
|
||||||
|
return SortTime, nil
|
||||||
|
case "name", "Name":
|
||||||
|
return SortName, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1, fmt.Errorf("sorting method \"%s\" unknown", sortMethod_string)
|
||||||
|
}
|
||||||
|
|
||||||
|
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 aptlySnapshotList(cmd *commander.Command, args []string) error {
|
func aptlySnapshotList(cmd *commander.Command, args []string) error {
|
||||||
var err error
|
var err error
|
||||||
if len(args) != 0 {
|
if len(args) != 0 {
|
||||||
@@ -15,32 +55,35 @@ func aptlySnapshotList(cmd *commander.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
raw := cmd.Flag.Lookup("raw").Value.Get().(bool)
|
raw := cmd.Flag.Lookup("raw").Value.Get().(bool)
|
||||||
|
sortMethod_string := cmd.Flag.Lookup("sort").Value.Get().(string)
|
||||||
|
|
||||||
snapshots := make([]string, context.CollectionFactory().SnapshotCollection().Len())
|
snapshotsToSort := &snapshotListToSort{}
|
||||||
|
snapshotsToSort.list = make([]*deb.Snapshot, context.CollectionFactory().SnapshotCollection().Len())
|
||||||
|
snapshotsToSort.sortMethod, err = ParseSortMethod(sortMethod_string)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
i := 0
|
i := 0
|
||||||
context.CollectionFactory().SnapshotCollection().ForEach(func(snapshot *deb.Snapshot) error {
|
context.CollectionFactory().SnapshotCollection().ForEach(func(snapshot *deb.Snapshot) error {
|
||||||
if raw {
|
snapshotsToSort.list[i] = snapshot
|
||||||
snapshots[i] = snapshot.Name
|
|
||||||
} else {
|
|
||||||
snapshots[i] = snapshot.String()
|
|
||||||
}
|
|
||||||
i++
|
i++
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
sort.Strings(snapshots)
|
sort.Sort(snapshotsToSort)
|
||||||
|
|
||||||
if raw {
|
if raw {
|
||||||
for _, snapshot := range snapshots {
|
for _, snapshot := range snapshotsToSort.list {
|
||||||
fmt.Printf("%s\n", snapshot)
|
fmt.Printf("%s\n", snapshot.Name)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if len(snapshots) > 0 {
|
if len(snapshotsToSort.list) > 0 {
|
||||||
fmt.Printf("List of snapshots:\n")
|
fmt.Printf("List of snapshots:\n")
|
||||||
|
|
||||||
for _, snapshot := range snapshots {
|
for _, snapshot := range snapshotsToSort.list {
|
||||||
fmt.Printf(" * %s\n", snapshot)
|
fmt.Printf(" * %s\n", snapshot.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("\nTo get more information about snapshot, run `aptly snapshot show <name>`.\n")
|
fmt.Printf("\nTo get more information about snapshot, run `aptly snapshot show <name>`.\n")
|
||||||
@@ -67,6 +110,7 @@ Example:
|
|||||||
}
|
}
|
||||||
|
|
||||||
cmd.Flag.Bool("raw", false, "display list in machine-readable format")
|
cmd.Flag.Bool("raw", false, "display list in machine-readable format")
|
||||||
|
cmd.Flag.String("sort", "name", "display list in 'name' or creation 'time' order")
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-1
@@ -1,7 +1,7 @@
|
|||||||
.\" generated with Ronn/v0.7.3
|
.\" generated with Ronn/v0.7.3
|
||||||
.\" http://github.com/rtomayko/ronn/tree/0.7.3
|
.\" http://github.com/rtomayko/ronn/tree/0.7.3
|
||||||
.
|
.
|
||||||
.TH "APTLY" "1" "June 2014" "" ""
|
.TH "APTLY" "1" "July 2014" "" ""
|
||||||
.
|
.
|
||||||
.SH "NAME"
|
.SH "NAME"
|
||||||
\fBaptly\fR \- Debian repository management tool
|
\fBaptly\fR \- Debian repository management tool
|
||||||
@@ -531,6 +531,10 @@ Options:
|
|||||||
\-\fBraw\fR=false
|
\-\fBraw\fR=false
|
||||||
display list in machine\-readable format
|
display list in machine\-readable format
|
||||||
.
|
.
|
||||||
|
.TP
|
||||||
|
\-\fBsort\fR=name
|
||||||
|
display list in \(cqname\(cq or creation \(cqtime\(cq order
|
||||||
|
.
|
||||||
.SH "SHOWS DETAILS ABOUT SNAPSHOT"
|
.SH "SHOWS DETAILS ABOUT SNAPSHOT"
|
||||||
\fBaptly\fR \fBsnapshot\fR \fBshow\fR \fIname\fR
|
\fBaptly\fR \fBsnapshot\fR \fBshow\fR \fIname\fR
|
||||||
.
|
.
|
||||||
|
|||||||
+14
-3
@@ -84,6 +84,8 @@ class BaseTest(object):
|
|||||||
|
|
||||||
outputMatchPrepare = None
|
outputMatchPrepare = None
|
||||||
|
|
||||||
|
captureResults = False
|
||||||
|
|
||||||
def test(self):
|
def test(self):
|
||||||
self.prepare()
|
self.prepare()
|
||||||
self.run()
|
self.run()
|
||||||
@@ -181,12 +183,21 @@ class BaseTest(object):
|
|||||||
def expand_environ(self, gold):
|
def expand_environ(self, gold):
|
||||||
return string.Template(gold).substitute(os.environ)
|
return string.Template(gold).substitute(os.environ)
|
||||||
|
|
||||||
|
def get_gold_filename(self, gold_name="gold"):
|
||||||
|
return os.path.join(os.path.dirname(inspect.getsourcefile(self.__class__)), self.__class__.__name__ + "_" + gold_name)
|
||||||
|
|
||||||
def get_gold(self, gold_name="gold"):
|
def get_gold(self, gold_name="gold"):
|
||||||
gold = os.path.join(os.path.dirname(inspect.getsourcefile(self.__class__)), self.__class__.__name__ + "_" + gold_name)
|
return self.gold_processor(open(self.get_gold_filename(gold_name), "r").read())
|
||||||
return self.gold_processor(open(gold, "r").read())
|
|
||||||
|
|
||||||
def check_output(self):
|
def check_output(self):
|
||||||
self.verify_match(self.get_gold(), self.output, match_prepare=self.outputMatchPrepare)
|
try:
|
||||||
|
self.verify_match(self.get_gold(), self.output, match_prepare=self.outputMatchPrepare)
|
||||||
|
except:
|
||||||
|
if self.captureResults:
|
||||||
|
with open(self.get_gold_filename(), "w") as f:
|
||||||
|
f.write(self.output)
|
||||||
|
else:
|
||||||
|
raise
|
||||||
|
|
||||||
def check_cmd_output(self, command, gold_name, match_prepare=None, expected_code=0):
|
def check_cmd_output(self, command, gold_name, match_prepare=None, expected_code=0):
|
||||||
self.verify_match(self.get_gold(gold_name), self.run_cmd(command, expected_code=expected_code), match_prepare)
|
self.verify_match(self.get_gold(gold_name), self.run_cmd(command, expected_code=expected_code), match_prepare)
|
||||||
|
|||||||
+34
-7
@@ -4,6 +4,7 @@ import glob
|
|||||||
import importlib
|
import importlib
|
||||||
import os
|
import os
|
||||||
import inspect
|
import inspect
|
||||||
|
import fnmatch
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from lib import BaseTest
|
from lib import BaseTest
|
||||||
@@ -15,11 +16,11 @@ except ImportError:
|
|||||||
return s
|
return s
|
||||||
|
|
||||||
|
|
||||||
def run(include_long_tests=False, tests=None):
|
def run(include_long_tests=False, capture_results=False, tests=None, filters=None):
|
||||||
"""
|
"""
|
||||||
Run system test.
|
Run system test.
|
||||||
"""
|
"""
|
||||||
if tests is None:
|
if not tests:
|
||||||
tests = glob.glob("t*_*")
|
tests = glob.glob("t*_*")
|
||||||
fails = []
|
fails = []
|
||||||
numTests = numFailed = numSkipped = 0
|
numTests = numFailed = numSkipped = 0
|
||||||
@@ -34,6 +35,17 @@ def run(include_long_tests=False, tests=None):
|
|||||||
if not (inspect.isclass(o) and issubclass(o, BaseTest) and o is not BaseTest):
|
if not (inspect.isclass(o) and issubclass(o, BaseTest) and o is not BaseTest):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
if filters:
|
||||||
|
matches = False
|
||||||
|
|
||||||
|
for filt in filters:
|
||||||
|
if fnmatch.fnmatch(o.__name__, filt):
|
||||||
|
matches = True
|
||||||
|
break
|
||||||
|
|
||||||
|
if not matches:
|
||||||
|
continue
|
||||||
|
|
||||||
t = o()
|
t = o()
|
||||||
if t.longTest and not include_long_tests or not t.fixture_available():
|
if t.longTest and not include_long_tests or not t.fixture_available():
|
||||||
numSkipped += 1
|
numSkipped += 1
|
||||||
@@ -44,6 +56,7 @@ def run(include_long_tests=False, tests=None):
|
|||||||
sys.stdout.write("%s:%s... " % (test, o.__name__))
|
sys.stdout.write("%s:%s... " % (test, o.__name__))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
t.captureResults = capture_results
|
||||||
t.test()
|
t.test()
|
||||||
except BaseException, e:
|
except BaseException, e:
|
||||||
numFailed += 1
|
numFailed += 1
|
||||||
@@ -69,11 +82,25 @@ def run(include_long_tests=False, tests=None):
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
os.chdir(os.path.realpath(os.path.dirname(sys.argv[0])))
|
os.chdir(os.path.realpath(os.path.dirname(sys.argv[0])))
|
||||||
include_long_tests = False
|
include_long_tests = False
|
||||||
|
capture_results = False
|
||||||
tests = None
|
tests = None
|
||||||
if len(sys.argv) > 1:
|
args = sys.argv[1:]
|
||||||
if sys.argv[1] == "--long":
|
|
||||||
|
while len(args) > 0 and args[0].startswith("--"):
|
||||||
|
if args[0] == "--long":
|
||||||
include_long_tests = True
|
include_long_tests = True
|
||||||
tests = sys.argv[2:]
|
elif args[0] == "--capture":
|
||||||
|
capture_results = True
|
||||||
|
|
||||||
|
args = args[1:]
|
||||||
|
|
||||||
|
tests = []
|
||||||
|
filters = []
|
||||||
|
|
||||||
|
for arg in args:
|
||||||
|
if arg.startswith('t'):
|
||||||
|
tests.append(arg)
|
||||||
else:
|
else:
|
||||||
tests = sys.argv[1:]
|
filters.append(arg)
|
||||||
run(include_long_tests, tests)
|
|
||||||
|
run(include_long_tests, capture_results, tests, filters)
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
snap2
|
||||||
|
snap1
|
||||||
|
snap3
|
||||||
|
snap4
|
||||||
|
snap5
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
List of snapshots:
|
||||||
|
* [snap2]: Snapshot from mirror [wheezy-contrib]: http://mirror.yandex.ru/debian/ wheezy
|
||||||
|
* [snap1]: Snapshot from mirror [wheezy-main]: http://mirror.yandex.ru/debian/ wheezy
|
||||||
|
* [snap3]: Merged from sources: 'snap1', 'snap2'
|
||||||
|
* [snap4]: Pulled into 'snap1' with 'snap2' as source, pull request was: 'mame unrar'
|
||||||
|
* [snap5]: Snapshot from local repo [local-repo]
|
||||||
|
|
||||||
|
To get more information about snapshot, run `aptly snapshot show <name>`.
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
ERROR: sorting method "planet" unknown
|
||||||
@@ -47,3 +47,46 @@ class ListSnapshot4Test(BaseTest):
|
|||||||
list snapshots: raw empty list
|
list snapshots: raw empty list
|
||||||
"""
|
"""
|
||||||
runCmd = "aptly snapshot -raw list"
|
runCmd = "aptly snapshot -raw list"
|
||||||
|
|
||||||
|
|
||||||
|
class ListSnapshot5Test(BaseTest):
|
||||||
|
"""
|
||||||
|
list snapshots: raw regular list sorted by time
|
||||||
|
"""
|
||||||
|
fixtureDB = True
|
||||||
|
fixtureCmds = [
|
||||||
|
"aptly snapshot create snap2 from mirror wheezy-main",
|
||||||
|
"aptly snapshot create snap1 from mirror wheezy-contrib",
|
||||||
|
"aptly snapshot merge snap3 snap1 snap2",
|
||||||
|
"aptly snapshot pull snap1 snap2 snap4 mame unrar",
|
||||||
|
"aptly repo create local-repo",
|
||||||
|
"aptly repo add local-repo ${files}",
|
||||||
|
"aptly snapshot create snap5 from repo local-repo",
|
||||||
|
]
|
||||||
|
runCmd = "aptly -raw -sort=time snapshot list"
|
||||||
|
|
||||||
|
|
||||||
|
class ListSnapshot6Test(BaseTest):
|
||||||
|
"""
|
||||||
|
list snapshots: regular list sorted by time
|
||||||
|
"""
|
||||||
|
fixtureDB = True
|
||||||
|
fixtureCmds = [
|
||||||
|
"aptly snapshot create snap2 from mirror wheezy-contrib",
|
||||||
|
"aptly snapshot create snap1 from mirror wheezy-main",
|
||||||
|
"aptly snapshot merge snap3 snap1 snap2",
|
||||||
|
"aptly snapshot pull snap1 snap2 snap4 mame unrar",
|
||||||
|
"aptly repo create local-repo",
|
||||||
|
"aptly repo add local-repo ${files}",
|
||||||
|
"aptly snapshot create snap5 from repo local-repo",
|
||||||
|
]
|
||||||
|
runCmd = "aptly -sort=time snapshot list"
|
||||||
|
|
||||||
|
|
||||||
|
class ListSnapshot7Test(BaseTest):
|
||||||
|
"""
|
||||||
|
list snapshots: wrong parameter sort
|
||||||
|
"""
|
||||||
|
runCmd = "aptly -sort=planet snapshot list"
|
||||||
|
expectedCode = 1
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user