Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/+gcs-upload-filename.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed uploads to the PyPI endpoint failing when Pulp stores artifacts in Google Cloud Storage.
10 changes: 6 additions & 4 deletions pulp_python/app/pypi/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,17 @@ def validate(self, data):
if action != "file_upload":
raise serializers.ValidationError(_("We do not support the :action {}").format(action))
file = data.get("content")
# The Google Cloud Storage backend renames the file when the artifact is saved.
filename = file.name
for ext, packagetype in DIST_EXTENSIONS.items():
if file.name.endswith(ext):
if filename.endswith(ext):
break
else:
raise serializers.ValidationError(
_(
"Extension on {} is not a valid python extension "
"(.whl, .exe, .egg, .tar.gz, .tar.bz2, .zip)"
).format(file.name)
).format(filename)
)
sha256 = data.get("sha256_digest")
digests = {"sha256": sha256} if sha256 else None
Expand All @@ -82,8 +84,8 @@ def validate(self, data):
except IntegrityError:
artifact = Artifact.objects.get(sha256=artifact.sha256, pulp_domain=get_domain())
artifact.touch()
log.info(f"Artifact for {file.name} already existed in database")
data["content"] = (artifact, file.name)
log.info(f"Artifact for {filename} already existed in database")
data["content"] = (artifact, filename)
return data


Expand Down
26 changes: 26 additions & 0 deletions pulp_python/tests/unit/test_upload_serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import hashlib
from unittest import mock

from django.core.files.uploadedfile import SimpleUploadedFile

from pulpcore.plugin.models import Artifact

from pulp_python.app.pypi.serializers import PackageUploadSerializer


def test_upload_keeps_package_filename_when_saving_the_artifact_renames_the_file():
filename = "shelf_reader-0.1-py2-none-any.whl"
payload = b"not a real wheel"
upload = SimpleUploadedFile(filename, payload)
artifact = mock.Mock()
# The Google Cloud Storage backend renames the file when the artifact is saved.
artifact.save.side_effect = lambda: setattr(upload, "name", "artifact/ab/cdef")

serializer = PackageUploadSerializer(
data={"content": upload, "sha256_digest": hashlib.sha256(payload).hexdigest()}
)
with mock.patch.object(Artifact, "init_and_validate", return_value=artifact):
assert serializer.is_valid(), serializer.errors

artifact.save.assert_called_once()
assert serializer.validated_data["content"] == (artifact, filename)
1 change: 1 addition & 0 deletions unittest_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mock
pytest<8
pytest-django
Loading