Last active
November 15, 2021 17:18
-
-
Save DMRobertson/f34bac20c299a57952ee2e484da20c01 to your computer and use it in GitHub Desktop.
Dirty store inheritance script
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
import sys | |
import re | |
import subprocess | |
import networkx | |
from networkx import is_directed_acyclic_graph, descendants | |
oneline_class_pattern = re.compile("^class (.*)\((.*)\):$") | |
opening_class_pattern = re.compile("^class (.*)\($") | |
lines = subprocess.check_output( | |
[ | |
"rg", | |
"-o", | |
"--no-line-number", | |
"--no-filename", | |
"--multiline", | |
"class .*Store\((.|\n)*?\):$", | |
"synapse", | |
"tests", | |
], | |
cwd="/home/dmr/workspace/synapse/" | |
).decode() | |
def load_graph(): | |
G = networkx.DiGraph() | |
child = None | |
for line in lines.split("\n"): | |
line = line.strip() | |
if not line or line.startswith("#"): | |
continue | |
if (match := oneline_class_pattern.match(line)) is not None: | |
child, parents = match.groups() | |
for parent in parents.split(", "): | |
if "metaclass" not in parent: | |
G.add_edge(child, parent) | |
child = None | |
elif (match := opening_class_pattern.match(line)) is not None: | |
(child,) = match.groups() | |
elif line == "):": | |
child = None | |
else: | |
assert child is not None, repr(line) | |
parent = line.strip(",") | |
if "metaclass" not in parent: | |
G.add_edge(child, parent) | |
return G | |
G = load_graph() | |
assert is_directed_acyclic_graph(G) | |
component: set = descendants(G, sys.argv[1]) | |
component.add(sys.argv[1]) | |
print( | |
"""\ | |
strict digraph { | |
rankdir="LR"; | |
node [shape=box]; | |
""" | |
) | |
for (child, parent) in G.edges: | |
if child in component and parent in component: | |
print(f" {child} -> {parent}") | |
print("}") | |
print("pipe this to 'dot -o /tmp/inheritance.png -Tpng' ", file=sys.stderr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment