[Yum-devel] [PATCH 2/3] Change the API used for downloading packages

Zdeněk Pavlas zpavlas at redhat.com
Wed Jul 27 12:23:38 UTC 2011


Drop repo.getPackage() in favor of getPackageAsync() and getPackageDone().
The new API allows for parallel downloads.
---
 yum/__init__.py |   65 ++++++++++++++++++++++++++++++------------------------
 1 files changed, 36 insertions(+), 29 deletions(-)

diff --git a/yum/__init__.py b/yum/__init__.py
index f97378c..42fa2d7 100644
--- a/yum/__init__.py
+++ b/yum/__init__.py
@@ -1983,6 +1983,24 @@ class YumBase(depsolve.Depsolve):
         i = 0
         local_size = 0
         done_repos = set()
+
+        def checkfunc(po, error):
+            # Called when download of package 'po' has finished
+            # or terminated because of an error.
+            if not error:
+                try:
+                    self.verifyPkg(po.localPkg(), po, 1)
+                    if not po.repoid in done_repos:
+                        done_repos.add(po.repoid)
+                        # Check a single package per repo
+                        result, errmsg = self.sigCheckPkg(po)
+                        if result != 0:
+                            self.verbose_logger.warn("%s", errmsg)
+                except Exception, e:
+                    error = e
+            if error:
+                adderror(po, str(error))
+
         for po in remote_pkgs:
             #  Recheck if the file is there, works around a couple of weird
             # edge cases.
@@ -1999,7 +2017,6 @@ class YumBase(depsolve.Depsolve):
                 if os.path.getsize(local) >= po.size:
                     os.unlink(local)
 
-            checkfunc = (self.verifyPkg, (po, 1), {})
             dirstat = os.statvfs(po.repo.pkgdir)
             if (dirstat.f_bavail * dirstat.f_bsize) <= long(po.size):
                 adderror(po, _('Insufficient space in download directory %s\n'
@@ -2010,35 +2027,25 @@ class YumBase(depsolve.Depsolve):
                           format_number(po.size)))
                 continue
             
-            try:
-                if i == 1 and not local_size and remote_size == po.size:
-                    text = os.path.basename(po.relativepath)
-                else:
-                    text = '(%s/%s): %s' % (i, len(remote_pkgs),
-                                            os.path.basename(po.relativepath))
-                mylocal = po.repo.getPackage(po,
-                                   checkfunc=checkfunc,
-                                   text=text,
-                                   cache=po.repo.http_caching != 'none',
-                                   )
-                local_size += po.size
-                if hasattr(urlgrabber.progress, 'text_meter_total_size'):
-                    urlgrabber.progress.text_meter_total_size(remote_size,
-                                                              local_size)
-                if po.repoid not in done_repos:
-                    #  Check a single package per. repo. ... to give a hint to
-                    # the user on big downloads.
-                    result, errmsg = self.sigCheckPkg(po)
-                    if result != 0:
-                        self.verbose_logger.warn("%s", errmsg)
-                done_repos.add(po.repoid)
-
-            except Errors.RepoError, e:
-                adderror(po, str(e))
+            if i == 1 and not local_size and remote_size == po.size:
+                text = os.path.basename(po.relativepath)
             else:
-                po.localpath = mylocal
-                if po in errors:
-                    del errors[po]
+                text = '(%s/%s): %s' % (i, len(remote_pkgs),
+                                        os.path.basename(po.relativepath))
+
+            # Start downloading the package in background
+            po.repo.getPackageAsync(po, checkfunc,
+                text=text,
+                parallel=self.conf.parallel,
+            )
+
+            local_size += po.size
+            if hasattr(urlgrabber.progress, 'text_meter_total_size'):
+                urlgrabber.progress.text_meter_total_size(remote_size,
+                                                          local_size)
+        
+        # Make sure all downloads have finished
+        yumRepo.YumRepository.getPackageDone()
 
         if hasattr(urlgrabber.progress, 'text_meter_total_size'):
             urlgrabber.progress.text_meter_total_size(0)
-- 
1.7.4.4



More information about the Yum-devel mailing list