Skip to content

Commit

Permalink
resolver.py: Add CustomPackageProvider class
Browse files Browse the repository at this point in the history
JIRA: https://issues.redhat.com/browse/RHELAI-3189

The builder project contains code that attempts to satisfy constraints and
package requirements.  This code is effectively replicating
is_satsified_by() of the GenericProvider class and should be removed from
builder in favor of a new class in fromager.

The new class has find_best_match(), which unlike find_matches() returns
the best match in a provided list of versions.

Suggested-by: Doug Hellmann <[email protected]>
Signed-off-by: Prarit Bhargava <[email protected]>
  • Loading branch information
prarit committed Jan 28, 2025
1 parent 2075b40 commit 6a37714
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/fromager/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,3 +509,52 @@ def _find_tags(
yield entry["tarball_url"], version
# pagination links
nexturl = resp.links.get("next", {}).get("url")


class CustomPackageProvider(BaseProvider):
"""Get best match"""

def __init__(
self,
req: Requirement,
constraints: Constraints,
versions: dict[Version, str],
):
super().__init__(constraints=constraints)
self.req = req
self.versions = versions

def get_cache(self) -> dict[str, list[Candidate]]:
return GenericProvider.generic_resolver_cache

def find_best_match(
self,
) -> Version:
# convert self.versions to candidates
candidates = []
for version, _ in self.versions.items():
# Candidates require a URL for initialization: Use localhost as a place holder
# Candidates must match the requirement name in is_satisfied_call below
candidate = Candidate(self.req.name, version, "https://localhost")
candidates.append(candidate)

constraint = self.constraints.get_constraint(self.req.name)
constraintspec = ""
if constraint is not None:
constraintspec = str(constraint.specifier)

for candidate in reversed(candidates):
if self.is_satisfied_by(self.req, candidate):
logger.info(
f"{self.req.name}{constraintspec} matches {self.req.name} {candidate.version}"
)
return candidate.version
else:
if DEBUG_RESOLVER:
logger.debug(
f"find_match {self.req.name} is rejected by {candidate.version}"
)

raise ValueError(
f"{self.req.name}: {self.req.name}{self.req.specifier} and constraint{constraintspec} conflict. No matches found."
)

0 comments on commit 6a37714

Please sign in to comment.