Skip to content

Commit

Permalink
fixup! chore: more cleanup to BuildDependencyCompiler.resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-fs committed Aug 12, 2024
1 parent e8d1501 commit 5b65c60
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/pybuild_deps/compile_build_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,22 @@ def resolve(
log.info("=" * 80)
log.info(str(ireq))
log.info("-" * 80)
ireq_key = key_from_ireq(ireq)
if ireq_key in dependency_cache:
all_build_deps.extend(dependency_cache[ireq_key])
req_str = str(ireq.req)
if req_str in dependency_cache:
all_build_deps.extend(dependency_cache[req_str])
log.debug(f"{ireq.req} was already solved, moving on...")
continue
build_ireqs = set(self._find_build_dependencies(ireq))
if not build_ireqs:
dependency_cache[ireq_key] = set()
dependency_cache[req_str] = set()
continue
try:
# Attempt to resolve ireq's transitive dependencies using
# runtime requirements as constraint. This is same concept of
# "constraint" that can be used with pip, like when running
# "pip install -c constraints.txt some-package"
build_dependencies = self._resolve_with_piptools(
package=ireq_key,
package=req_str,
ireqs=build_ireqs,
constraints=existing_constraints,
)
Expand All @@ -80,7 +80,7 @@ def resolve(
# If this step fails, the same exception will bubble up and explode
# in an error.
build_dependencies = self._resolve_with_piptools(
package=ireq_key,
package=req_str,
ireqs=build_ireqs,
)

Expand All @@ -94,8 +94,11 @@ def resolve(
existing_constraints=existing_constraints,
dependency_cache=dependency_cache,
)
build_dependencies = deduplicate_install_requirements(
build_dependencies
)

dependency_cache[ireq_key] = build_dependencies
dependency_cache[req_str] = build_dependencies

all_build_deps.extend(build_dependencies)

Expand All @@ -105,7 +108,7 @@ def _resolve_with_piptools(
self,
package: str,
ireqs: Iterable[InstallRequirement],
constraints: set[InstallRequirement],
constraints: dict[InstallRequirement] | None = None,
) -> set[InstallRequirement]:
# backup unsafe data before overriding resolver, we will need it later
# on piptools writer to export the file
Expand All @@ -114,7 +117,7 @@ def _resolve_with_piptools(
# override resolver - we don't want references from other
self.resolver = BacktrackingResolver(
constraints=ireqs,
existing_constraints=constraints,
existing_constraints=constraints or {},
repository=self.repository,
allow_unsafe=True,
)
Expand Down Expand Up @@ -148,15 +151,15 @@ def deduplicate_install_requirements(ireqs: Iterable[InstallRequirement]):
"""Deduplicate InstallRequirements."""
unique_ireqs = {}
for ireq in ireqs:
ireq_key = key_from_ireq(ireq)
if ireq_key not in unique_ireqs:
req_tuple = ireq.name, get_version(ireq)
if req_tuple not in unique_ireqs:
# NOTE: piptools hacks pip's InstallRequirement to allow support from
# multiple sources. Let's use the same attr so piptools file writer can
# use this information.
# https://github.com/jazzband/pip-tools/blob/53309647980e2a4981db54c0033f98c61142de0b/piptools/resolver.py#L118-L122
# https://github.com/jazzband/pip-tools/blob/53309647980e2a4981db54c0033f98c61142de0b/piptools/writer.py#L309-L314
ireq._source_ireqs = getattr(ireq, "_source_ireqs", [ireq])
unique_ireqs[ireq_key] = ireq
unique_ireqs[req_tuple] = ireq
else:
unique_ireqs[ireq_key]._source_ireqs.append(ireq)
unique_ireqs[req_tuple]._source_ireqs.append(ireq)
return set(unique_ireqs.values())

0 comments on commit 5b65c60

Please sign in to comment.