Blob Blame History Raw
From 1a0601d29794cec1f735a10208364d11958c41ec Mon Sep 17 00:00:00 2001
From: Ondrej Nosek <onosek@redhat.com>
Date: Wed, 26 Jul 2023 01:30:12 +0200
Subject: [PATCH 2/4] Prepare the lookaside cache code for retries

These changes should not have an impact on the original functionality.

JIRA: RHELCMP-11210

Signed-off-by: Ondrej Nosek <onosek@redhat.com>
---
 pyrpkg/lookaside.py | 96 ++++++++++++++++++++++-----------------------
 1 file changed, 48 insertions(+), 48 deletions(-)

diff --git a/pyrpkg/lookaside.py b/pyrpkg/lookaside.py
index 3efcd88..f94ffdb 100644
--- a/pyrpkg/lookaside.py
+++ b/pyrpkg/lookaside.py
@@ -163,17 +163,17 @@ class CGILookasideCache(object):
             url = url.encode('utf-8')
         self.log.debug("Full url: %s", url)
 
+        c = pycurl.Curl()
+        c.setopt(pycurl.URL, url)
+        c.setopt(pycurl.HTTPHEADER, ['Pragma:'])
+        c.setopt(pycurl.NOPROGRESS, False)
+        c.setopt(pycurl.PROGRESSFUNCTION, self.print_progress)
+        c.setopt(pycurl.OPT_FILETIME, True)
+        c.setopt(pycurl.LOW_SPEED_LIMIT, 1000)
+        c.setopt(pycurl.LOW_SPEED_TIME, 300)
+        c.setopt(pycurl.FOLLOWLOCATION, 1)
         with open(outfile, 'wb') as f:
-            c = pycurl.Curl()
-            c.setopt(pycurl.URL, url)
-            c.setopt(pycurl.HTTPHEADER, ['Pragma:'])
-            c.setopt(pycurl.NOPROGRESS, False)
-            c.setopt(pycurl.PROGRESSFUNCTION, self.print_progress)
-            c.setopt(pycurl.OPT_FILETIME, True)
             c.setopt(pycurl.WRITEDATA, f)
-            c.setopt(pycurl.LOW_SPEED_LIMIT, 1000)
-            c.setopt(pycurl.LOW_SPEED_TIME, 300)
-            c.setopt(pycurl.FOLLOWLOCATION, 1)
             try:
                 c.perform()
                 tstamp = c.getinfo(pycurl.INFO_FILETIME)
@@ -254,29 +254,29 @@ class CGILookasideCache(object):
                      ('%ssum' % self.hashtype, hash),
                      ('filename', filename)]
 
-        with io.BytesIO() as buf:
-            c = pycurl.Curl()
-            c.setopt(pycurl.URL, self.upload_url)
-            c.setopt(pycurl.WRITEFUNCTION, buf.write)
-            c.setopt(pycurl.HTTPPOST, post_data)
-            c.setopt(pycurl.FOLLOWLOCATION, 1)
+        c = pycurl.Curl()
+        c.setopt(pycurl.URL, self.upload_url)
+        c.setopt(pycurl.HTTPPOST, post_data)
+        c.setopt(pycurl.FOLLOWLOCATION, 1)
 
-            if self.client_cert is not None:
-                if os.path.exists(self.client_cert):
-                    c.setopt(pycurl.SSLCERT, self.client_cert)
-                else:
-                    self.log.warning("Missing certificate: %s"
-                                     % self.client_cert)
+        if self.client_cert is not None:
+            if os.path.exists(self.client_cert):
+                c.setopt(pycurl.SSLCERT, self.client_cert)
+            else:
+                self.log.warning("Missing certificate: %s"
+                                 % self.client_cert)
 
-            if self.ca_cert is not None:
-                if os.path.exists(self.ca_cert):
-                    c.setopt(pycurl.CAINFO, self.ca_cert)
-                else:
-                    self.log.warning("Missing certificate: %s", self.ca_cert)
+        if self.ca_cert is not None:
+            if os.path.exists(self.ca_cert):
+                c.setopt(pycurl.CAINFO, self.ca_cert)
+            else:
+                self.log.warning("Missing certificate: %s", self.ca_cert)
 
-            c.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_GSSNEGOTIATE)
-            c.setopt(pycurl.USERPWD, ':')
+        c.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_GSSNEGOTIATE)
+        c.setopt(pycurl.USERPWD, ':')
 
+        with io.BytesIO() as buf:
+            c.setopt(pycurl.WRITEFUNCTION, buf.write)
             try:
                 c.perform()
                 status = c.getinfo(pycurl.RESPONSE_CODE)
@@ -341,30 +341,30 @@ class CGILookasideCache(object):
             ('mtime', str(int(os.stat(filepath).st_mtime))),
         ]
 
-        with io.BytesIO() as buf:
-            c = pycurl.Curl()
-            c.setopt(pycurl.URL, self.upload_url)
-            c.setopt(pycurl.NOPROGRESS, False)
-            c.setopt(pycurl.PROGRESSFUNCTION, self.print_progress)
-            c.setopt(pycurl.WRITEFUNCTION, buf.write)
-            c.setopt(pycurl.HTTPPOST, post_data)
-            c.setopt(pycurl.FOLLOWLOCATION, 1)
+        c = pycurl.Curl()
+        c.setopt(pycurl.URL, self.upload_url)
+        c.setopt(pycurl.NOPROGRESS, False)
+        c.setopt(pycurl.PROGRESSFUNCTION, self.print_progress)
+        c.setopt(pycurl.HTTPPOST, post_data)
+        c.setopt(pycurl.FOLLOWLOCATION, 1)
 
-            if self.client_cert is not None:
-                if os.path.exists(self.client_cert):
-                    c.setopt(pycurl.SSLCERT, self.client_cert)
-                else:
-                    self.log.warning("Missing certificate: %s", self.client_cert)
+        if self.client_cert is not None:
+            if os.path.exists(self.client_cert):
+                c.setopt(pycurl.SSLCERT, self.client_cert)
+            else:
+                self.log.warning("Missing certificate: %s", self.client_cert)
 
-            if self.ca_cert is not None:
-                if os.path.exists(self.ca_cert):
-                    c.setopt(pycurl.CAINFO, self.ca_cert)
-                else:
-                    self.log.warning("Missing certificate: %s", self.ca_cert)
+        if self.ca_cert is not None:
+            if os.path.exists(self.ca_cert):
+                c.setopt(pycurl.CAINFO, self.ca_cert)
+            else:
+                self.log.warning("Missing certificate: %s", self.ca_cert)
 
-            c.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_GSSNEGOTIATE)
-            c.setopt(pycurl.USERPWD, ':')
+        c.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_GSSNEGOTIATE)
+        c.setopt(pycurl.USERPWD, ':')
 
+        with io.BytesIO() as buf:
+            c.setopt(pycurl.WRITEFUNCTION, buf.write)
             try:
                 c.perform()
                 status = c.getinfo(pycurl.RESPONSE_CODE)
-- 
2.41.0