Skip to content

Instantly share code, notes, and snippets.

@auxiliary
Last active June 21, 2018 15:51
Show Gist options
  • Save auxiliary/1ccd52b59aca747fba5d72cc9d0b535f to your computer and use it in GitHub Desktop.
Save auxiliary/1ccd52b59aca747fba5d72cc9d0b535f to your computer and use it in GitHub Desktop.
Get a dependency graph of the scripts in a directory
#!/usr/bin/env python3
import os
import sys
import networkx as nx
from networkx.drawing.nx_pydot import write_dot
filename = sys.argv[1] if len(sys.argv) > 1 else "output.dot"
FILE_SIZE_LIMIT = 5 * 1024 * 1024 # bytes
files = os.listdir(".")
filtered = [x for x in files if os.stat(x).st_size < FILE_SIZE_LIMIT \
and os.path.isfile(x)]
filtered = [x for x in filtered if x != filename and x != __file__.replace("./", "")]
G = nx.DiGraph()
for i in filtered:
G.add_node(i)
for target in filtered:
with open(target) as fp:
try:
txt = fp.read()
except:
continue
for needle in filtered:
if needle in txt:
G.add_edge(target, needle) # target file uses needle file
to_remove = []
for key, val in G.degree:
if val == 0:
to_remove.append(key)
for x in to_remove:
G.remove_node(x)
write_dot(G, filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment