Skip to content

Commit

Permalink
make --dot and --json flags honor pkg exclude options, fixes #24
Browse files Browse the repository at this point in the history
  • Loading branch information
rvalieris committed Nov 20, 2024
1 parent 73b68ab commit 3d82204
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
37 changes: 28 additions & 9 deletions conda-tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import conda.base.context
import networkx

__version__ = '1.1.0'
__version__ = '1.1.1'

# The number of spaces
TABSIZE = 3
Expand All @@ -38,10 +38,11 @@ def make_cache_graph(cache):
g.add_edge(n, n2, version=v2)
return(g)

def print_graph_dot(g):
def print_graph_dot(g, exclude_pkgs=set()):
print("digraph {")
for k,v in g.edges():
print(f" \"{k}\" -> \"{v}\"")
if k not in exclude_pkgs and v not in exclude_pkgs:
print(f" \"{k}\" -> \"{v}\"")
print("}")

def remove_from_graph(g, node, _cache=None):
Expand Down Expand Up @@ -175,8 +176,13 @@ def is_node_reachable(graph, source, target):
for s in source:
if is_node_reachable(graph, s, target):
return True
return False
else:
return any(networkx.algorithms.simple_paths.all_simple_paths(graph, source, target))
try:
paths = networkx.shortest_path(graph, source, target)
return len(paths)>0
except:
return False

def print_pkgs(pkgs, with_json=False):
if with_json:
Expand All @@ -185,6 +191,21 @@ def print_pkgs(pkgs, with_json=False):
for p in pkgs:
print(p)

def find_reachable_pkgs(graph, pkg, down_search=True, exclude_pkgs=set()):
if down_search:
paths = networkx.shortest_path(graph, source=pkg)
else:
paths = networkx.shortest_path(graph, target=pkg)

reachable_pkgs = []
for k, v in paths.items():
if len(exclude_pkgs.intersection(v)) > 0 and k not in exclude_pkgs:
pass # remove paths with excluded packages
elif k != pkg:
reachable_pkgs.append(k)

return reachable_pkgs

def main():
parser = argparse.ArgumentParser()
parser.add_argument('-p','--prefix',
Expand Down Expand Up @@ -346,15 +367,13 @@ def pkgs_with_cycles(graph):
print(f"warning: package \"{args.package}\" not found", file=sys.stderr)
sys.exit(1)
elif args.dot:
fn = networkx.descendants if state["down_search"] else networkx.ancestors
e = list(fn(g, args.package))
e = find_reachable_pkgs(g, args.package, exclude_pkgs=set(args.exclude), down_search=state["down_search"])
print_graph_dot(g.subgraph(e+[args.package]))
elif args.tree:
tree, state = print_dep_tree(g, args.package, None, state)
print(tree, end='')
elif args.recursive:
fn = networkx.descendants if state["down_search"] else networkx.ancestors
e = list(fn(g, args.package))
e = find_reachable_pkgs(g, args.package, exclude_pkgs=set(args.exclude), down_search=state["down_search"])
print_pkgs(e, with_json=args.json)
else:
edges = g.out_edges(args.package) if state["down_search"] else g.in_edges(args.package)
Expand All @@ -375,7 +394,7 @@ def pkgs_with_cycles(graph):

elif args.subcmd == 'deptree':
if args.dot:
print_graph_dot(g)
print_graph_dot(g, exclude_pkgs=set(args.exclude))
elif args.json:
print_pkgs(list(g), with_json=True)
else:
Expand Down
14 changes: 10 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
from setuptools import setup
import shutil


# we need to rename the script because it's not a valid module name
shutil.copyfile('conda-tree.py', 'conda_tree.py')

setup(name='conda-tree',
version='1.0.5',
pname = 'conda-tree'

exec(list(filter(
lambda l: l.startswith("__version__"),
open(pname+'.py').read().split("\n")
))[0])

setup(name=pname,
version=__version__,
description='conda dependency tree helper',
author='Renan Valieris',
url='https://github.com/rvalieris/conda-tree',
url='https://github.com/conda-incubator/conda-tree',
py_modules=['conda_tree'],
install_requires=[
'networkx',
Expand Down

0 comments on commit 3d82204

Please sign in to comment.