Skip to content

Commit

Permalink
Extract select dependencies code in a separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagokokada committed Oct 26, 2023
1 parent 1f4e08e commit e6e6450
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 34 deletions.
79 changes: 47 additions & 32 deletions nix_alien/libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,46 +56,61 @@ def find_libs(
if not dep.soname or dep.found:
continue

dep_candidates = find_lib_candidates(dep.soname)
selected_dep = None
candidates = find_lib_candidates(dep.soname)
selected_dep = select_dep_from_candidates(
dep.soname, resolved_deps, candidates, select_candidates
)

if len(dep_candidates) == 0:
if selected_dep is None:
_print(f"No candidate found for '{dep.soname}'", file=sys.stderr)
elif len(dep_candidates) == 1:
selected_dep = dep_candidates[0]
else:
if select_candidates:
# Prioritise user selected candidates
selected_dep = next(
(c for c in dep_candidates if re.search(select_candidates, c)), None
)

# Try to find an dependency that is already solved
if not selected_dep:
selected_dep = next(
(d for d in resolved_deps.values() if d in dep_candidates), None
)

# Show FZF to allow user to select the best dependency
if not selected_dep:
fzf_options = join(
[
"--cycle",
"--prompt",
f"Select candidate for '{dep.soname}'> ",
]
)
selected_dep = fzf.prompt(dep_candidates, fzf_options)[0]

_print(
f"Selected candidate for '{dep.soname}': {selected_dep}",
file=sys.stderr,
)
_print(
f"Selected candidate for '{dep.soname}': {selected_dep}",
file=sys.stderr,
)

resolved_deps[dep.soname] = selected_dep

return resolved_deps


def select_dep_from_candidates(
soname: str,
resolved_deps: dict[str, Optional[str]],
candidates: list[str],
select_candidates: Optional[str] = None,
) -> Optional[str]:
# Trivial cases
if len(candidates) == 0:
return None
if len(candidates) == 1:
return candidates[0]

# Prioritise user selected candidates via --select-candidates flag
if select_candidates and (
selected_candidate := next(
(c for c in candidates if re.search(select_candidates, c)), None
)
):
return selected_candidate

# Try to find one match between the already resolved dependencies
if resolved_candidate := next(
(d for d in resolved_deps.values() if d in candidates), None
):
return resolved_candidate

# Show FZF to allow user to select the best dependency
fzf_options = join(
[
"--cycle",
"--prompt",
f"Select candidate for '{soname}'> ",
]
)
return fzf.prompt(candidates, fzf_options)[0]


def get_unique_packages(libs: dict[str, Optional[str]]) -> Iterable[str]:
# remove None values
unique_packages = {lib for lib in libs.values() if lib is not None}
Expand Down
2 changes: 0 additions & 2 deletions tests/test_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ def test_find_libs_when_no_candidates_found(
err
== """\
[nix-alien] No candidate found for 'libfoo.so'
[nix-alien] Selected candidate for 'libfoo.so': None
[nix-alien] No candidate found for 'libbar.so'
[nix-alien] Selected candidate for 'libbar.so': None
"""
)

Expand Down

0 comments on commit e6e6450

Please sign in to comment.