Skip to content

Commit b98d020

Browse files
committed
ensure metadata and pgp files are archived along with the actual distribution file
also resolves #13414
1 parent d4aed1f commit b98d020

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed

tests/unit/packaging/test_tasks.py

+59-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,65 @@ def mock_named_temporary_file():
8484
assert primary_stub.get_metadata.calls == [pretend.call(file.path)]
8585
assert primary_stub.get.calls == [pretend.call(file.path)]
8686
assert archive_stub.store.calls == [
87-
pretend.call(file.path, "/tmp/wutang", meta={"fizz": "buzz"})
87+
pretend.call(file.path, "/tmp/wutang", meta={"fizz": "buzz"}),
88+
]
89+
else:
90+
assert primary_stub.get_metadata.calls == []
91+
assert primary_stub.get.calls == []
92+
assert archive_stub.store.calls == []
93+
94+
95+
@pytest.mark.parametrize("archived", [True, False])
96+
def test_sync_file_to_archive_includes_bonus_files(db_request, monkeypatch, archived):
97+
file = FileFactory(
98+
archived=archived,
99+
has_signature=True,
100+
metadata_file_sha256_digest="deadbeefdeadbeefdeadbeefdeadbeef",
101+
)
102+
primary_stub = pretend.stub(
103+
get_metadata=pretend.call_recorder(lambda path: {"fizz": "buzz"}),
104+
get=pretend.call_recorder(
105+
lambda path: pretend.stub(read=lambda: b"my content")
106+
),
107+
)
108+
archive_stub = pretend.stub(
109+
store=pretend.call_recorder(lambda filename, path, meta=None: None)
110+
)
111+
db_request.find_service = pretend.call_recorder(
112+
lambda iface, name=None: {"primary": primary_stub, "archive": archive_stub}[
113+
name
114+
]
115+
)
116+
117+
@contextmanager
118+
def mock_named_temporary_file():
119+
yield pretend.stub(
120+
name="/tmp/wutang",
121+
write=lambda bites: None,
122+
flush=lambda: None,
123+
)
124+
125+
monkeypatch.setattr(tempfile, "NamedTemporaryFile", mock_named_temporary_file)
126+
127+
sync_file_to_archive(db_request, file.id)
128+
129+
assert file.archived
130+
131+
if not archived:
132+
assert primary_stub.get_metadata.calls == [
133+
pretend.call(file.path),
134+
pretend.call(file.metadata_path),
135+
pretend.call(file.pgp_path),
136+
]
137+
assert primary_stub.get.calls == [
138+
pretend.call(file.path),
139+
pretend.call(file.metadata_path),
140+
pretend.call(file.pgp_path),
141+
]
142+
assert archive_stub.store.calls == [
143+
pretend.call(file.path, "/tmp/wutang", meta={"fizz": "buzz"}),
144+
pretend.call(file.metadata_path, "/tmp/wutang", meta={"fizz": "buzz"}),
145+
pretend.call(file.pgp_path, "/tmp/wutang", meta={"fizz": "buzz"}),
88146
]
89147
else:
90148
assert primary_stub.get_metadata.calls == []

warehouse/packaging/tasks.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,28 @@
2929
from warehouse.utils import readme
3030

3131

32+
def _copy_file_to_archive(primary_storage, archive_storage, path):
33+
metadata = primary_storage.get_metadata(path)
34+
file_obj = primary_storage.get(path)
35+
with tempfile.NamedTemporaryFile() as file_for_archive:
36+
file_for_archive.write(file_obj.read())
37+
file_for_archive.flush()
38+
archive_storage.store(path, file_for_archive.name, meta=metadata)
39+
40+
3241
@tasks.task(ignore_result=True, acks_late=True)
3342
def sync_file_to_archive(request, file_id):
3443
file = request.db.get(File, file_id)
3544
if not file.archived:
3645
primary_storage = request.find_service(IFileStorage, name="primary")
3746
archive_storage = request.find_service(IFileStorage, name="archive")
38-
metadata = primary_storage.get_metadata(file.path)
39-
file_obj = primary_storage.get(file.path)
40-
with tempfile.NamedTemporaryFile() as file_for_archive:
41-
file_for_archive.write(file_obj.read())
42-
file_for_archive.flush()
43-
archive_storage.store(file.path, file_for_archive.name, meta=metadata)
47+
48+
_copy_file_to_archive(primary_storage, archive_storage, file.path)
49+
if file.metadata_file_sha256_digest is not None:
50+
_copy_file_to_archive(primary_storage, archive_storage, file.metadata_path)
51+
if file.has_signature:
52+
_copy_file_to_archive(primary_storage, archive_storage, file.pgp_path)
53+
4454
file.archived = True
4555

4656

0 commit comments

Comments
 (0)