GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Keep the upload filename when the storage backend renames the file · pulp/pulp_python@09637bd

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • CHANGES
    • +gcs-upload-filename.bugfix
  • pulp_python
    • app/pypi
      • serializers.py
    • tests/unit
      • test_upload_serializer.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎CHANGES/+gcs-upload-filename.bugfix‎

Copy file name to clipboard
+1Lines 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.
Collapse file

‎pulp_python/app/pypi/serializers.py‎

Copy file name to clipboardExpand all lines: pulp_python/app/pypi/serializers.py
+8-6Lines 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+
# Remote storage backends rename 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

Collapse file

‎pulp_python/tests/unit/test_upload_serializer.py‎

Copy file name to clipboard
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import hashlib
2+
from unittest import mock
3+
4+
from django.core.files.base import ContentFile
5+
from django.test import TestCase
6+
7+
from pulpcore.plugin.files import PulpTemporaryUploadedFile
8+
from pulpcore.plugin.util import get_domain
9+
10+
from pulp_python.app.pypi.serializers import PackageUploadSerializer
11+
12+
13+
class TestPackageUploadSerializer(TestCase):
14+
def test_filename_survives_a_storage_that_renames_the_upload(self):
15+
filename = "shelf_reader-0.1-py2-none-any.whl"
16+
payload = b"not a real wheel"
17+
storage = get_domain().get_storage()
18+
original_save = storage._save
19+
20+
def save_and_rename(name, content):
21+
# The Google Cloud Storage backend renames the saved file to its storage path.
22+
saved = original_save(name, content)
23+
content.name = saved
24+
return saved
25+
26+
serializer = PackageUploadSerializer(
27+
data={
28+
"content": PulpTemporaryUploadedFile.from_file(ContentFile(payload, name=filename)),
29+
"sha256_digest": hashlib.sha256(payload).hexdigest(),
30+
}
31+
)
32+
with mock.patch.object(storage, "_save", side_effect=save_and_rename):
33+
self.assertTrue(serializer.is_valid(), serializer.errors)
34+
35+
artifact, uploaded_filename = serializer.validated_data["content"]
36+
self.assertEqual(uploaded_filename, filename)
37+
self.assertNotEqual(artifact.file.name, filename)

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.