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

Zdeněk Pavlas zpavlas at redhat.com
Wed Dec 21 16:01:28 UTC 2011


Call this function with the error message instead of raising
RepoError exception.  This should work even with urlgrabber
that does not support 'failfunc' callbacks.
---
 yum/misc.py    |    5 ++++
 yum/yumRepo.py |   64 ++++++++++++++++++++++++++++++++++++++-----------------
 2 files changed, 49 insertions(+), 20 deletions(-)

diff --git a/yum/misc.py b/yum/misc.py
index 5321003..8644978 100644
--- a/yum/misc.py
+++ b/yum/misc.py
@@ -150,6 +150,11 @@ def re_remote_url(s):
         return True
     return False
 
+class Dummy:
+    """ A dict-as-instance wrapper """
+    def __init__(self, **kw): self.__dict__ = kw
+    def __repr__(self): return '<0x%x: %s>' % (id(self), self.__dict__)
+
 ###########
 # Title: Remove duplicates from a sequence
 # Submitter: Tim Peters 
diff --git a/yum/yumRepo.py b/yum/yumRepo.py
index d886187..11ab66b 100644
--- a/yum/yumRepo.py
+++ b/yum/yumRepo.py
@@ -767,7 +767,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, errorfunc=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"""
@@ -816,11 +816,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 errorfunc:
+                    return errorfunc(errstr)
+                raise Errors.RepoError(errstr)
+
+        # We don't want to add 14th argument 'async' to _getFile().
+        # just assume that 'errorfunc' implies parallel downloading.
+        async = errorfunc and (url and urlparse.urlsplit(url).netloc, 3)
 
         if url and scheme != "media":
             ugopts = self._default_grabopts(cache=cache)
@@ -836,24 +843,42 @@ Insufficient space in download directory %s
             remote = url + '/' + relative
 
             try:
+                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 errorfunc:
+                        return errorfunc(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,
                                     )
             except URLGrabError, e:
-                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 e.errno == 256:
-                    raise Errors.NoMoreMirrorsRepoError, errstr
-                else:
-                    raise Errors.RepoError, errstr
-
-
+                opts = misc.Dummy(exception=e)
+                return failfunc(opts)
         else:
             headers = tuple(self.__headersListFromDict(cache=cache))
+
             try:
+                def failfunc(opts):
+                    e = opts.exception
+                    errstr = "failure: %s from %s: %s" % (relative, self.id, e)
+                    if errorfunc:
+                        return errorfunc(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),
@@ -861,19 +886,17 @@ Insufficient space in download directory %s
                                            reget = reget,
                                            checkfunc=checkfunc,
                                            http_headers=headers,
-                                           size=size
+                                           size=size,
+                                           failfunc=failfunc,
+                                           async=async,
                                            )
             except URLGrabError, e:
-                errstr = "failure: %s from %s: %s" % (relative, self.id, e)
-                if e.errno == 256:
-                    raise Errors.NoMoreMirrorsRepoError, errstr
-                else:
-                    raise Errors.RepoError, errstr
-
+                opts = misc.Dummy(exception=e)
+                return failfunc(opts)
         return result
     __get = _getFile
 
-    def getPackage(self, package, checkfunc=None, text=None, cache=True):
+    def getPackage(self, package, checkfunc=None, text=None, cache=True, errorfunc=None):
         remote = package.relativepath
         local = package.localPkg()
         basepath = package.basepath
@@ -890,6 +913,7 @@ Insufficient space in download directory %s
                         text=text,
                         cache=cache,
                         size=package.size,
+                        errorfunc=errorfunc,
                         )
 
     def getHeader(self, package, checkfunc = None, reget = 'simple',
-- 
1.7.4.4



More information about the Yum-devel mailing list