[PATCH 14/21] lookaside: Be more flexible when building the download URL

Mathieu Bridon bochecha at fedoraproject.org
Wed May 6 11:53:10 UTC 2015


From: Mathieu Bridon <bochecha at daitauha.fr>

We're never going to know in advance everything our downstreams are
going to need as part of their download URL.

However, we already know that one of them, CentOS, needs the git branch
in there.

We could add the git branch as an argument to the download function, and
use it for the string interpolation.

However, someone would soon come and need something else.

This commit provides a much more generic way, which could work for all
downstreams. All they'd have to do is override pyrpkg.Commands.sources
to pass their own keyword arguments to
pyrpkg.lookaside.CGILookasideCache.download.

We could even add them to pyrpkg if they make sense to have them shared.

And in fact, since we already know that CentOS wants the git branch,
this commit also adds it, which should make them much happier, as that
should make it much easier for them to reuse more code from pyrpkg.
---
 src/pyrpkg/__init__.py  |  2 +-
 src/pyrpkg/lookaside.py |  5 ++++-
 test/test_lookaside.py  | 42 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 47 insertions(+), 2 deletions(-)

diff --git a/src/pyrpkg/__init__.py b/src/pyrpkg/__init__.py
index ce262e6..c98e3e6 100644
--- a/src/pyrpkg/__init__.py
+++ b/src/pyrpkg/__init__.py
@@ -1581,7 +1581,7 @@ class Commands(object):
             outfile = os.path.join(outdir, entry.file)
             self.lookasidecache.download(
                 self.module_name, entry.file, entry.hash, outfile,
-                hashtype=entry.hashtype)
+                hashtype=entry.hashtype, branch=self.branch_merge)
 
     def switch_branch(self, branch, fetch=True):
         """Switch the working branch
diff --git a/src/pyrpkg/lookaside.py b/src/pyrpkg/lookaside.py
index 14d0640..a1c17cb 100644
--- a/src/pyrpkg/lookaside.py
+++ b/src/pyrpkg/lookaside.py
@@ -105,7 +105,7 @@ class CGILookasideCache(object):
         sum = self.hash_file(filename, hashtype)
         return sum == hash
 
-    def download(self, name, filename, hash, outfile, hashtype=None):
+    def download(self, name, filename, hash, outfile, hashtype=None, **kwargs):
         """Download a source file
 
         Args:
@@ -115,6 +115,8 @@ class CGILookasideCache(object):
             outfile (str): The full path where to save the downloaded file.
             hashtype (str, optional): The hash algorithm. (e.g 'md5')
                 This defaults to the hashtype passed to the constructor.
+            **kwargs: Additional keyword arguments. They will be used when
+                contructing the full URL to the file to download.
         """
         if hashtype is None:
             hashtype = self.hashtype
@@ -128,6 +130,7 @@ class CGILookasideCache(object):
 
         path_dict = {'name': name, 'filename': urled_file, 'hash': hash,
                      'hashtype': hashtype}
+        path_dict.update(kwargs)
         path = self.download_path % path_dict
         url = '%s/%s' % (self.download_url, path)
         self.log.debug("Full url: %s" % url)
diff --git a/test/test_lookaside.py b/test/test_lookaside.py
index 7fe1743..4d987c1 100644
--- a/test/test_lookaside.py
+++ b/test/test_lookaside.py
@@ -116,6 +116,48 @@ class CGILookasideCacheTestCase(unittest.TestCase):
         self.assertEqual(curl.perform.call_count, 2)
 
     @mock.patch('pyrpkg.lookaside.pycurl.Curl')
+    def test_download_kwargs(self, mock_curl):
+        def mock_getinfo(info):
+            return 200 if info == pycurl.RESPONSE_CODE else 0
+
+        def mock_perform():
+            with open(self.filename, 'rb') as f:
+                curlopts[pycurl.WRITEDATA].write(f.read())
+
+        def mock_setopt(opt, value):
+            curlopts[opt] = value
+
+        curlopts = {}
+        curl = mock_curl.return_value
+        curl.getinfo.side_effect = mock_getinfo
+        curl.perform.side_effect = mock_perform
+        curl.setopt.side_effect = mock_setopt
+
+        with open(self.filename, 'wb') as f:
+            f.write(b'content')
+
+        name = 'pyrpkg'
+        filename = 'pyrpkg-0.0.tar.xz'
+        branch = 'f22'
+        hash = hashlib.sha512(b'content').hexdigest()
+        outfile = os.path.join(self.workdir, 'pyrpkg-0.0.tar.xz')
+
+        path = '%(name)s/%(filename)s/%(branch)s/%(hashtype)s/%(hash)s'
+        full_url = 'http://example.com/%s' % (
+            path % {'name': name, 'filename': filename, 'branch': branch,
+                    'hashtype': 'sha512', 'hash': hash})
+
+        lc = CGILookasideCache('sha512', 'http://example.com', '_')
+
+        # Modify the download path, to try arbitrary kwargs
+        lc.download_path = path
+
+        lc.download(name, filename, hash, outfile, hashtype='sha512',
+                    branch=branch)
+        self.assertEqual(curl.perform.call_count, 1)
+        self.assertEqual(curlopts[pycurl.URL], full_url)
+
+    @mock.patch('pyrpkg.lookaside.pycurl.Curl')
     def test_download_corrupted(self, mock_curl):
         def mock_getinfo(info):
             return 200 if info == pycurl.RESPONSE_CODE else 0
-- 
2.1.0



More information about the rel-eng mailing list