Skip to content

Commit

Permalink
Optimize database queries and file handling in content serializers
Browse files Browse the repository at this point in the history
[noissue]
  • Loading branch information
hstct committed Sep 4, 2024
1 parent abe39a3 commit 7bc7444
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions pulp_deb/app/serializers/content_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,13 +488,10 @@ def to822(self, component=""):
artifact = RemoteArtifact.objects.filter(sha256=self.instance.sha256).first()

if artifact:
if artifact.md5:
ret["MD5sum"] = artifact.md5
if artifact.sha1:
ret["SHA1"] = artifact.sha1
ret["MD5sum"] = artifact.md5 if artifact.md5 else None
ret["SHA1"] = artifact.sha1 if artifact.sha1 else None
ret["SHA256"] = artifact.sha256
ret["Size"] = str(artifact.size)

ret["Filename"] = self.instance.filename(component)

return ret
Expand Down Expand Up @@ -600,24 +597,21 @@ def deferred_validate(self, data):
data = super().deferred_validate(data)

try:
package_paragraph = debfile.DebFile(fileobj=data["artifact"].file).debcontrol()
with debfile.DebFile(fileobj=data["artifact"].file) as deb_file:
package_paragraph = deb_file.debcontrol()
except debfile.DebError as e:
if "[Errno 2] No such file or directory: 'unzstd'" in "{}".format(e):
message = (
"The package file provided uses zstd compression, but the unzstd binary is not "
"available! Make sure the zstd package (depending on your package manager) is "
"installed."
)
else:
message = (
"python-debian was unable to read the provided package file! The error is '{}'."
error_message = str(e)
if "unzstd" in error_message:
raise ValidationError(
_("The package file used zstd compression, but 'unzstd' is not available!")
)
raise ValidationError(_(message).format(e))
raise ValidationError(_("Failed to read the package file: {}").format(error_message))

from822_serializer = self.Meta.from822_serializer.from822(data=package_paragraph)
from822_serializer.is_valid(raise_exception=True)
package_data = from822_serializer.validated_data
data.update(package_data)

data["sha256"] = data["artifact"].sha256

if "relative_path" not in data:
Expand All @@ -633,9 +627,9 @@ def deferred_validate(self, data):
def retrieve(self, validated_data):
content = self.Meta.model.objects.filter(
sha256=validated_data["sha256"], relative_path=validated_data["relative_path"]
)
).first()

return content.first()
return content

class Meta:
fields = (
Expand Down Expand Up @@ -1218,16 +1212,16 @@ def deferred_validate(self, data):

artifacts = {data["relative_path"]: data["artifact"]}
for source in data["checksums_sha256"]:
content = Artifact.objects.filter(sha256=source["sha256"], size=source["size"])
if not content.exists():
content = Artifact.objects.get(sha256=source["sha256"], size=source["size"])
if not content:
raise ValidationError(
_(
"A source file is listed in the DSC file but is not yet available '{name}'"
" and sha256 '{sha256}'."
).format(name=source["name"], sha256=source["sha256"])
)
artifacts[os.path.join(os.path.dirname(data["relative_path"]), source["name"])] = (
content.first()
content
)

data["artifacts"] = artifacts
Expand All @@ -1237,7 +1231,11 @@ def retrieve(self, data):
"""
If the Source Package already exists, retrieve it
"""
return SourcePackage.objects.filter(source=data["source"], version=data["version"]).first()
return (
SourcePackage.objects.only("source", "version")
.filter(source=data["source"], version=data["version"])
.first()
)

class Meta:
fields = MultipleArtifactContentSerializer.Meta.fields + (
Expand Down

0 comments on commit 7bc7444

Please sign in to comment.