[PATCH 10/21] lookaside: Move handling of file verification

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


From: Mathieu Bridon <bochecha at daitauha.fr>

The only time we need to verify whether a source file is valid is when
we interact with the lookaside cache, so it makes sense to have it
there, and remove some clutter from the main module.

However, the Commands._verify_file might be used by downstreams, so
let's keep it for a while, make it use the new lookaside module, and
emit a deprecation warning.
---
 src/pyrpkg/__init__.py  | 20 ++++++--------------
 src/pyrpkg/lookaside.py | 15 +++++++++++++++
 test/test_lookaside.py  | 11 +++++++++++
 3 files changed, 32 insertions(+), 14 deletions(-)

diff --git a/src/pyrpkg/__init__.py b/src/pyrpkg/__init__.py
index e369db5..eff3551 100644
--- a/src/pyrpkg/__init__.py
+++ b/src/pyrpkg/__init__.py
@@ -879,18 +879,10 @@ class Commands(object):
         return
 
     def _verify_file(self, file, hash, hashtype):
-        """Given a file, a hash of that file, and a hashtype, verify.
-
-        Returns True if the file verifies, False otherwise
-
-        """
-
-        # get the hash
-        sum = self.lookasidecache.hash_file(file, hashtype=hashtype)
-        # now do the comparison
-        if sum == hash:
-            return True
-        return False
+        warn_deprecated(self.__class__.__name__, '_verify_file',
+                        'lookasidecache.file_is_valid')
+        return self.lookasidecache.file_is_valid(file, hash,
+                                                 hashtype=hashtype)
 
     def _newer(self, file1, file2):
         """Compare the last modification time of the given files
@@ -1590,7 +1582,7 @@ class Commands(object):
             # See if we already have a valid copy downloaded
             outfile = os.path.join(outdir, entry.file)
             if os.path.exists(outfile):
-                if self._verify_file(outfile, entry.hash, entry.hashtype):
+                if self.lookasidecache.file_is_valid(outfile, entry.hash, hashtype=entry.hashtype):
                     continue
             self.log.info("Downloading %s" % (entry.file))
             urled_file = entry.file.replace(' ', '%20')
@@ -1604,7 +1596,7 @@ class Commands(object):
                 command.append('-s')
             command.append(url)
             self._run_command(command)
-            if not self._verify_file(outfile, entry.hash, entry.hashtype):
+            if not self.lookasidecache.file_is_valid(outfile, entry.hash, hashtype=entry.hashtype):
                 raise rpkgError('%s failed checksum' % entry.file)
         return
 
diff --git a/src/pyrpkg/lookaside.py b/src/pyrpkg/lookaside.py
index 0a7e873..e3c1bac 100644
--- a/src/pyrpkg/lookaside.py
+++ b/src/pyrpkg/lookaside.py
@@ -62,3 +62,18 @@ class CGILookasideCache(object):
                 chunk = f.read(8192)
 
         return sum.hexdigest()
+
+    def file_is_valid(self, filename, hash, hashtype=None):
+        """Ensure the file is correct
+
+        Args:
+            filename (str): The full path to the file. It is assumed to exist.
+            hash (str): The known good hash of the file.
+            hashtype (str, optional): The hash algorithm to use. (e.g 'md5')
+                This defaults to the hashtype passed to the constructor.
+
+        Returns:
+            True if the file is valid, False otherwise.
+        """
+        sum = self.hash_file(filename, hashtype)
+        return sum == hash
diff --git a/test/test_lookaside.py b/test/test_lookaside.py
index aea886b..4a4f067 100644
--- a/test/test_lookaside.py
+++ b/test/test_lookaside.py
@@ -53,3 +53,14 @@ class CGILookasideCacheTestCase(unittest.TestCase):
 
         result = lc.hash_file(self.filename, 'md5')
         self.assertEqual(result, 'd41d8cd98f00b204e9800998ecf8427e')
+
+    def test_file_is_valid(self):
+        lc = CGILookasideCache('md5', '_', '_')
+
+        with open(self.filename, 'w') as f:
+            f.write('something')
+
+        self.assertTrue(lc.file_is_valid(self.filename,
+                                         '437b930db84b8079c2dd804a71936b5f'))
+        self.assertFalse(lc.file_is_valid(self.filename, 'not the right hash',
+                                          hashtype='sha512'))
-- 
2.1.0



More information about the rel-eng mailing list