]> git.ipfire.org Git - pbs.git/commitdiff
uploads: Add simple function to compute digest
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 15 Jul 2022 09:53:57 +0000 (09:53 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 15 Jul 2022 09:53:57 +0000 (09:53 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/buildservice/uploads.py

index d79c0f7100e15a8274cb56bf7ebc35686c4eb60f..d8e6bf5a2d4fbd996e350adf8d30486c83245815 100644 (file)
@@ -1,8 +1,8 @@
 #!/usr/bin/python3
 
+import hashlib
 import logging
 import os
-import shutil
 import tempfile
 
 from . import base
@@ -159,3 +159,25 @@ class Upload(base.DataObject):
        @property
        def expires_at(self):
                return self.data.expires_at
+
+       async def digest(self, algo):
+               """
+                       Computes the digest of this download
+               """
+               log.debug("Computing '%s' digest for %s" % (algo, self))
+
+               return await asyncio.to_thread(self._digest, algo)
+
+       def _digest(self, algo):
+               h = hashlib.new(algo)
+
+               # Read the entire file and pass it to the hash function
+               with open(self.path, "rb") as f:
+                       buf = f.read(4096)
+                       if not buf:
+                               break
+
+                       h.update(buf)
+
+               # Return the digest
+               return h.digest()