Skip to content

Commit 6939fc7

Browse files
committed
Add Metadata API integration
1 parent 257b667 commit 6939fc7

3 files changed

Lines changed: 58 additions & 13 deletions

File tree

‎pulp_python/app/pypi/views.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,12 +504,18 @@ def retrieve(self, request, path, meta):
504504
headers = {PYPI_LAST_SERIAL: str(PYPI_SERIAL_CONSTANT)}
505505
if settings.DOMAIN_ENABLED:
506506
domain = get_domain()
507+
yank_markers = dict(
508+
PackageYank.objects.filter(
509+
pk__in=repo_ver.content, name_normalized=normalized
510+
).values_list("version", "yanked_reason")
511+
)
507512
json_body = python_content_to_json(
508513
path,
509514
package_content,
510515
version=version,
511516
domain=domain,
512517
repository_version=repo_ver,
518+
yank_markers=yank_markers,
513519
)
514520
if json_body:
515521
return Response(data=json_body, headers=headers)

‎pulp_python/app/utils.py‎

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,12 @@ def fetch_json_release_metadata(name: str, version: str, remotes: set[Remote]) -
362362

363363

364364
def python_content_to_json(
365-
base_path, content_query, version=None, domain=None, repository_version=None
365+
base_path,
366+
content_query,
367+
version=None,
368+
domain=None,
369+
repository_version=None,
370+
yank_markers=None,
366371
):
367372
"""
368373
Converts a QuerySet of PythonPackageContent into the PyPi JSON format
@@ -375,6 +380,8 @@ def python_content_to_json(
375380
376381
Returns None if version is specified but not found within content_query
377382
"""
383+
if yank_markers is None:
384+
yank_markers = {}
378385
if repository_version:
379386
content_query = content_query.annotate(
380387
active_membership=FilteredRelation(
@@ -386,13 +393,20 @@ def python_content_to_json(
386393
),
387394
repo_added_time=F("active_membership__pulp_created"),
388395
)
389-
full_metadata = {"last_serial": 0} # For now the serial field isn't supported by Pulp
390-
latest_content = latest_content_version(content_query, version)
396+
397+
all_content = list(content_query)
398+
for content in all_content:
399+
content.yanked = content.version in yank_markers
400+
content.yanked_reason = yank_markers.get(content.version)
401+
402+
latest_content = latest_content_version(all_content, version)
391403
if not latest_content:
392404
return None
393-
full_metadata.update({"info": python_content_to_info(latest_content[0])})
394-
full_metadata.update({"releases": python_content_to_releases(content_query, base_path, domain)})
395-
full_metadata.update({"urls": python_content_to_urls(latest_content, base_path, domain)})
405+
406+
full_metadata = {"last_serial": 0} # For now the serial field isn't supported by Pulp
407+
full_metadata["info"] = python_content_to_info(latest_content[0])
408+
full_metadata["releases"] = python_content_to_releases(all_content, base_path, domain)
409+
full_metadata["urls"] = python_content_to_urls(latest_content, base_path, domain)
396410
return full_metadata
397411

398412

@@ -462,8 +476,8 @@ def python_content_to_info(content):
462476
"platform": content.platform or "",
463477
"requires_dist": json_to_dict(content.requires_dist) or None,
464478
"classifiers": json_to_dict(content.classifiers) or None,
465-
"yanked": False, # These are no longer used on PyPI, but are still present
466-
"yanked_reason": None,
479+
"yanked": content.yanked,
480+
"yanked_reason": content.yanked_reason,
467481
# New core metadata (Version 2.1, 2.2, 2.4)
468482
"provides_extras": json_to_dict(content.provides_extras) or None,
469483
"dynamic": json_to_dict(content.dynamic) or None,
@@ -472,13 +486,13 @@ def python_content_to_info(content):
472486
}
473487

474488

475-
def python_content_to_releases(content_query, base_path, domain=None):
489+
def python_content_to_releases(contents, base_path, domain=None):
476490
"""
477-
Takes a QuerySet of PythonPackageContent and returns a dictionary of releases
491+
Takes a list of PythonPackageContent and returns a dictionary of releases
478492
with each key being a version and value being a list of content for that version of the package
479493
"""
480494
releases = defaultdict(lambda: [])
481-
for content in content_query:
495+
for content in contents:
482496
releases[content.version].append(
483497
python_content_to_download_info(content, base_path, domain)
484498
)
@@ -535,8 +549,8 @@ def find_artifact():
535549
(getattr(content, "repo_added_time", None) or content.pulp_created).isoformat()
536550
),
537551
"url": url,
538-
"yanked": False,
539-
"yanked_reason": None,
552+
"yanked": content.yanked,
553+
"yanked_reason": content.yanked_reason,
540554
}
541555

542556

‎pulp_python/tests/functional/api/test_yank.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ def test_yank_and_unyank(
7777
response = requests.get(simple_url)
7878
assert response.text.count('data-yanked="broken"') == 2
7979

80+
# PyPI JSON API check - yanked info in metadata
81+
pypi_url = urljoin(distro.base_url, f"pypi/{TWINE_NAME}/json")
82+
response = requests.get(pypi_url)
83+
data = response.json()
84+
assert data["info"]["yanked"] is True
85+
assert data["info"]["yanked_reason"] == "broken"
86+
for f in data["releases"][TWINE_VERSION]:
87+
assert f["yanked"] is True
88+
assert f["yanked_reason"] == "broken"
89+
for f in data["urls"]:
90+
assert f["yanked"] is True
91+
assert f["yanked_reason"] == "broken"
92+
8093
# Yank again - idempotent, no new repo version
8194
response = yank(distro, TWINE_NAME, TWINE_VERSION, yanked_reason="broken")
8295
assert response.status_code == 202
@@ -105,6 +118,18 @@ def test_yank_and_unyank(
105118
response = requests.get(simple_url)
106119
assert "data-yanked" not in response.text
107120

121+
# PyPI JSON API - yanked should be False after unyank
122+
response = requests.get(pypi_url)
123+
data = response.json()
124+
assert data["info"]["yanked"] is False
125+
assert data["info"]["yanked_reason"] is None
126+
for f in data["releases"][TWINE_VERSION]:
127+
assert f["yanked"] is False
128+
assert f["yanked_reason"] is None
129+
for f in data["urls"]:
130+
assert f["yanked"] is False
131+
assert f["yanked_reason"] is None
132+
108133
# Unyank again - idempotent, no new repo version
109134
response = unyank(distro, TWINE_NAME, TWINE_VERSION)
110135
assert response.status_code == 202

0 commit comments

Comments
 (0)