From: Michael Tremer Date: Fri, 16 Feb 2018 15:46:13 +0000 (+0100) Subject: Add TransactionDownloader X-Git-Tag: 0.9.28~1285^2~1115 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5460b95e175a16bc92165d9e79fbbcbb38fa61c6;p=pakfire.git Add TransactionDownloader This class downloads all required packages in a transaction. Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/cli.py b/src/pakfire/cli.py index 388774ec2..bb76ec032 100644 --- a/src/pakfire/cli.py +++ b/src/pakfire/cli.py @@ -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() diff --git a/src/pakfire/downloaders.py b/src/pakfire/downloaders.py index 086bda7a2..9b92d0ea9 100644 --- a/src/pakfire/downloaders.py +++ b/src/pakfire/downloaders.py @@ -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)