Report back estimated size of downloads.

This commit is contained in:
Andrey Smirnov
2014-01-14 14:17:41 +04:00
parent 7afcc93507
commit 8ece5368b1
3 changed files with 30 additions and 9 deletions

13
debian/package.go vendored
View File

@@ -269,10 +269,17 @@ func (p *Package) PoolDirectory() (string, error) {
return filepath.Join(subdir, source), nil
}
// PackageDownloadTask is a element of download queue for the package
type PackageDownloadTask struct {
RepoURI string
DestinationPath string
Size int64
}
// DownloadList returns list of missing package files for download in format
// [[srcpath, dstpath]]
func (p *Package) DownloadList(packageRepo *Repository) (result [][]string, err error) {
result = make([][]string, 0, 1)
func (p *Package) DownloadList(packageRepo *Repository) (result []PackageDownloadTask, err error) {
result = make([]PackageDownloadTask, 0, 1)
for _, f := range p.Files {
poolPath, err := packageRepo.PoolPath(f.Filename, f.Checksums.MD5)
@@ -286,7 +293,7 @@ func (p *Package) DownloadList(packageRepo *Repository) (result [][]string, err
}
if !verified {
result = append(result, []string{f.Filename, poolPath})
result = append(result, PackageDownloadTask{RepoURI: f.Filename, DestinationPath: poolPath, Size: f.Checksums.Size})
}
}

View File

@@ -189,7 +189,11 @@ func (s *PackageSuite) TestDownloadList(c *C) {
list, err := p.DownloadList(packageRepo)
c.Check(err, IsNil)
c.Check(list, DeepEquals, [][]string{[]string{"pool/contrib/a/alien-arena/alien-arena-common_7.40-2_i386.deb", poolPath}})
c.Check(list, DeepEquals, []PackageDownloadTask{
PackageDownloadTask{
RepoURI: "pool/contrib/a/alien-arena/alien-arena-common_7.40-2_i386.deb",
DestinationPath: poolPath,
Size: 5}})
err = os.MkdirAll(filepath.Dir(poolPath), 0755)
c.Assert(err, IsNil)
@@ -201,7 +205,7 @@ func (s *PackageSuite) TestDownloadList(c *C) {
list, err = p.DownloadList(packageRepo)
c.Check(err, IsNil)
c.Check(list, DeepEquals, [][]string{})
c.Check(list, DeepEquals, []PackageDownloadTask{})
}
type PackageCollectionSuite struct {

18
debian/remote.go vendored
View File

@@ -148,6 +148,8 @@ func (repo *RemoteRepo) Fetch(d utils.Downloader) error {
func (repo *RemoteRepo) Download(d utils.Downloader, packageCollection *PackageCollection, packageRepo *Repository) error {
list := NewPackageList()
fmt.Printf("Downloading & parsing package files...\n")
// Download and parse all Release files
for _, component := range repo.Components {
for _, architecture := range repo.Architectures {
@@ -175,6 +177,8 @@ func (repo *RemoteRepo) Download(d utils.Downloader, packageCollection *PackageC
}
}
fmt.Printf("Saving packages to database...\n")
// Save package meta information to DB
err := list.ForEach(func(p *Package) error {
return packageCollection.Update(p)
@@ -184,20 +188,24 @@ func (repo *RemoteRepo) Download(d utils.Downloader, packageCollection *PackageC
return fmt.Errorf("unable to save packages to db: %s", err)
}
fmt.Printf("Building download queue...\n")
// Download all package files
ch := make(chan error, list.Len())
queued := make(map[string]bool, 1024)
count := 0
downloadSize := int64(0)
err = list.ForEach(func(p *Package) error {
list, err := p.DownloadList(packageRepo)
for _, pair := range list {
key := pair[0] + "-" + pair[1]
for _, task := range list {
key := task.RepoURI + "-" + task.DestinationPath
_, found := queued[key]
if !found {
d.Download(repo.PackageURL(pair[0]).String(), pair[1], ch)
d.Download(repo.PackageURL(task.RepoURI).String(), task.DestinationPath, ch)
count++
downloadSize += task.Size
} else {
queued[key] = true
}
@@ -210,6 +218,8 @@ func (repo *RemoteRepo) Download(d utils.Downloader, packageCollection *PackageC
return fmt.Errorf("unable to download packages: %s", err)
}
fmt.Printf("Download queue: %d items, %.2f GiB size\n", count, float64(downloadSize)/(1024.0*1024.0*1024.0))
errors := make([]string, 0)
// Wait for all downloads to finish
@@ -222,7 +232,7 @@ func (repo *RemoteRepo) Download(d utils.Downloader, packageCollection *PackageC
}
if len(errors) > 0 {
return fmt.Errorf("download errors: %s", strings.Join(errors, ", "))
return fmt.Errorf("download errors:\n %s\n", strings.Join(errors, "\n "))
}
repo.LastDownloadDate = time.Now()