diff --git a/CHANGES/1371.bugfix b/CHANGES/1371.bugfix new file mode 100644 index 00000000..13a57c03 --- /dev/null +++ b/CHANGES/1371.bugfix @@ -0,0 +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. diff --git a/pulp_python/app/pypi/views.py b/pulp_python/app/pypi/views.py index 8d580a2f..4846c914 100644 --- a/pulp_python/app/pypi/views.py +++ b/pulp_python/app/pypi/views.py @@ -98,6 +98,27 @@ def _last_modified_func(request, path, **kwargs): return repo_ver.pulp_created +def merge_pull_through_releases(remote_releases, local_releases): + """ + Merge the files a pull-through remote lists for a package with those in the repository. + + A file served from the repository replaces the remote's entry, so the client downloads the + copy Pulp holds. When the remote lists the same file (same name and sha256), its upload time + is the file's real one, while the repository's is when the file was added to it, and a yank + on the remote still applies to the cached copy. + """ + releases = dict(remote_releases) + for filename, local in local_releases.items(): + remote = remote_releases.get(filename) + if remote is not None and remote.get("sha256") == local.get("sha256"): + local = {**local, "upload_time": remote.get("upload_time") or local["upload_time"]} + if remote.get("yanked") and not local.get("yanked"): + local["yanked"] = True + local["yanked_reason"] = remote.get("yanked_reason", "") + releases[filename] = local + return releases + + class PyPISimpleHTMLRenderer(TemplateHTMLRenderer): media_type = PYPI_SIMPLE_V1_HTML @@ -457,7 +478,7 @@ def retrieve(self, request, path, package): } for p in packages } - releases.update(local_releases) + releases = merge_pull_through_releases(releases, local_releases) if not releases: return HttpResponseNotFound(f"{normalized} does not exist.") diff --git a/pulp_python/tests/unit/test_pull_through_releases.py b/pulp_python/tests/unit/test_pull_through_releases.py new file mode 100644 index 00000000..26676882 --- /dev/null +++ b/pulp_python/tests/unit/test_pull_through_releases.py @@ -0,0 +1,81 @@ +from datetime import datetime, timezone + +from pulp_python.app.pypi.views import merge_pull_through_releases + +UPSTREAM_TIME = datetime(2024, 1, 2, tzinfo=timezone.utc) +CACHED_TIME = datetime(2026, 11, 1, tzinfo=timezone.utc) + + +def remote_entry(sha256="aaa", yanked=False, yanked_reason="", upload_time=UPSTREAM_TIME): + return { + "filename": "pkg-1.0-py3-none-any.whl", + "url": "https://pulp/pypi/cache/pkg-1.0-py3-none-any.whl?redirect=https://upstream/x", + "sha256": sha256, + "upload_time": upload_time, + "version": "1.0", + "yanked": yanked, + "yanked_reason": yanked_reason, + } + + +def local_entry(sha256="aaa", yanked=False, yanked_reason=""): + return { + "filename": "pkg-1.0-py3-none-any.whl", + "url": "https://pulp/pypi/cache/pkg-1.0-py3-none-any.whl", + "sha256": sha256, + "upload_time": CACHED_TIME, + "version": "1.0", + "yanked": yanked, + "yanked_reason": yanked_reason, + } + + +NAME = "pkg-1.0-py3-none-any.whl" + + +def test_cached_file_keeps_the_remote_upload_time(): + merged = merge_pull_through_releases({NAME: remote_entry()}, {NAME: local_entry()}) + + assert merged[NAME]["upload_time"] == UPSTREAM_TIME + assert merged[NAME]["url"] == local_entry()["url"] + + +def test_cached_file_yanked_on_the_remote_is_yanked(): + merged = merge_pull_through_releases( + {NAME: remote_entry(yanked=True, yanked_reason="broken build")}, {NAME: local_entry()} + ) + + assert merged[NAME]["yanked"] is True + assert merged[NAME]["yanked_reason"] == "broken build" + + +def test_local_yank_is_kept(): + merged = merge_pull_through_releases( + {NAME: remote_entry()}, {NAME: local_entry(yanked=True, yanked_reason="ours")} + ) + + assert merged[NAME]["yanked"] is True + assert merged[NAME]["yanked_reason"] == "ours" + + +def test_different_file_under_the_same_name_takes_nothing_from_the_remote(): + merged = merge_pull_through_releases( + {NAME: remote_entry(sha256="bbb", yanked=True)}, {NAME: local_entry()} + ) + + assert merged[NAME] == local_entry() + + +def test_remote_without_upload_time_keeps_the_local_one(): + merged = merge_pull_through_releases( + {NAME: remote_entry(upload_time=None)}, {NAME: local_entry()} + ) + + assert merged[NAME]["upload_time"] == CACHED_TIME + + +def test_files_on_only_one_side_are_listed_unchanged(): + other = {**remote_entry(), "filename": "pkg-2.0.tar.gz"} + merged = merge_pull_through_releases({"pkg-2.0.tar.gz": other}, {NAME: local_entry()}) + + assert merged == {"pkg-2.0.tar.gz": other, NAME: local_entry()}