Skip to content

Commit e8b8c31

Browse files
committed
Keep the upload filename when the storage backend renames the file
The Google Cloud Storage backend sets the name of the saved file object to its storage path. The upload serializer read file.name after it saved the artifact, so the upload task got the storage path instead of the package filename. The task then failed to find a known extension, and the upload was lost. The serializer now reads the filename before it saves the artifact. Assisted By: Claude Code (claude-opus-5-5) Signed-off-by: LilDojd <yawner@pm.me> Change-Id: I07b6af9ae80ac1393e9253df19e5a8ee6a6a6964
1 parent 8807ad9 commit e8b8c31

3 files changed

Lines changed: 35 additions & 6 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed uploads to the PyPI endpoint failing when Pulp stores artifacts in Google Cloud Storage.

‎pulp_python/app/pypi/serializers.py‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,16 @@ def validate(self, data):
9696
if action != "file_upload":
9797
raise serializers.ValidationError(_("We do not support the :action {}").format(action))
9898
file = data.get("content")
99+
# The Google Cloud Storage backend renames the file when the artifact is saved.
100+
filename = file.name
99101
for ext, packagetype in DIST_EXTENSIONS.items():
100-
if file.name.endswith(ext):
102+
if filename.endswith(ext):
101103
if (filetype := data.get("filetype")) and filetype != packagetype:
102104
raise serializers.ValidationError(
103105
{
104106
"filetype": _(
105107
"filetype {} does not match found filetype {} for file {}"
106-
).format(filetype, packagetype, file.name)
108+
).format(filetype, packagetype, filename)
107109
}
108110
)
109111
break
@@ -113,7 +115,7 @@ def validate(self, data):
113115
"content": _(
114116
"Extension on {} is not a valid python extension "
115117
"(.whl, .exe, .egg, .tar.gz, .tar.bz2, .zip)"
116-
).format(file.name)
118+
).format(filename)
117119
}
118120
)
119121

@@ -130,7 +132,7 @@ def validate(self, data):
130132
att_bundle = AttestationBundle(publisher=publisher, attestations=attestations)
131133
provenance = Provenance(attestation_bundles=[att_bundle])
132134
try:
133-
verify_provenance(file.name, sha256, provenance, offline=True)
135+
verify_provenance(filename, sha256, provenance, offline=True)
134136
except AttestationError as e:
135137
raise serializers.ValidationError(
136138
{"attestations": _("Attestations failed verification: {}").format(e)}
@@ -143,8 +145,8 @@ def validate(self, data):
143145
except IntegrityError:
144146
artifact = Artifact.objects.get(sha256=artifact.sha256, pulp_domain=get_domain())
145147
artifact.touch()
146-
log.info(f"Artifact for {file.name} already existed in database")
147-
data["content"] = (artifact, file.name)
148+
log.info(f"Artifact for {filename} already existed in database")
149+
data["content"] = (artifact, filename)
148150
return data
149151

150152

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import hashlib
2+
from unittest import mock
3+
4+
from django.core.files.uploadedfile import SimpleUploadedFile
5+
6+
from pulpcore.plugin.models import Artifact
7+
8+
from pulp_python.app.pypi.serializers import PackageUploadSerializer
9+
10+
11+
def test_upload_keeps_package_filename_when_saving_the_artifact_renames_the_file():
12+
filename = "shelf_reader-0.1-py2-none-any.whl"
13+
payload = b"not a real wheel"
14+
upload = SimpleUploadedFile(filename, payload)
15+
artifact = mock.Mock()
16+
# The Google Cloud Storage backend renames the file when the artifact is saved.
17+
artifact.save.side_effect = lambda: setattr(upload, "name", "artifact/ab/cdef")
18+
19+
serializer = PackageUploadSerializer(
20+
data={"content": upload, "sha256_digest": hashlib.sha256(payload).hexdigest()}
21+
)
22+
with mock.patch.object(Artifact, "init_and_validate", return_value=artifact):
23+
assert serializer.is_valid(), serializer.errors
24+
25+
artifact.save.assert_called_once()
26+
assert serializer.validated_data["content"] == (artifact, filename)

0 commit comments

Comments
 (0)