-
Notifications
You must be signed in to change notification settings - Fork 18
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 185b99e
Showing
10 changed files
with
169 additions
and
10 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 ``--sorting`` 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,59 @@ | ||
from typing import Dict, Iterable, Set | ||
|
||
import graphlib | ||
import typer | ||
|
||
from manifestoo_core.addons_set import AddonsSet | ||
|
||
from . import echo | ||
|
||
|
||
class AddonSorter: | ||
@staticmethod | ||
def from_name(name: str) -> "AddonSorter": | ||
if name == "alphabetical": | ||
return AddonSorterAlphabetical() | ||
elif name == "topological": | ||
try: | ||
graphlib.TopologicalSorter # noqa B018 | ||
except AttributeError as e: | ||
echo.error( | ||
"The 'topological' sorter requires Python 3.9 or later", | ||
err=False, | ||
) | ||
raise typer.Exit(1) from e | ||
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]: | ||
return addons_selection | ||
|
||
|
||
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: | ||
addon = addon_set[addon_name] | ||
result_dict[addon_name] = set(addon.manifest.depends) | ||
from graphlib import TopologicalSorter | ||
|
||
topological_sorted_res = TopologicalSorter(result_dict) | ||
try: | ||
res = list(topological_sorted_res.static_order()) | ||
except graphlib.CycleError as e: | ||
echo.error("Cycle detected in dependencies", err=False) | ||
raise typer.Exit(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
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