From 6a28e02cad11443982eefba75b5c681733f0b96d Mon Sep 17 00:00:00 2001 From: John Turner Date: Mon, 29 Sep 2025 00:39:02 -0400 Subject: use installed USE flags to filter reverse dependencies in equery depends --- pym/gentoolkit/dependencies.py | 19 +++++++++++++++++++ pym/gentoolkit/equery/depends.py | 18 +++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/pym/gentoolkit/dependencies.py b/pym/gentoolkit/dependencies.py index be5c71f..45f4759 100644 --- a/pym/gentoolkit/dependencies.py +++ b/pym/gentoolkit/dependencies.py @@ -23,12 +23,23 @@ from gentoolkit import errors from gentoolkit.atom import Atom from gentoolkit.query import Query from gentoolkit.cpv import CPV +from gentoolkit.flag import get_final_flags # ======= # Classes # ======= +@cache +def is_installed(cpv: CPV) -> bool: + return cpv in portage.db[portage.root]["vartree"].dbapi.cpv_all() + + +@cache +def flags_cache(cpv: CPV) -> List[str]: + return get_final_flags(cpv) + + class DependencyKind(Enum): DEPEND = "DEPEND" RDEPEND = "RDEPEND" @@ -190,6 +201,7 @@ class Dependencies(Query): pkgset: Iterable[Union[str, CPV]], max_depth: Optional[int] = None, only_direct: bool = True, + conditionals: bool = False, # The rest of these are only used internally: depth: int = 0, seen: Optional[Set[str]] = None, @@ -238,6 +250,13 @@ class Dependencies(Query): found_match = False for dep in pkgdep.get_all_depends(): if dep.intersects(self): + if conditionals and is_installed(pkgdep.cpv): + if ( + dep.use_conditional is not None + and dep.use_conditional not in flags_cache(pkgdep.cpv) + ): + continue + pkgdep.depatom = dep pkgdep.depth = depth yield pkgdep diff --git a/pym/gentoolkit/equery/depends.py b/pym/gentoolkit/equery/depends.py index 39e0b25..34ac974 100644 --- a/pym/gentoolkit/equery/depends.py +++ b/pym/gentoolkit/equery/depends.py @@ -28,6 +28,7 @@ QUERY_OPTS = { "only_direct": True, "max_depth": None, "package_format": None, + "conditionals": False, } # ======= @@ -154,6 +155,10 @@ def print_help(with_description=True): (" -D, --indirect", "search both direct and indirect dependencies"), (" -F, --format=TMPL", "specify a custom output format"), (" --depth=N", "limit indirect dependency tree to specified depth"), + ( + " --conditionals", + "filter conditional dependencies using your installed flags", + ), ) ) ) @@ -174,6 +179,8 @@ def parse_module_options(module_opts): QUERY_OPTS["only_direct"] = False elif opt in ("-F", "--format"): QUERY_OPTS["package_format"] = posarg + elif opt in ("--conditionals"): + QUERY_OPTS["conditionals"] = True elif opt in ("--depth"): if posarg.isdigit(): depth = int(posarg) @@ -189,7 +196,15 @@ def parse_module_options(module_opts): def main(input_args): """Parse input and run the program""" short_opts = "hadDF:" # -d, --direct was old option for default action - long_opts = ("help", "all-packages", "direct", "indirect", "format", "depth=") + long_opts = ( + "help", + "all-packages", + "direct", + "indirect", + "format", + "depth=", + "conditionals", + ) try: module_opts, queries = gnu_getopt(input_args, short_opts, long_opts) @@ -233,6 +248,7 @@ def main(input_args): pkgset=sorted(pkggetter()), only_direct=QUERY_OPTS["only_direct"], max_depth=QUERY_OPTS["max_depth"], + conditionals=QUERY_OPTS["conditionals"], ): if last_seen is None or last_seen != pkgdep: seen = False -- cgit v1.2.3