Skip to content

Commit aa6b596

Browse files
aevnsvigerrod3
authored andcommitted
Keep the remote's upload time and yank on cached pull-through files
On a pull-through distribution with a repository, a file already in the repository replaced the remote's entry on the simple page completely. Its upload time became the time it was cached, so tools filtering releases by upload time (uv --exclude-newer, pip --uploaded-prior-to) saw old files as new; and a yank on the remote, before or after caching, no longer showed. When the remote lists the same file (same name and sha256), the entry now keeps the remote's upload time and is yanked when the remote yanked it. The URL and the rest still come from the repository. Closes: #1371
1 parent 2e1ce32 commit aa6b596

3 files changed

Lines changed: 104 additions & 1 deletion

File tree

‎CHANGES/1371.bugfix‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Pull-through simple pages now show the remote's upload time for files already cached in the repository, instead of the time they were cached, and mark them yanked when the remote has yanked them.

‎pulp_python/app/pypi/views.py‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,27 @@ def _last_modified_func(request, path, **kwargs):
9898
return repo_ver.pulp_created
9999

100100

101+
def merge_pull_through_releases(remote_releases, local_releases):
102+
"""
103+
Merge the files a pull-through remote lists for a package with those in the repository.
104+
105+
A file served from the repository replaces the remote's entry, so the client downloads the
106+
copy Pulp holds. When the remote lists the same file (same name and sha256), its upload time
107+
is the file's real one, while the repository's is when the file was added to it, and a yank
108+
on the remote still applies to the cached copy.
109+
"""
110+
releases = dict(remote_releases)
111+
for filename, local in local_releases.items():
112+
remote = remote_releases.get(filename)
113+
if remote is not None and remote.get("sha256") == local.get("sha256"):
114+
local = {**local, "upload_time": remote.get("upload_time") or local["upload_time"]}
115+
if remote.get("yanked") and not local.get("yanked"):
116+
local["yanked"] = True
117+
local["yanked_reason"] = remote.get("yanked_reason", "")
118+
releases[filename] = local
119+
return releases
120+
121+
101122
class PyPISimpleHTMLRenderer(TemplateHTMLRenderer):
102123
media_type = PYPI_SIMPLE_V1_HTML
103124

@@ -457,7 +478,7 @@ def retrieve(self, request, path, package):
457478
}
458479
for p in packages
459480
}
460-
releases.update(local_releases)
481+
releases = merge_pull_through_releases(releases, local_releases)
461482
if not releases:
462483
return HttpResponseNotFound(f"{normalized} does not exist.")
463484

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
from datetime import datetime, timezone
2+
3+
from pulp_python.app.pypi.views import merge_pull_through_releases
4+
5+
UPSTREAM_TIME = datetime(2024, 1, 2, tzinfo=timezone.utc)
6+
CACHED_TIME = datetime(2026, 11, 1, tzinfo=timezone.utc)
7+
8+
9+
def remote_entry(sha256="aaa", yanked=False, yanked_reason="", upload_time=UPSTREAM_TIME):
10+
return {
11+
"filename": "pkg-1.0-py3-none-any.whl",
12+
"url": "https://pulp/pypi/cache/pkg-1.0-py3-none-any.whl?redirect=https://upstream/x",
13+
"sha256": sha256,
14+
"upload_time": upload_time,
15+
"version": "1.0",
16+
"yanked": yanked,
17+
"yanked_reason": yanked_reason,
18+
}
19+
20+
21+
def local_entry(sha256="aaa", yanked=False, yanked_reason=""):
22+
return {
23+
"filename": "pkg-1.0-py3-none-any.whl",
24+
"url": "https://pulp/pypi/cache/pkg-1.0-py3-none-any.whl",
25+
"sha256": sha256,
26+
"upload_time": CACHED_TIME,
27+
"version": "1.0",
28+
"yanked": yanked,
29+
"yanked_reason": yanked_reason,
30+
}
31+
32+
33+
NAME = "pkg-1.0-py3-none-any.whl"
34+
35+
36+
def test_cached_file_keeps_the_remote_upload_time():
37+
merged = merge_pull_through_releases({NAME: remote_entry()}, {NAME: local_entry()})
38+
39+
assert merged[NAME]["upload_time"] == UPSTREAM_TIME
40+
assert merged[NAME]["url"] == local_entry()["url"]
41+
42+
43+
def test_cached_file_yanked_on_the_remote_is_yanked():
44+
merged = merge_pull_through_releases(
45+
{NAME: remote_entry(yanked=True, yanked_reason="broken build")}, {NAME: local_entry()}
46+
)
47+
48+
assert merged[NAME]["yanked"] is True
49+
assert merged[NAME]["yanked_reason"] == "broken build"
50+
51+
52+
def test_local_yank_is_kept():
53+
merged = merge_pull_through_releases(
54+
{NAME: remote_entry()}, {NAME: local_entry(yanked=True, yanked_reason="ours")}
55+
)
56+
57+
assert merged[NAME]["yanked"] is True
58+
assert merged[NAME]["yanked_reason"] == "ours"
59+
60+
61+
def test_different_file_under_the_same_name_takes_nothing_from_the_remote():
62+
merged = merge_pull_through_releases(
63+
{NAME: remote_entry(sha256="bbb", yanked=True)}, {NAME: local_entry()}
64+
)
65+
66+
assert merged[NAME] == local_entry()
67+
68+
69+
def test_remote_without_upload_time_keeps_the_local_one():
70+
merged = merge_pull_through_releases(
71+
{NAME: remote_entry(upload_time=None)}, {NAME: local_entry()}
72+
)
73+
74+
assert merged[NAME]["upload_time"] == CACHED_TIME
75+
76+
77+
def test_files_on_only_one_side_are_listed_unchanged():
78+
other = {**remote_entry(), "filename": "pkg-2.0.tar.gz"}
79+
merged = merge_pull_through_releases({"pkg-2.0.tar.gz": other}, {NAME: local_entry()})
80+
81+
assert merged == {"pkg-2.0.tar.gz": other, NAME: local_entry()}

0 commit comments

Comments
 (0)