Skip to content

Commit

Permalink
add and maintain md5 hashes
Browse files Browse the repository at this point in the history
- add a functions to get md5 hash in build model and spk
- use function in build model when uploading build
- use function in spk when signing/unsigning spk
  • Loading branch information
mreid-tt committed Jan 4, 2024
1 parent ec69e1e commit bd613dc
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
19 changes: 19 additions & 0 deletions spkrepo/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import hashlib
import io
import os
import shutil
Expand Down Expand Up @@ -360,6 +361,24 @@ def save(self, stream):
) as f:
f.write(stream.read())

def calculate_md5(self):
if not self.path:
raise ValueError("Path cannot be empty.")

file_path = os.path.join(current_app.config["DATA_PATH"], self.path)

if not os.path.exists(file_path):
raise FileNotFoundError(f"File not found at path: {file_path}")

if self.md5 is None:
with io.open(file_path, "rb") as f:
md5_hash = hashlib.md5()
for chunk in iter(lambda: f.read(4096), b""):
md5_hash.update(chunk)
return md5_hash.hexdigest()

return self.md5

def _after_insert(self):
assert os.path.exists(os.path.join(current_app.config["DATA_PATH"], self.path))

Expand Down
3 changes: 1 addition & 2 deletions spkrepo/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,7 @@ def create_spk(self, create, extracted, **kwargs):
with create_spk(self) as spk_stream:
self.save(spk_stream)
if self.md5 is None:
spk_stream.seek(0)
self.md5 = hashlib.md5(spk_stream.read()).hexdigest()
self.md5 = self.calculate_md5()
spk_stream.close()

@classmethod
Expand Down
12 changes: 12 additions & 0 deletions spkrepo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,18 @@ def unsign(self):
self.stream.truncate()
self.stream.seek(0)

def calculate_md5(self):
md5_hash = hashlib.md5()

# Ensure the stream position is at the beginning
self.stream.seek(0)

# Update MD5 hash directly from the stream
for chunk in iter(lambda: self.stream.read(4096), b""):
md5_hash.update(chunk)

return md5_hash.hexdigest()

def _generate_signature(self, stream, timestamp_url, gnupghome): # pragma: no cover
# generate the signature
gpg = gnupg.GPG(gnupghome=gnupghome)
Expand Down
12 changes: 12 additions & 0 deletions spkrepo/views/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,11 @@ def action_sign(self, ids):
current_app.config["GNUPG_TIMESTAMP_URL"],
current_app.config["GNUPG_PATH"],
)
build.md5 = spk.calculate_md5()
self.session.commit()
success.append(filename)
except Exception:
self.session.rollback()
failed.append(filename)
if failed:
if len(failed) == 1:
Expand Down Expand Up @@ -479,8 +482,11 @@ def action_unsign(self, ids):
continue
try:
spk.unsign()
build.md5 = spk.calculate_md5()
self.session.commit()
success.append(filename)
except Exception:
self.session.rollback()
failed.append(filename)
if failed:
if len(failed) == 1:
Expand Down Expand Up @@ -683,8 +689,11 @@ def action_sign(self, ids):
current_app.config["GNUPG_TIMESTAMP_URL"],
current_app.config["GNUPG_PATH"],
)
build.md5 = spk.calculate_md5()
self.session.commit()
success.append(filename)
except Exception:
self.session.rollback()
failed.append(filename)
if failed:
if len(failed) == 1:
Expand Down Expand Up @@ -733,8 +742,11 @@ def action_unsign(self, ids):
continue
try:
spk.unsign()
build.md5 = spk.calculate_md5()
self.session.commit()
success.append(filename)
except Exception:
self.session.rollback()
failed.append(filename)
if failed:
if len(failed) == 1:
Expand Down
2 changes: 2 additions & 0 deletions spkrepo/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ def post(self):
for size, icon in build.version.icons.items():
icon.save(spk.icons[size])
build.save(spk.stream)
# generate md5 hash
build.md5 = build.calculate_md5()
except Exception as e: # pragma: no cover
if create_package:
shutil.rmtree(os.path.join(data_path, package.name), ignore_errors=True)
Expand Down

0 comments on commit bd613dc

Please sign in to comment.