]> git.ipfire.org Git - pakfire.git/commitdiff
Add TransactionDownloader
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 16 Feb 2018 15:46:13 +0000 (16:46 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 16 Feb 2018 15:46:13 +0000 (16:46 +0100)
This class downloads all required packages in a transaction.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/cli.py
src/pakfire/downloaders.py

index 388774ec24d0e924ca5f96a6317200f38f14bd41..bb76ec0327a2710ab9cc4198ab100328e1e9c79b 100644 (file)
@@ -35,6 +35,7 @@ from . import builder
 from . import client
 from . import config
 from . import daemon
+from . import downloaders
 from . import packages
 from . import repository
 from . import ui
@@ -254,6 +255,15 @@ class Cli(object):
                for line in t.splitlines():
                        self.ui.message(line)
 
+       def _download_transaction(self, transaction):
+               """
+                       Downloads all packages required for this transaction
+               """
+               d = downloaders.TransactionDownloader(self.pakfire, transaction)
+
+               # Run the download
+               d.download()
+
        def _execute_transaction(self, transaction):
                # Don't do anything if the transaction is empty
                if len(transaction) == 0:
@@ -268,6 +278,9 @@ class Cli(object):
                        self.ui.message(_("Aborted by user"))
                        return
 
+               # Download transaction
+               self._download_transaction(transaction)
+
                # Run the transaction
                transaction.run()
 
index 086bda7a2dd9f03f2f19e304a7fabf83b48109dd..9b92d0ea9c3f77bddcb27041b1dd796d74c6c56e 100644 (file)
@@ -29,7 +29,6 @@ from .i18n import _
 log = logging.getLogger("pakfire.downloader")
 log.propagate = 1
 
-
 class Downloader(object):
        pass
 
@@ -172,3 +171,44 @@ class RepositoryDownloader(Downloader):
                # XXX compare checksum here
                downloader.retrieve("repodata/%s" % self.metadata.database, filename=path,
                        message=_("%s: package database") % self.repo.name)
+
+       def download_package(self, pkg):
+               """
+                       Downloads a package to it's cache path
+               """
+               downloader = self.make_downloader()
+
+               downloader.retrieve(pkg.filename, filename=pkg.cache_path,
+                       message="%s" % pkg, checksum_algo="sha1", checksum=pkg.checksum)
+
+
+class TransactionDownloader(Downloader):
+       def __init__(self, pakfire, transaction):
+               self.pakfire = pakfire
+
+               # The transaction that we process
+               self.transaction = transaction
+
+               # Cache repository downloaders
+               self._repo_downloaders = {}
+
+       def _get_repo_downloader(self, repo):
+               try:
+                       return self._repo_downloaders[repo]
+               except KeyError:
+                       d = RepositoryDownloader(self.pakfire, repo)
+                       self._repo_downloaders[repo] = d
+
+                       return d
+
+       def download(self):
+               for step in self.transaction:
+                       # Skip any steps that do not need a download
+                       if not step.needs_download:
+                               continue
+
+                       # Get the downloader
+                       downloader = self._get_repo_downloader(step.package.repo)
+
+                       # Download the package
+                       downloader.download_package(step.package)