-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] list_depends: allow to sort topologically
- Loading branch information
1 parent
c3ab1f8
commit b304897
Showing
12 changed files
with
251 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add ``--sort`` option on list, list-depends and list-codepends. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import sys | ||
from typing import Dict, Iterable, Set | ||
|
||
import typer | ||
|
||
from manifestoo_core.addons_set import AddonsSet | ||
|
||
from . import echo | ||
from .exceptions import CycleErrorExit | ||
|
||
|
||
class AddonSorter: | ||
@staticmethod | ||
def from_name(name: str) -> "AddonSorter": | ||
if name == "alphabetical": | ||
return AddonSorterAlphabetical() | ||
elif name == "topological": | ||
if sys.version_info < (3, 9): | ||
echo.error( | ||
"The 'topological' sorter requires Python 3.9 or later", | ||
err=False, | ||
) | ||
raise typer.Exit(1) | ||
return AddonSorterTopological() | ||
else: | ||
echo.error(f"Unknown sorter {name}", err=False) | ||
raise typer.Exit(1) | ||
|
||
def sort( | ||
self, addons_selection: Iterable[str], addon_set: AddonsSet | ||
) -> Iterable[str]: | ||
raise NotImplementedError() | ||
|
||
|
||
class AddonSorterAlphabetical(AddonSorter): | ||
def sort( | ||
self, addons_selection: Iterable[str], addon_set: AddonsSet | ||
) -> Iterable[str]: | ||
return sorted(addons_selection) | ||
|
||
|
||
class AddonSorterTopological(AddonSorter): | ||
def sort( | ||
self, addons_selection: Iterable[str], addon_set: AddonsSet | ||
) -> Iterable[str]: | ||
result_dict: Dict[str, Set[str]] = {} | ||
for addon_name in addons_selection: | ||
try: | ||
addon = addon_set[addon_name] | ||
result_dict[addon_name] = set( | ||
depend | ||
for depend in addon.manifest.depends | ||
if depend in addons_selection | ||
) | ||
except KeyError: | ||
echo.debug(f"Addon {addon_name} not found in addon set") | ||
from graphlib import CycleError, TopologicalSorter | ||
|
||
topological_sorted_res = TopologicalSorter(result_dict) | ||
try: | ||
res = list(topological_sorted_res.static_order()) | ||
except CycleError as e: | ||
echo.error("Cycle detected in dependencies", err=False) | ||
raise CycleErrorExit(1) from e | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
from typing import Iterable | ||
from typing import Iterable, Optional | ||
|
||
from manifestoo_core.addons_set import AddonsSet | ||
|
||
from ..addon_sorter import AddonSorter, AddonSorterAlphabetical | ||
from ..addons_selection import AddonsSelection | ||
|
||
|
||
def list_command(addons_selection: AddonsSelection) -> Iterable[str]: | ||
return sorted(addons_selection) | ||
def list_command( | ||
addons_selection: AddonsSelection, | ||
addons_set: AddonsSet, | ||
addon_sorter: Optional[AddonSorter] = None, | ||
) -> Iterable[str]: | ||
if not addon_sorter: | ||
addon_sorter = AddonSorterAlphabetical() | ||
return addon_sorter.sort(addons_selection, addons_set) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from typer import Exit | ||
|
||
|
||
class CycleErrorExit(Exit): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.