[Yum-devel] [PATCH 2/3] Add argument 'errors' to _getFile and getPackage.

Zdeněk Pavlas zpavlas at redhat.com
Fri Oct 21 14:30:39 UTC 2011


Don't expect exception, handle errors in 'failfunc' callback.
If argument 'errors' is provided, call it with the error message.
---
 yum/yumRepo.py |   59 +++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 39 insertions(+), 20 deletions(-)

diff --git a/yum/yumRepo.py b/yum/yumRepo.py
index 1181fae..47d58df 100644
--- a/yum/yumRepo.py
+++ b/yum/yumRepo.py
@@ -760,7 +760,7 @@ class YumRepository(Repository, config.RepoConf):
 
     def _getFile(self, url=None, relative=None, local=None, start=None, end=None,
             copy_local=None, checkfunc=None, text=None, reget='simple', 
-            cache=True, size=None):
+            cache=True, size=None, errors=None):
         """retrieve file from the mirrorgroup for the repo
            relative to local, optionally get range from
            start to end, also optionally retrieve from a specific baseurl"""
@@ -809,11 +809,18 @@ class YumRepository(Repository, config.RepoConf):
             dirstat = os.statvfs(os.path.dirname(local))
             avail = dirstat.f_bavail * dirstat.f_bsize
             if avail < long(size):
-                raise Errors.RepoError, _('''\
+                errstr = _('''\
 Insufficient space in download directory %s
     * free   %s
     * needed %s'''
                 ) % (os.path.dirname(local), format_number(avail), format_number(long(size)))
+                if errors: return errors(errstr)
+                raise Errors.RepoError(errstr)
+
+        # To save the myriad of arguments to _getFile, we expect that 'errors' implies
+        # parallel downloading.  MG updates arguments before we get to urlgrab(), so just
+        # use some defaults.
+        async = errors and (None, 3) or False
 
         if url and scheme != "media":
             ugopts = self._default_grabopts(cache=cache)
@@ -828,45 +835,56 @@ Insufficient space in download directory %s
 
             remote = url + '/' + relative
 
-            try:
-                result = ug.urlgrab(misc.to_utf8(remote), local,
-                                    text=misc.to_utf8(text),
-                                    range=(start, end),
-                                    )
-            except URLGrabError, e:
+            def failfunc(opts):
+                e = opts.exception
                 errstr = "failed to retrieve %s from %s\nerror was %s" % (relative, self.id, e)
                 if self.mirrorurls:
                     errstr +="\n  You could try running: yum clean expire-cache"
                     errstr +="\n  To get a new set of mirrors."
+                if errors:
+                    return errors(errstr)
                 if e.errno == 256:
                     raise Errors.NoMoreMirrorsRepoError, errstr
                 else:
                     raise Errors.RepoError, errstr
 
+            result = ug.urlgrab(
+                misc.to_utf8(remote), local,
+                text=misc.to_utf8(text),
+                range=(start, end),
+                failfunc=failfunc,
+                async=async,
+            )
+
 
         else:
             headers = tuple(self.__headersListFromDict(cache=cache))
-            try:
-                result = self.grab.urlgrab(misc.to_utf8(relative), local,
-                                           text = misc.to_utf8(text),
-                                           range = (start, end),
-                                           copy_local=copy_local,
-                                           reget = reget,
-                                           checkfunc=checkfunc,
-                                           http_headers=headers,
-                                           size=size
-                                           )
-            except URLGrabError, e:
+
+            def failfunc(opts):
+                e = opts.exception
                 errstr = "failure: %s from %s: %s" % (relative, self.id, e)
+                if errors:
+                    return errors(errstr)
                 if e.errno == 256:
                     raise Errors.NoMoreMirrorsRepoError, errstr
                 else:
                     raise Errors.RepoError, errstr
 
+            result = self.grab.urlgrab(
+                misc.to_utf8(relative), local,
+                text = misc.to_utf8(text), range = (start, end),
+                copy_local=copy_local,
+                reget = reget,
+                checkfunc=checkfunc,
+                failfunc=failfunc,
+                async=async,
+                http_headers=headers,
+                size=size,
+            )
         return result
     __get = _getFile
 
-    def getPackage(self, package, checkfunc=None, text=None, cache=True):
+    def getPackage(self, package, checkfunc=None, text=None, cache=True, errors=None):
         remote = package.relativepath
         local = package.localPkg()
         basepath = package.basepath
@@ -883,6 +901,7 @@ Insufficient space in download directory %s
                         text=text,
                         cache=cache,
                         size=package.size,
+                        errors=errors,
                         )
 
     def getHeader(self, package, checkfunc = None, reget = 'simple',
-- 
1.7.4.4



More information about the Yum-devel mailing list