Skip to content

Commit

Permalink
Merge pull request #12569 from ichard26/populate_homepage_from_projec…
Browse files Browse the repository at this point in the history
…t_urls

show: populate missing home-page from project-urls if possible
  • Loading branch information
pradyunsg authored Apr 7, 2024
2 parents 2f49e8f + e0cddaa commit 31437d6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
3 changes: 3 additions & 0 deletions news/11221.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Display the Project-URL value under key "Home-page" in ``pip show`` when the Home-Page metadata field is not set.

The Project-URL key detection is case-insensitive, and ignores any dashes and underscores.
20 changes: 18 additions & 2 deletions src/pip/_internal/commands/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,22 @@ def _get_requiring_packages(current_dist: BaseDistribution) -> Iterator[str]:

metadata = dist.metadata

project_urls = metadata.get_all("Project-URL", [])
homepage = metadata.get("Home-page", "")
if not homepage:
# It's common that there is a "homepage" Project-URL, but Home-page
# remains unset (especially as PEP 621 doesn't surface the field).
#
# This logic was taken from PyPI's codebase.
for url in project_urls:
url_label, url = url.split(",", maxsplit=1)
normalized_label = (
url_label.casefold().replace("-", "").replace("_", "").strip()
)
if normalized_label == "homepage":
homepage = url.strip()
break

yield _PackageInfo(
name=dist.raw_name,
version=str(dist.version),
Expand All @@ -132,8 +148,8 @@ def _get_requiring_packages(current_dist: BaseDistribution) -> Iterator[str]:
metadata_version=dist.metadata_version or "",
classifiers=metadata.get_all("Classifier", []),
summary=metadata.get("Summary", ""),
homepage=metadata.get("Home-page", ""),
project_urls=metadata.get_all("Project-URL", []),
homepage=homepage,
project_urls=project_urls,
author=metadata.get("Author", ""),
author_email=metadata.get("Author-email", ""),
license=metadata.get("License", ""),
Expand Down
22 changes: 22 additions & 0 deletions tests/functional/test_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import re
import textwrap

import pytest

from pip import __version__
from pip._internal.commands.show import search_packages_info
from pip._internal.utils.unpacking import untar_file
Expand Down Expand Up @@ -387,3 +389,23 @@ def test_show_deduplicate_requirements(script: PipTestEnvironment) -> None:
result = script.pip("show", "simple", cwd=pkg_path)
lines = result.stdout.splitlines()
assert "Requires: pip" in lines


@pytest.mark.parametrize(
"project_url", ["Home-page", "home-page", "Homepage", "homepage"]
)
def test_show_populate_homepage_from_project_urls(
script: PipTestEnvironment, project_url: str
) -> None:
pkg_path = create_test_package_with_setup(
script,
name="simple",
version="1.0",
project_urls={project_url: "https://example.com"},
)
script.run("python", "setup.py", "egg_info", expect_stderr=True, cwd=pkg_path)
script.environ.update({"PYTHONPATH": pkg_path})

result = script.pip("show", "simple", cwd=pkg_path)
lines = result.stdout.splitlines()
assert "Home-page: https://example.com" in lines

0 comments on commit 31437d6

Please sign in to comment.