Add package refs to mirror state.

This commit is contained in:
Andrey Smirnov
2013-12-19 23:35:54 +04:00
parent f7f4ba1691
commit 7940f5e698
4 changed files with 81 additions and 13 deletions
+9 -1
View File
@@ -76,6 +76,14 @@ func aptlyMirrorShow(cmd *commander.Command, args []string) {
fmt.Printf("Distribution: %s\n", repo.Distribution) fmt.Printf("Distribution: %s\n", repo.Distribution)
fmt.Printf("Components: %s\n", strings.Join(repo.Components, ", ")) fmt.Printf("Components: %s\n", strings.Join(repo.Components, ", "))
fmt.Printf("Architectures: %s\n", strings.Join(repo.Architectures, ", ")) fmt.Printf("Architectures: %s\n", strings.Join(repo.Architectures, ", "))
if repo.LastDownloadDate.IsZero() {
fmt.Printf("Last update: never\n")
} else {
fmt.Printf("Last update: %s\n", repo.LastDownloadDate.Format("2006-01-02 15:04:05 MST"))
}
if repo.PackageRefs != nil {
fmt.Printf("Number of packages: %d\n", repo.PackageRefs.Len())
}
fmt.Printf("\nInformation from release file:\n") fmt.Printf("\nInformation from release file:\n")
for name, value := range repo.Meta { for name, value := range repo.Meta {
@@ -193,7 +201,7 @@ func makeCmdMirror() *commander.Commander {
makeCmdMirrorCreate(), makeCmdMirrorCreate(),
makeCmdMirrorList(), makeCmdMirrorList(),
makeCmdMirrorShow(), makeCmdMirrorShow(),
//makeCmdMirrorDelete(), //makeCmdMirrorDeestroy(),
makeCmdMirrorUpdate(), makeCmdMirrorUpdate(),
}, },
Flag: flag.NewFlagSet("aptly-mirror", flag.ExitOnError), Flag: flag.NewFlagSet("aptly-mirror", flag.ExitOnError),
+44 -2
View File
@@ -1,7 +1,9 @@
package debian package debian
import ( import (
"bytes"
"fmt" "fmt"
"sort"
) )
// PackageList is list of unique (by key) packages // PackageList is list of unique (by key) packages
@@ -40,7 +42,47 @@ func (l *PackageList) ForEach(handler func(*Package)) {
} }
} }
// Length returns number of packages in the list // Len returns number of packages in the list
func (l *PackageList) Length() int { func (l *PackageList) Len() int {
return len(l.packages) return len(l.packages)
} }
// PackageRefList is a list of keys of packages, this is basis for snapshot
// and similar stuff
//
// Refs are sorted in lexographical order
type PackageRefList struct {
// List of package keys
Refs [][]byte
}
// NewPackageRefListFromPackageList creates PackageRefList from PackageList
func NewPackageRefListFromPackageList(list *PackageList) *PackageRefList {
reflist := &PackageRefList{}
reflist.Refs = make([][]byte, list.Len())
i := 0
for _, p := range list.packages {
reflist.Refs[i] = p.Key()
i++
}
sort.Sort(reflist)
return reflist
}
// Len returns number of refs
func (l *PackageRefList) Len() int {
return len(l.Refs)
}
// Swap swaps two refs
func (l *PackageRefList) Swap(i, j int) {
l.Refs[i], l.Refs[j] = l.Refs[j], l.Refs[i]
}
// Compare compares two refs in lexographical order
func (l *PackageRefList) Less(i, j int) bool {
return bytes.Compare(l.Refs[i], l.Refs[j]) < 0
}
+18 -8
View File
@@ -33,14 +33,14 @@ func (s *PackageListSuite) SetUpTest(c *C) {
s.p4 = NewPackageFromControlFile(para) s.p4 = NewPackageFromControlFile(para)
} }
func (s *PackageListSuite) TestAddLength(c *C) { func (s *PackageListSuite) TestAddLen(c *C) {
c.Check(s.list.Length(), Equals, 0) c.Check(s.list.Len(), Equals, 0)
c.Check(s.list.Add(s.p1), IsNil) c.Check(s.list.Add(s.p1), IsNil)
c.Check(s.list.Length(), Equals, 1) c.Check(s.list.Len(), Equals, 1)
c.Check(s.list.Add(s.p2), IsNil) c.Check(s.list.Add(s.p2), IsNil)
c.Check(s.list.Length(), Equals, 1) c.Check(s.list.Len(), Equals, 1)
c.Check(s.list.Add(s.p3), IsNil) c.Check(s.list.Add(s.p3), IsNil)
c.Check(s.list.Length(), Equals, 2) c.Check(s.list.Len(), Equals, 2)
c.Check(s.list.Add(s.p4), ErrorMatches, "conflict in package.*") c.Check(s.list.Add(s.p4), ErrorMatches, "conflict in package.*")
} }
@@ -48,10 +48,20 @@ func (s *PackageListSuite) TestForeach(c *C) {
s.list.Add(s.p1) s.list.Add(s.p1)
s.list.Add(s.p3) s.list.Add(s.p3)
length := 0 Len := 0
s.list.ForEach(func(*Package) { s.list.ForEach(func(*Package) {
length++ Len++
}) })
c.Check(length, Equals, 2) c.Check(Len, Equals, 2)
}
func (s *PackageListSuite) TestNewPackageRefList(c *C) {
s.list.Add(s.p1)
s.list.Add(s.p3)
reflist := NewPackageRefListFromPackageList(s.list)
c.Assert(reflist.Len(), Equals, 2)
c.Assert(reflist.Refs[0], DeepEquals, []byte(s.p1.Key()))
c.Assert(reflist.Refs[1], DeepEquals, []byte(s.p3.Key()))
} }
+10 -2
View File
@@ -12,6 +12,7 @@ import (
"log" "log"
"net/url" "net/url"
"strings" "strings"
"time"
) )
// RemoteRepo represents remote (fetchable) Debian repository. // RemoteRepo represents remote (fetchable) Debian repository.
@@ -32,7 +33,11 @@ type RemoteRepo struct {
// List of architectures to fetch, if empty, then fetch all architectures // List of architectures to fetch, if empty, then fetch all architectures
Architectures []string Architectures []string
// Meta-information about repository // Meta-information about repository
Meta debc.Paragraph Meta debc.Paragraph
// Last update date
LastDownloadDate time.Time
// "Snapshot" of current list of packages
PackageRefs *PackageRefList
archiveRootURL *url.URL archiveRootURL *url.URL
} }
@@ -168,7 +173,7 @@ func (repo *RemoteRepo) Download(d utils.Downloader, db database.Storage, packag
}) })
// Download all package files // Download all package files
ch := make(chan error, list.Length()) ch := make(chan error, list.Len())
count := 0 count := 0
list.ForEach(func(p *Package) { list.ForEach(func(p *Package) {
@@ -188,6 +193,9 @@ func (repo *RemoteRepo) Download(d utils.Downloader, db database.Storage, packag
count-- count--
} }
repo.LastDownloadDate = time.Now()
repo.PackageRefs = NewPackageRefListFromPackageList(list)
return nil return nil
} }