Blame SOURCES/BZ-1342179-add-retry-no-cache-opt.patch

babe2e
diff -up urlgrabber-3.10/test/test_mirror.py.orig urlgrabber-3.10/test/test_mirror.py
babe2e
--- urlgrabber-3.10/test/test_mirror.py.orig	2013-08-26 09:09:07.000000000 +0200
babe2e
+++ urlgrabber-3.10/test/test_mirror.py	2016-06-29 18:26:06.790393129 +0200
babe2e
@@ -268,33 +268,55 @@ class ActionTests(TestCase):
babe2e
         self.assertEquals(self.g.calls, expected_calls)
babe2e
         self.assertEquals(urlgrabber.mirror.DEBUG.logs, expected_logs)
babe2e
                 
babe2e
+import thread, socket
babe2e
+LOCALPORT = 'localhost', 2000
babe2e
 
babe2e
 class HttpReplyCode(TestCase):
babe2e
     def setUp(self):
babe2e
+        # start the server
babe2e
+        self.exit = False
babe2e
         def server():
babe2e
-            import socket
babe2e
             s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
babe2e
             s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
babe2e
-            s.bind(('localhost', 2000)); s.listen(1)
babe2e
+            s.bind(LOCALPORT); s.listen(1)
babe2e
             while 1:
babe2e
                 c, a = s.accept()
babe2e
+                if self.exit: c.close(); break
babe2e
                 while not c.recv(4096).endswith('\r\n\r\n'): pass
babe2e
                 c.sendall('HTTP/1.1 %d %s\r\n' % self.reply)
babe2e
+                if self.content is not None:
babe2e
+                    c.sendall('Content-Length: %d\r\n\r\n' % len(self.content))
babe2e
+                    c.sendall(self.content)
babe2e
                 c.close()
babe2e
-        import thread
babe2e
-        self.reply = 503, "Busy"
babe2e
+            s.close()
babe2e
+            self.exit = False
babe2e
         thread.start_new_thread(server, ())
babe2e
 
babe2e
+        # create grabber and mirror group objects
babe2e
         def failure(obj):
babe2e
             self.code = getattr(obj.exception, 'code', None)
babe2e
             return {}
babe2e
         self.g  = URLGrabber()
babe2e
-        self.mg = MirrorGroup(self.g, ['http://localhost:2000/'], failure_callback = failure)
babe2e
+        self.mg = MirrorGroup(self.g, ['http://%s:%d' % LOCALPORT],
babe2e
+                              failure_callback = failure)
babe2e
+
babe2e
+    def tearDown(self):
babe2e
+        # shut down the server
babe2e
+        self.exit = True
babe2e
+        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
babe2e
+        s.connect(LOCALPORT); s.close() # wake it up
babe2e
+        while self.exit: pass # poor man's join
babe2e
 
babe2e
     def test_grab(self):
babe2e
+        'tests the propagation of HTTP reply code'
babe2e
+        self.reply = 503, "Busy"
babe2e
+        self.content = None
babe2e
+
babe2e
+        # single
babe2e
         self.assertRaises(URLGrabError, self.mg.urlgrab, 'foo')
babe2e
         self.assertEquals(self.code, 503); del self.code
babe2e
 
babe2e
+        # multi
babe2e
         err = []
babe2e
         self.mg.urlgrab('foo', async = True, failfunc = err.append)
babe2e
         urlgrabber.grabber.parallel_wait()
babe2e
diff -up urlgrabber-3.10/test/test_mirror.py.orig urlgrabber-3.10/test/test_mirror.py
babe2e
--- urlgrabber-3.10/test/test_mirror.py.orig	2016-06-29 18:26:06.790393129 +0200
babe2e
+++ urlgrabber-3.10/test/test_mirror.py	2016-06-29 18:26:58.886148544 +0200
babe2e
@@ -268,13 +268,14 @@ class ActionTests(TestCase):
babe2e
         self.assertEquals(self.g.calls, expected_calls)
babe2e
         self.assertEquals(urlgrabber.mirror.DEBUG.logs, expected_logs)
babe2e
                 
babe2e
-import thread, socket
babe2e
+import threading, socket
babe2e
 LOCALPORT = 'localhost', 2000
babe2e
 
babe2e
 class HttpReplyCode(TestCase):
babe2e
     def setUp(self):
babe2e
         # start the server
babe2e
         self.exit = False
babe2e
+        self.process = lambda data: None
babe2e
         def server():
babe2e
             s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
babe2e
             s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
babe2e
@@ -282,7 +283,10 @@ class HttpReplyCode(TestCase):
babe2e
             while 1:
babe2e
                 c, a = s.accept()
babe2e
                 if self.exit: c.close(); break
babe2e
-                while not c.recv(4096).endswith('\r\n\r\n'): pass
babe2e
+                data = ''
babe2e
+                while not data.endswith('\r\n\r\n'):
babe2e
+                    data = c.recv(4096)
babe2e
+                self.process(data)
babe2e
                 c.sendall('HTTP/1.1 %d %s\r\n' % self.reply)
babe2e
                 if self.content is not None:
babe2e
                     c.sendall('Content-Length: %d\r\n\r\n' % len(self.content))
babe2e
@@ -290,7 +294,8 @@ class HttpReplyCode(TestCase):
babe2e
                 c.close()
babe2e
             s.close()
babe2e
             self.exit = False
babe2e
-        thread.start_new_thread(server, ())
babe2e
+        self.thread = threading.Thread(target=server)
babe2e
+        self.thread.start()
babe2e
 
babe2e
         # create grabber and mirror group objects
babe2e
         def failure(obj):
babe2e
@@ -305,7 +310,7 @@ class HttpReplyCode(TestCase):
babe2e
         self.exit = True
babe2e
         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
babe2e
         s.connect(LOCALPORT); s.close() # wake it up
babe2e
-        while self.exit: pass # poor man's join
babe2e
+        self.thread.join()
babe2e
 
babe2e
     def test_grab(self):
babe2e
         'tests the propagation of HTTP reply code'
babe2e
@@ -323,6 +328,45 @@ class HttpReplyCode(TestCase):
babe2e
         self.assertEquals([e.exception.errno for e in err], [256])
babe2e
         self.assertEquals(self.code, 503); del self.code
babe2e
 
babe2e
+    def test_retry_no_cache(self):
babe2e
+        'test bypassing proxy cache on failure'
babe2e
+        def process(data):
babe2e
+            if 'Pragma:no-cache' in data:
babe2e
+                self.content = 'version2'
babe2e
+            else:
babe2e
+                self.content = 'version1'
babe2e
+
babe2e
+        def checkfunc_read(obj):
babe2e
+            if obj.data == 'version1':
babe2e
+                raise URLGrabError(-1, 'Outdated version of foo')
babe2e
+
babe2e
+        def checkfunc_grab(obj):
babe2e
+            with open('foo') as f:
babe2e
+                if f.read() == 'version1':
babe2e
+                    raise URLGrabError(-1, 'Outdated version of foo')
babe2e
+
babe2e
+        self.process = process
babe2e
+        self.reply = 200, "OK"
babe2e
+
babe2e
+        opts = self.g.opts
babe2e
+        opts.retry = 3
babe2e
+        opts.retry_no_cache = True
babe2e
+
babe2e
+        # single
babe2e
+        opts.checkfunc = checkfunc_read
babe2e
+        try:
babe2e
+            self.mg.urlread('foo')
babe2e
+        except URLGrabError as e:
babe2e
+            self.fail(str(e))
babe2e
+
babe2e
+        # multi
babe2e
+        opts.checkfunc = checkfunc_grab
babe2e
+        self.mg.urlgrab('foo', async=True)
babe2e
+        try:
babe2e
+            urlgrabber.grabber.parallel_wait()
babe2e
+        except URLGrabError as e:
babe2e
+            self.fail(str(e))
babe2e
+
babe2e
 def suite():
babe2e
     tl = TestLoader()
babe2e
     return tl.loadTestsFromModule(sys.modules[__name__])
babe2e
diff -up urlgrabber-3.10/urlgrabber/grabber.py.orig urlgrabber-3.10/urlgrabber/grabber.py
babe2e
--- urlgrabber-3.10/urlgrabber/grabber.py.orig	2016-06-29 18:25:53.964453346 +0200
babe2e
+++ urlgrabber-3.10/urlgrabber/grabber.py	2016-06-29 18:26:58.886148544 +0200
babe2e
@@ -171,6 +171,12 @@ GENERAL ARGUMENTS (kwargs)
babe2e
     The libproxy code is only used if the proxies dictionary
babe2e
     does not provide any proxies.
babe2e
 
babe2e
+  no_cache = False
babe2e
+
babe2e
+    When True, server-side cache will be disabled for http and https
babe2e
+    requests.  This is equivalent to setting
babe2e
+      http_headers = (('Pragma', 'no-cache'),)
babe2e
+
babe2e
   prefix = None
babe2e
 
babe2e
     a url prefix that will be prepended to all requested urls.  For
babe2e
@@ -383,10 +389,11 @@ RETRY RELATED ARGUMENTS
babe2e
     identical to checkfunc, except for the attributes defined in the
babe2e
     CallbackObject instance.  The attributes for failure_callback are:
babe2e
 
babe2e
-      exception = the raised exception
babe2e
-      url       = the url we're trying to fetch
babe2e
-      tries     = the number of tries so far (including this one)
babe2e
-      retry     = the value of the retry option
babe2e
+      exception      = the raised exception
babe2e
+      url            = the url we're trying to fetch
babe2e
+      tries          = the number of tries so far (including this one)
babe2e
+      retry          = the value of the retry option
babe2e
+      retry_no_cache = the value of the retry_no_cache option
babe2e
 
babe2e
     The callback is present primarily to inform the calling program of
babe2e
     the failure, but if it raises an exception (including the one it's
babe2e
@@ -431,6 +438,19 @@ RETRY RELATED ARGUMENTS
babe2e
     passed the same arguments, so you could use the same function for
babe2e
     both.
babe2e
       
babe2e
+  retry_no_cache = False
babe2e
+
babe2e
+    When True, automatically enable no_cache for future retries if
babe2e
+    checkfunc performs an unsuccessful check.
babe2e
+
babe2e
+    This option is useful if your application expects a set of files
babe2e
+    from the same server to form an atomic unit and you write your
babe2e
+    checkfunc to ensure each file being downloaded belongs to such a
babe2e
+    unit.  If transparent proxy caching is in effect, the files can
babe2e
+    become out-of-sync, disrupting the atomicity.  Enabling this option
babe2e
+    will prevent that, while ensuring that you still enjoy the benefits
babe2e
+    of caching when possible.
babe2e
+
babe2e
 BANDWIDTH THROTTLING
babe2e
 
babe2e
   urlgrabber supports throttling via two values: throttle and
babe2e
@@ -1001,6 +1021,8 @@ class URLGrabberOptions:
babe2e
         self.half_life = 30*24*60*60 # 30 days
babe2e
         self.default_speed = 1e6 # 1 MBit
babe2e
         self.ftp_disable_epsv = False
babe2e
+        self.no_cache = False
babe2e
+        self.retry_no_cache = False
babe2e
         
babe2e
     def __repr__(self):
babe2e
         return self.format()
babe2e
@@ -1077,7 +1099,8 @@ class URLGrabber(object):
babe2e
             if callback:
babe2e
                 if DEBUG: DEBUG.info('calling callback: %s', callback)
babe2e
                 obj = CallbackObject(exception=exception, url=args[0],
babe2e
-                                     tries=tries, retry=opts.retry)
babe2e
+                                     tries=tries, retry=opts.retry,
babe2e
+                                     retry_no_cache=opts.retry_no_cache)
babe2e
                 _run_callback(callback, obj)
babe2e
 
babe2e
             if (opts.retry is None) or (tries == opts.retry):
babe2e
@@ -1089,6 +1112,8 @@ class URLGrabber(object):
babe2e
                 if DEBUG: DEBUG.info('retrycode (%i) not in list %s, re-raising',
babe2e
                                      retrycode, opts.retrycodes)
babe2e
                 raise
babe2e
+            if retrycode is not None and retrycode < 0 and opts.retry_no_cache:
babe2e
+                opts.no_cache = True
babe2e
     
babe2e
     def urlopen(self, url, opts=None, **kwargs):
babe2e
         """open the url and return a file object
babe2e
@@ -1429,11 +1454,15 @@ class PyCurlFileObject(object):
babe2e
                 self.curl_obj.setopt(pycurl.SSLKEYPASSWD, opts.ssl_key_pass)
babe2e
 
babe2e
         #headers:
babe2e
-        if opts.http_headers and self.scheme in ('http', 'https'):
babe2e
+        if self.scheme in ('http', 'https'):
babe2e
             headers = []
babe2e
-            for (tag, content) in opts.http_headers:
babe2e
-                headers.append('%s:%s' % (tag, content))
babe2e
-            self.curl_obj.setopt(pycurl.HTTPHEADER, headers)
babe2e
+            if opts.http_headers is not None:
babe2e
+                for (tag, content) in opts.http_headers:
babe2e
+                    headers.append('%s:%s' % (tag, content))
babe2e
+            if opts.no_cache:
babe2e
+                headers.append('Pragma:no-cache')
babe2e
+            if headers:
babe2e
+                self.curl_obj.setopt(pycurl.HTTPHEADER, headers)
babe2e
 
babe2e
         # ranges:
babe2e
         if opts.range or opts.reget:
babe2e
@@ -2055,7 +2084,8 @@ class _ExternalDownloader:
babe2e
         'ssl_key_pass',
babe2e
         'ssl_verify_peer', 'ssl_verify_host',
babe2e
         'size', 'max_header_size', 'ip_resolve',
babe2e
-        'ftp_disable_epsv'
babe2e
+        'ftp_disable_epsv',
babe2e
+        'no_cache',
babe2e
     )
babe2e
 
babe2e
     def start(self, opts):
babe2e
@@ -2236,6 +2266,8 @@ def parallel_wait(meter=None):
babe2e
                 except URLGrabError, ug_err:
babe2e
                     retry = 0 # no retries
babe2e
             if opts.tries < retry and ug_err.errno in opts.retrycodes:
babe2e
+                if ug_err.errno < 0 and opts.retry_no_cache:
babe2e
+                    opts.no_cache = True
babe2e
                 start(opts, opts.tries + 1) # simple retry
babe2e
                 continue
babe2e