Skip to content

Commit

Permalink
Mark wheels with long filenames as unbackfillable (#15439)
Browse files Browse the repository at this point in the history
  • Loading branch information
di authored Feb 21, 2024
1 parent 6666fb8 commit 4d789c8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
50 changes: 49 additions & 1 deletion tests/unit/packaging/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,7 @@ def mock_open(filename, perms):
]


def test_metadata_backfill_file_unbackfillable(db_request, monkeypatch, metrics):
def test_metadata_backfill_file_invalid_wheel(db_request, monkeypatch, metrics):
project = ProjectFactory()
release = ReleaseFactory(project=project)
backfillable_file = FileFactory(
Expand Down Expand Up @@ -1062,6 +1062,54 @@ def test_metadata_backfill_file_unbackfillable(db_request, monkeypatch, metrics)
assert metrics.increment.calls == []


def test_metadata_backfill_file_oserror(db_request, monkeypatch, metrics):
project = ProjectFactory()
release = ReleaseFactory(project=project)
backfillable_file = FileFactory(
release=release, packagetype="bdist_wheel", metadata_file_sha256_digest=None
)

metadata_contents = b"some\nmetadata\ncontents"
stub_dist = pretend.stub(
_dist=pretend.stub(_files={Path("METADATA"): metadata_contents})
)
stub_session = pretend.stub()
dist_from_wheel_url = pretend.call_recorder(
lambda project_name, file_url, session: stub_dist
)
monkeypatch.setattr(
warehouse.packaging.tasks, "dist_from_wheel_url", dist_from_wheel_url
)
stub_session = pretend.stub()
monkeypatch.setattr(warehouse.packaging.tasks, "PipSession", lambda: stub_session)

@contextmanager
def mock_open(filename, perms):
raise OSError

monkeypatch.setattr(builtins, "open", mock_open)

db_request.find_service = pretend.call_recorder(
lambda iface, name=None, context=None: {
IFileStorage: {
"archive": pretend.stub(),
"cache": pretend.stub(),
},
IMetricsService: {None: metrics},
}[iface][name]
)
db_request.registry.settings[
"files.url"
] = "https://files.example.com/packages/{path}"

assert backfillable_file.metadata_file_unbackfillable is False

metadata_backfill_individual(db_request, backfillable_file.id)

assert backfillable_file.metadata_file_unbackfillable is True
assert metrics.increment.calls == []


def test_metadata_backfill_file_delorted(db_request, monkeypatch, metrics):
stub_session = pretend.stub()
monkeypatch.setattr(warehouse.packaging.tasks, "PipSession", lambda: stub_session)
Expand Down
8 changes: 6 additions & 2 deletions warehouse/packaging/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,12 @@ def metadata_backfill_individual(request, file_id):
with tempfile.TemporaryDirectory() as tmpdir:
# Write the metadata to a temporary file
temporary_filename = os.path.join(tmpdir, file_.filename) + ".metadata"
with open(temporary_filename, "wb") as fp:
fp.write(wheel_metadata_contents)
try:
with open(temporary_filename, "wb") as fp:
fp.write(wheel_metadata_contents)
except OSError:
file_.metadata_file_unbackfillable = True
return

# Store the metadata file via our object storage backend
cache_storage.store(
Expand Down

0 comments on commit 4d789c8

Please sign in to comment.