Last active
June 28, 2018 17:48
-
-
Save robertmaxwilliams/b688d4169adec7c30183ae7b780c97a9 to your computer and use it in GitHub Desktop.
makes dot file from project dir
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 os | |
import os.path as p | |
import random | |
import sys | |
# TODO: DO TO | |
PACKAGE_NAME = "mastml" #TODO | |
#pure_print = print | |
#def make_myprint(leader): | |
# def myprint(*args): | |
# pure_print(leader, *args) | |
# return myprint | |
def un(): | |
return str(random.randint(0, 1000)) | |
def extract_imports(node_name, filename, root): | |
for i, line in enumerate(import_lines(p.join(root, filename))): | |
if '"' in line or "'" in line: continue # skip quoted lines | |
original = line | |
if '#' in line: | |
line = line[:line.find('#')] | |
line = line.replace(' . ', ' mastml ') | |
line = line.replace(' .', ' mastml.') | |
line = line.strip() | |
line = line.replace('.', '__') | |
line = line.replace(', ', ',') | |
line = line.split(' ') | |
yield f'{node_name} [style=filled, fillcolor=red]' | |
# import X | |
if len(line) == 2 and line[0] == "import": | |
_, X = line | |
if 'mastml' in X: | |
yield f'{node_name} -> {X} [label="imports"]' | |
yield f'{X} [style=filled, fillcolor=orange]' | |
else: | |
yield f'{node_name} -> {X+un()} [label="imports"]' | |
# import X as Y | |
elif len(line) == 4 and line[0] == "import" and line[2] == "as": | |
_, X, _, Y = line | |
if 'mastml' in X: | |
yield f'{node_name} -> {X} [label="imports"]' | |
yield f'{X} -> {Y} [label="as" color=yellow]' | |
yield f'{X} [style=filled, fillcolor=orange]' | |
else: | |
uni = un() | |
yield f'{node_name} -> {X+uni} [label="imports"]' | |
yield f'{X+uni} -> {Y+un()} [label="as" color=yellow]' | |
# from X import * | |
elif len(line) == 4 and line[0] == "from" and line[2] == "import" and line[3] == "*": | |
_, X, _, _ = line | |
if 'mastml' in X: | |
yield f'{node_name} -> {X} [label="imports all", color=red]' | |
yield f'{X} [style=filled, fillcolor=orange]' | |
else: | |
yield f'{node_name} -> {X+un()} [label="imports all"]' | |
# from X import a,b,c | |
elif len(line) == 4 and line[0] == "from" and line[2] == "import": | |
_, X, _, subs = line | |
for sub in subs.split(','): | |
sub = X + '__' + sub | |
if 'mastml' in X: | |
yield f'{node_name} -> {sub} [label="imports"]' | |
yield f'{sub} [style=filled, fillcolor=orange]' | |
else: | |
yield f'{node_name} -> {sub+un()} [label="imports"]' | |
# from X import Y as name | |
elif len(line) == 6 and line[0] == "from" and line[2] == "import" and line[4] == "as": | |
_, X, _, Y, _, name = line | |
pack = X + '__' + Y | |
if 'mastml' in X: | |
yield f'{node_name} -> {pack} [label="imports"]' | |
yield f'{pack} -> {name} [label="as" color=yellow]' | |
yield f'{pack} [style=filled, fillcolor=orange]' | |
else: | |
uni = un() | |
yield f'{node_name} -> {pack+uni} [label="imports"]' | |
yield f'{pack+uni} -> {name+un()} [label="as" color=yellow]' | |
# (empty line) | |
elif line == ['']: | |
continue | |
else: | |
print('\noops:') | |
print(original) | |
print('\t\t' + str(line)) | |
print() | |
def import_lines(filename): | |
with open(filename, 'r') as f: | |
for line in f.readlines(): | |
if 'import' in line: | |
yield line | |
graph = dict() | |
print('digraph graphname {') | |
all_edges = set() | |
for root, dirs, files in os.walk('./'): | |
for f in files: | |
name, ext = p.splitext(f) | |
if ext == '.py' and 'init' not in name: | |
node_name = 'mastml' + p.join(root, name).replace(os.sep, '__')[1:] | |
#sys.stderr.write(node_name + ' \n') | |
print(f'subgraph {node_name}' ' {') | |
print(f'label ="{node_name}"') | |
#all_edges.update(extract_imports(node_name, f, root)) | |
[print(x) for x in extract_imports(node_name, f, root)] | |
print('}') | |
print() | |
for edge in all_edges: | |
print(edge) | |
print('}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment