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 16, 2024
1 parent cf0db3c commit 8d62828
Showing 1 changed file with 24 additions and 30 deletions.
54 changes: 24 additions & 30 deletions pulp_deb/app/serializers/content_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,22 +482,15 @@ def to822(self, component=""):
try:
artifact = self.instance._artifacts.get()
artifact.touch() # Orphan cleanup protection until we are done!
if artifact.md5:
ret["MD5sum"] = artifact.md5
if artifact.sha1:
ret["SHA1"] = artifact.sha1
ret["SHA256"] = artifact.sha256
ret["Size"] = str(artifact.size)
except Artifact.DoesNotExist:
artifact = RemoteArtifact.objects.filter(sha256=self.instance.sha256).first()
if artifact.md5:
ret["MD5sum"] = artifact.md5
if artifact.sha1:
ret["SHA1"] = artifact.sha1
ret["SHA256"] = artifact.sha256
ret["Size"] = str(artifact.size)

ret["Filename"] = self.instance.filename(component)
if artifact:
ret.update({"MD5sum": artifact.md5} if artifact.md5 else {})
ret.update({"SHA1": artifact.sha1} if artifact.sha1 else {})
ret.update({"SHA256": artifact.sha256})
ret.update({"Size": str(artifact.size)})
ret.update({"Filename": self.instance.filename(component)})

return ret

Expand Down Expand Up @@ -602,24 +595,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 @@ -635,9 +625,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 @@ -1220,16 +1210,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 @@ -1239,7 +1229,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 8d62828

Please sign in to comment.