Merge branch 'add-selected-flag-to-all-commands' into 'master'

Add --selected to list and history commands

See merge request khumba/nvd!5
This commit is contained in:
Bryan Gardiner 2024-09-27 02:14:36 +00:00
commit bf4f249ff3
2 changed files with 57 additions and 32 deletions

74
src/nvd
View file

@ -542,9 +542,9 @@ def render_versions(
items.reverse() items.reverse()
return ", ".join(items) return ", ".join(items)
def print_package_list( def print_packages(
*, *,
pnames: list[str], pnames: Iterable[str],
sort_comparator: Optional[PackageListEntryComparator], sort_comparator: Optional[PackageListEntryComparator],
selected_sets: PackageSetPair, selected_sets: PackageSetPair,
left_package_set: PackageSet, left_package_set: PackageSet,
@ -843,7 +843,7 @@ def run_list(
if (path / "sw").is_dir(): if (path / "sw").is_dir():
selected_set = PackageSet.from_direct_dependencies((path / "sw").resolve()) selected_set = PackageSet.from_direct_dependencies((path / "sw").resolve())
else: else:
selected_set = PackageSet.from_direct_dependencies(path / "sw") selected_set = PackageSet.from_direct_dependencies(path)
closure_set = PackageSet.from_closure(path) closure_set = PackageSet.from_closure(path)
@ -860,7 +860,7 @@ def run_list(
target_pnames.sort() target_pnames.sort()
print_package_list( print_packages(
pnames=target_pnames, pnames=target_pnames,
selected_sets=PackageSetPair(selected_set, selected_set), selected_sets=PackageSetPair(selected_set, selected_set),
left_package_set=closure_set, left_package_set=closure_set,
@ -875,6 +875,7 @@ def run_diff(
root2, root2,
no_version_suffix_highlight: bool, no_version_suffix_highlight: bool,
sort_comparator: PackageListEntryComparator, sort_comparator: PackageListEntryComparator,
only_selected: bool,
): ):
left_path = Path(root1) left_path = Path(root1)
right_path = Path(root2) right_path = Path(root2)
@ -908,29 +909,41 @@ def run_diff(
left_closure_set = PackageSet.from_closure(left_resolved) left_closure_set = PackageSet.from_closure(left_resolved)
right_closure_set = PackageSet.from_closure(right_resolved) right_closure_set = PackageSet.from_closure(right_resolved)
left_package_names = set(left_closure_set.all_pnames()) left_closure_all_package_names = set(left_closure_set.all_pnames())
right_package_names = set(right_closure_set.all_pnames()) right_closure_all_package_names = set(right_closure_set.all_pnames())
common_package_names = left_package_names & right_package_names left_selected_package_names = set(left_selected_set.all_pnames())
left_only_package_names = left_package_names - right_package_names right_selected_package_names = set(right_selected_set.all_pnames())
right_only_package_names = right_package_names - left_package_names
# Compute changes to versions and selection states.
pnames_selected_in_either_closure = left_selected_package_names | right_selected_package_names
pnames_in_both_closures = left_closure_all_package_names & right_closure_all_package_names
# Announce version changes.
package_names_with_changed_versions = [] package_names_with_changed_versions = []
package_names_with_changed_selection_states = [] package_names_with_changed_selection_states = []
for pname in common_package_names: for pname in pnames_in_both_closures:
if only_selected and pname not in pnames_selected_in_either_closure:
continue
if left_closure_set.get_pname_versions(pname) != right_closure_set.get_pname_versions(pname): if left_closure_set.get_pname_versions(pname) != right_closure_set.get_pname_versions(pname):
package_names_with_changed_versions.append(pname) package_names_with_changed_versions.append(pname)
elif selected_sets.is_selection_state_changed(pname): elif selected_sets.is_selection_state_changed(pname):
package_names_with_changed_selection_states.append(pname) package_names_with_changed_selection_states.append(pname)
# Compute added and removed packages.
added_package_names = right_closure_all_package_names - left_closure_all_package_names
removed_package_names = left_closure_all_package_names - right_closure_all_package_names
if only_selected:
added_package_names &= pnames_selected_in_either_closure
removed_package_names &= pnames_selected_in_either_closure
any_changes_displayed = False any_changes_displayed = False
# Announce version changes. # Announce version changes.
if package_names_with_changed_versions != []: if package_names_with_changed_versions:
any_changes_displayed = True any_changes_displayed = True
print("Version changes:") print("Version changes:")
print_package_list( print_packages(
pnames=package_names_with_changed_versions, pnames=package_names_with_changed_versions,
selected_sets=selected_sets, selected_sets=selected_sets,
left_package_set=left_closure_set, left_package_set=left_closure_set,
@ -940,10 +953,10 @@ def run_diff(
) )
# Announce specific changes for packages whose versions haven't changed. # Announce specific changes for packages whose versions haven't changed.
if package_names_with_changed_selection_states != []: if package_names_with_changed_selection_states:
any_changes_displayed = True any_changes_displayed = True
print("Selection state changes:") print("Selection state changes:")
print_package_list( print_packages(
pnames=package_names_with_changed_selection_states, pnames=package_names_with_changed_selection_states,
selected_sets=selected_sets, selected_sets=selected_sets,
left_package_set=left_closure_set, left_package_set=left_closure_set,
@ -952,11 +965,11 @@ def run_diff(
) )
# Announce added packages. # Announce added packages.
if right_only_package_names != []: if added_package_names:
any_changes_displayed = True any_changes_displayed = True
print("Added packages:") print("Added packages:")
print_package_list( print_packages(
pnames=right_only_package_names, pnames=added_package_names,
selected_sets=selected_sets, selected_sets=selected_sets,
left_package_set=right_closure_set, # Yes, this is correct. left_package_set=right_closure_set, # Yes, this is correct.
fixed_install_state=INST_ADDED, fixed_install_state=INST_ADDED,
@ -964,11 +977,11 @@ def run_diff(
) )
# Announce removed packages. # Announce removed packages.
if left_only_package_names != []: if removed_package_names:
any_changes_displayed = True any_changes_displayed = True
print("Removed packages:") print("Removed packages:")
print_package_list( print_packages(
pnames=left_only_package_names, pnames=removed_package_names,
selected_sets=selected_sets, selected_sets=selected_sets,
left_package_set=left_closure_set, left_package_set=left_closure_set,
fixed_install_state=INST_REMOVED, fixed_install_state=INST_REMOVED,
@ -1008,6 +1021,7 @@ def run_history(
list_oldest: bool, list_oldest: bool,
no_version_suffix_highlight: bool, no_version_suffix_highlight: bool,
sort_comparator: PackageListEntryComparator, sort_comparator: PackageListEntryComparator,
only_selected: bool,
): ):
profile_path = Path(profile) profile_path = Path(profile)
@ -1034,7 +1048,7 @@ def run_history(
print(f">>> {oldest_profile.path()}") print(f">>> {oldest_profile.path()}")
run_list( run_list(
root=oldest_profile.path(), root=oldest_profile.path(),
only_selected=False, only_selected=only_selected,
name_patterns=[], name_patterns=[],
sort_comparator=sort_comparator sort_comparator=sort_comparator
) )
@ -1049,9 +1063,17 @@ def run_history(
root2=displayed_profile.path(), root2=displayed_profile.path(),
no_version_suffix_highlight=no_version_suffix_highlight, no_version_suffix_highlight=no_version_suffix_highlight,
sort_comparator=sort_comparator, sort_comparator=sort_comparator,
only_selected=only_selected,
) )
def parse_args(): def parse_args():
def add_selected_arg(p: argparse.ArgumentParser) -> None:
p.add_argument(
"-s", "--selected",
dest="only_selected",
action="store_true",
help="Only show selected packages (direct dependencies).")
def add_sort_arg(p: argparse.ArgumentParser) -> None: def add_sort_arg(p: argparse.ArgumentParser) -> None:
p.add_argument( p.add_argument(
"--sort", "--sort",
@ -1085,6 +1107,7 @@ def parse_args():
diff_parser = subparsers.add_parser( diff_parser = subparsers.add_parser(
"diff", "diff",
help="Diff two Nix store paths and their closures.") help="Diff two Nix store paths and their closures.")
add_selected_arg(diff_parser)
add_sort_arg(diff_parser) add_sort_arg(diff_parser)
diff_parser.add_argument( diff_parser.add_argument(
"--no-version-suffix-highlight", "--no-version-suffix-highlight",
@ -1100,16 +1123,12 @@ def parse_args():
list_parser = subparsers.add_parser( list_parser = subparsers.add_parser(
"list", "list",
help="List packages in the closure of a Nix store path.") help="List packages in the closure of a Nix store path.")
add_selected_arg(list_parser)
add_sort_arg(list_parser) add_sort_arg(list_parser)
list_parser.add_argument( list_parser.add_argument(
"-r", "--root", "-r", "--root",
default="/run/current-system", default="/run/current-system",
help="The The Nix store path to work with.") help="The The Nix store path to work with.")
list_parser.add_argument(
"-s", "--selected",
dest="only_selected",
action="store_true",
help="Only show selected packages (direct dependencies).")
list_parser.add_argument( list_parser.add_argument(
nargs="*", nargs="*",
default=[], default=[],
@ -1119,6 +1138,7 @@ def parse_args():
history_parser = subparsers.add_parser( history_parser = subparsers.add_parser(
"history", "history",
help="Show the history of a Nix profile.") help="Show the history of a Nix profile.")
add_selected_arg(history_parser)
add_sort_arg(history_parser) add_sort_arg(history_parser)
history_parser.add_argument( history_parser.add_argument(
"-p", "--profile", "-p", "--profile",

View file

@ -7,6 +7,8 @@ nvd \- Nix/NixOS package version diff tool
.P .P
.B nvd [ GLOBAL OPTIONS ] diff .B nvd [ GLOBAL OPTIONS ] diff
.RS .RS
.B [ -s|--selected ]
.br
.B [ --sort .B [ --sort
.I sort-order .I sort-order
.B ] .B ]
@ -37,6 +39,11 @@ nvd \- Nix/NixOS package version diff tool
.RS .RS
.B [ -p|--profile .B [ -p|--profile
.I profile .I profile
.br
.B [ -s|--selected ]
.br
.B [ --sort
.I sort-order
.B ] .B ]
.br .br
.B [ -m|--minimum-version .B [ -m|--minimum-version
@ -158,6 +165,9 @@ etc. binaries to use. If provided, all invocations of Nix binaries will use
this directory. If empty or not provided, then invocations of Nix binaries will this directory. If empty or not provided, then invocations of Nix binaries will
use regular path lookup. This is the default. use regular path lookup. This is the default.
.TP .TP
-s|--selected
Only print out selected packages (direct dependencies).
.TP
--sort=<sort-order> --sort=<sort-order>
Specifies the order to use to sort package lists. The argument is a Specifies the order to use to sort package lists. The argument is a
comma-separted list of keywords indicating which properties to sort on. The comma-separted list of keywords indicating which properties to sort on. The
@ -173,11 +183,6 @@ For the
.B list .B list
command, this is the root store path to use, or a link to the store path to use. command, this is the root store path to use, or a link to the store path to use.
.TP .TP
-s|--selected
For the
.B list
command, only print out selected packages (direct dependencies).
.TP
<name-pattern> <name-pattern>
For the For the
.B list .B list