Skip to content

Commit

Permalink
ensure metadata and pgp files are archived along with the actual dist…
Browse files Browse the repository at this point in the history
…ribution file

also resolves #13414
  • Loading branch information
ewdurbin committed May 11, 2023
1 parent 1914a5a commit d85432e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 7 deletions.
60 changes: 59 additions & 1 deletion tests/unit/packaging/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,65 @@ def mock_named_temporary_file():
assert primary_stub.get_metadata.calls == [pretend.call(file.path)]
assert primary_stub.get.calls == [pretend.call(file.path)]
assert archive_stub.store.calls == [
pretend.call(file.path, "/tmp/wutang", meta={"fizz": "buzz"})
pretend.call(file.path, "/tmp/wutang", meta={"fizz": "buzz"}),
]
else:
assert primary_stub.get_metadata.calls == []
assert primary_stub.get.calls == []
assert archive_stub.store.calls == []


@pytest.mark.parametrize("archived", [True, False])
def test_sync_file_to_archive_includes_bonus_files(db_request, monkeypatch, archived):
file = FileFactory(
archived=archived,
has_signature=True,
metadata_file_sha256_digest="deadbeefdeadbeefdeadbeefdeadbeef",
)
primary_stub = pretend.stub(
get_metadata=pretend.call_recorder(lambda path: {"fizz": "buzz"}),
get=pretend.call_recorder(
lambda path: pretend.stub(read=lambda: b"my content")
),
)
archive_stub = pretend.stub(
store=pretend.call_recorder(lambda filename, path, meta=None: None)
)
db_request.find_service = pretend.call_recorder(
lambda iface, name=None: {"primary": primary_stub, "archive": archive_stub}[
name
]
)

@contextmanager
def mock_named_temporary_file():
yield pretend.stub(
name="/tmp/wutang",
write=lambda bites: None,
flush=lambda: None,
)

monkeypatch.setattr(tempfile, "NamedTemporaryFile", mock_named_temporary_file)

sync_file_to_archive(db_request, file.id)

assert file.archived

if not archived:
assert primary_stub.get_metadata.calls == [
pretend.call(file.path),
pretend.call(file.metadata_path),
pretend.call(file.pgp_path),
]
assert primary_stub.get.calls == [
pretend.call(file.path),
pretend.call(file.metadata_path),
pretend.call(file.pgp_path),
]
assert archive_stub.store.calls == [
pretend.call(file.path, "/tmp/wutang", meta={"fizz": "buzz"}),
pretend.call(file.metadata_path, "/tmp/wutang", meta={"fizz": "buzz"}),
pretend.call(file.pgp_path, "/tmp/wutang", meta={"fizz": "buzz"}),
]
else:
assert primary_stub.get_metadata.calls == []
Expand Down
22 changes: 16 additions & 6 deletions warehouse/packaging/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,28 @@
from warehouse.utils import readme


def _copy_file_to_archive(primary_storage, archive_storage, path):
metadata = primary_storage.get_metadata(path)
file_obj = primary_storage.get(path)
with tempfile.NamedTemporaryFile() as file_for_archive:
file_for_archive.write(file_obj.read())
file_for_archive.flush()
archive_storage.store(path, file_for_archive.name, meta=metadata)


@tasks.task(ignore_result=True, acks_late=True)
def sync_file_to_archive(request, file_id):
file = request.db.get(File, file_id)
if not file.archived:
primary_storage = request.find_service(IFileStorage, name="primary")
archive_storage = request.find_service(IFileStorage, name="archive")
metadata = primary_storage.get_metadata(file.path)
file_obj = primary_storage.get(file.path)
with tempfile.NamedTemporaryFile() as file_for_archive:
file_for_archive.write(file_obj.read())
file_for_archive.flush()
archive_storage.store(file.path, file_for_archive.name, meta=metadata)

_copy_file_to_archive(primary_storage, archive_storage, file.path)
if file.metadata_file_sha256_digest is not None:
_copy_file_to_archive(primary_storage, archive_storage, file.metadata_path)
if file.has_signature:
_copy_file_to_archive(primary_storage, archive_storage, file.pgp_path)

file.archived = True


Expand Down

0 comments on commit d85432e

Please sign in to comment.