Skip to content

Commit

Permalink
fix: Link collection ignores package-index-binding (#2444)
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming authored Nov 29, 2023
1 parent 4f06613 commit 766a56b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
1 change: 1 addition & 0 deletions news/2442.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug that link collection ignores package-index-binding.
4 changes: 3 additions & 1 deletion src/pdm/models/candidates.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
cd,
convert_hashes,
create_tracked_tempdir,
filtered_sources,
get_rev_from_url,
normalize_name,
path_to_url,
Expand Down Expand Up @@ -427,7 +428,8 @@ def obtain(self, allow_all: bool = False, unpack: bool = True) -> None:
elif self._source_dir and self._source_dir.exists():
return

with self.environment.get_finder() as finder:
sources = filtered_sources(self.environment.project.sources, self.req.key)
with self.environment.get_finder(sources) as finder:
if not self.link or self.link.is_wheel and not self._wheel_compatible(self.link.filename, allow_all):
if self.req.is_file_or_url:
raise CandidateNotFound(f"The URL requirement {self.req.as_line()} is a wheel but incompatible")
Expand Down
18 changes: 2 additions & 16 deletions src/pdm/models/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from pdm.models.specifiers import PySpecSet
from pdm.utils import (
cd,
filtered_sources,
normalize_name,
path_to_url,
url_to_path,
Expand Down Expand Up @@ -76,22 +77,7 @@ def __init__(

def get_filtered_sources(self, req: Requirement) -> list[RepositoryConfig]:
"""Get matching sources based on the index attribute."""
source_preferences = [(s, self.source_preference(req, s)) for s in self.sources]
included_by = [s for s, p in source_preferences if p is True]
if included_by:
return included_by
return [s for s, p in source_preferences if p is None]

@staticmethod
def source_preference(req: Requirement, source: RepositoryConfig) -> bool | None:
key = req.key
if key is None:
return None
if any(fnmatch.fnmatch(key, pat) for pat in source.include_packages):
return True
if any(fnmatch.fnmatch(key, pat) for pat in source.exclude_packages):
return False
return None
return filtered_sources(self.sources, req.key)

def get_dependencies(self, candidate: Candidate) -> tuple[list[Requirement], PySpecSet, str]:
"""Get (dependencies, python_specifier, summary) of the candidate."""
Expand Down
22 changes: 22 additions & 0 deletions src/pdm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,25 @@ def is_conda_base_python(python: Path) -> bool:
except ValueError:
return False
return True


def filtered_sources(sources: list[RepositoryConfig], package: str | None) -> list[RepositoryConfig]:
"""Get matching sources based on the index attribute."""
source_preferences = [(s, _source_preference(package, s)) for s in sources]
included_by = [s for s, p in source_preferences if p is True]
if included_by:
return included_by
return [s for s, p in source_preferences if p is None]


def _source_preference(package: str | None, source: RepositoryConfig) -> bool | None:
import fnmatch

if package is None:
return None
key = normalize_name(package)
if any(fnmatch.fnmatch(key, pat) for pat in source.include_packages):
return True
if any(fnmatch.fnmatch(key, pat) for pat in source.exclude_packages):
return False
return None

0 comments on commit 766a56b

Please sign in to comment.