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

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