Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions news/d54d50c4-da3a-402b-886d-e6cecc11237a.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve local ``--find-links`` directory scanning performance by avoiding URL
conversion for files that are not valid package archives or HTML pages.
7 changes: 3 additions & 4 deletions src/pip/_internal/index/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ def _scan_directory(self) -> None:
and project_name_to_urls at the same time
"""
for entry in os.scandir(self._path):
url = path_to_url(entry.path)
if _is_html_file(url):
self._page_candidates.append(url)
if _is_html_file(entry.name):
self._page_candidates.append(path_to_url(entry.path))
continue

# File must have a valid wheel or sdist name,
Expand All @@ -75,7 +74,7 @@ def _scan_directory(self) -> None:
except InvalidSdistFilename:
continue

self._project_name_to_urls[project_filename].append(url)
self._project_name_to_urls[project_filename].append(path_to_url(entry.path))
self._scanned_directory = True

@property
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/test_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
_ensure_quoted_url,
)
from pip._internal.network.session import PipSession
from pip._internal.utils.urls import path_to_url

from tests.lib import (
TestData,
Expand Down Expand Up @@ -941,6 +942,36 @@ def check_links_include(links: list[Link], names: list[str]) -> None:
), f"name {name!r} not among links: {links}"


def test_flat_directory_source_ignores_non_package_files_before_url_conversion(
tmp_path: Path,
) -> None:
(tmp_path / "ignored file #1.txt").touch()
(tmp_path / "demo_pkg-1.0-py3-none-any.whl").touch()
(tmp_path / "page with spaces.html").touch()

def guarded_path_to_url(path: str) -> str:
assert "ignored file" not in path
return path_to_url(path)

with mock.patch(
"pip._internal.index.sources.path_to_url", side_effect=guarded_path_to_url
):
source = _FlatDirectorySource(
candidates_from_page=lambda link: [
InstallationCandidate("demo-pkg", "1.0", link)
],
path=os.fspath(tmp_path),
project_name="demo-pkg",
)

file_links = list(source.file_links())
page_candidates = list(source.page_candidates())

check_links_include(file_links, names=["demo_pkg-1.0-py3-none-any.whl"])
assert len(page_candidates) == 1
check_links_include([page_candidates[0].link], names=["page%20with%20spaces.html"])


class TestLinkCollector:
@mock.patch("pip._internal.index.collector._get_simple_response")
def test_fetch_response(self, mock_get_simple_response: mock.Mock) -> None:
Expand Down
Loading