diff --git a/pulp_deb/app/serializers/content_serializers.py b/pulp_deb/app/serializers/content_serializers.py index 168f752d..2c21e677 100644 --- a/pulp_deb/app/serializers/content_serializers.py +++ b/pulp_deb/app/serializers/content_serializers.py @@ -488,12 +488,12 @@ 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["SHA256"] = artifact.sha256 - ret["Size"] = str(artifact.size) + ret.update({ + "SHA256": artifact.sha256, + "Size": str(artifact.size), + **({"MD5sum": artifact.md5} if artifact.md5 else {}), + **({"SHA1": artifact.sha1} if artifact.sha1 else {}), + }) ret["Filename"] = self.instance.filename(component) @@ -600,42 +600,37 @@ 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.update(from822_serializer.validated_data) + data["sha256"] = data["artifact"].sha256 + model_instance = self.Meta.model(**data) + relative_path = data.get("relative_path", model_instance.filename()) - if "relative_path" not in data: - data["relative_path"] = self.Meta.model(**package_data).filename() - elif not os.path.basename(data["relative_path"]) == "{}.{}".format( - self.Meta.model(**package_data).name, self.Meta.model.SUFFIX - ): - data["artifact"].touch() # Orphan cleanup protection so the user can try again! - raise ValidationError(_("Invalid relative_path provided, filename does not match.")) + if os.path.basename(relative_path) != model_instance.filename(): + data["artifact"].touch() # Protect the artifact from orphan cleanup. + raise ValidationError(_("Invalide relative_path provided, filename does not match.")) + data["relative_path"] = relative_path return 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 = ( @@ -1218,8 +1213,8 @@ 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}'" @@ -1237,7 +1232,9 @@ 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 + (