| 1 |
from __future__ import division |
| 2 |
import py |
| 3 |
|
| 4 |
import random |
| 5 |
|
| 6 |
exclude_files = ["__init__.py", "conftest.py"] |
| 7 |
|
| 8 |
def include_file(path): |
| 9 |
if ("test" in str(path) or "tool" in str(path) or |
| 10 |
"documentation" in str(path) or |
| 11 |
"_cache" in str(path)): |
| 12 |
return False |
| 13 |
if path.basename in exclude_files: |
| 14 |
return False |
| 15 |
return True |
| 16 |
|
| 17 |
def get_mod_from_path(path): |
| 18 |
dirs = path.get("dirname")[0].split("/") |
| 19 |
pypyindex = dirs.index("pypy") |
| 20 |
return ".".join(dirs[pypyindex:] + path.get("purebasename")) |
| 21 |
|
| 22 |
|
| 23 |
def find_references(path): |
| 24 |
refs = [] |
| 25 |
for line in path.open("r"): |
| 26 |
if line.startswith(" "): |
| 27 |
continue |
| 28 |
if "\\" in line: |
| 29 |
continue |
| 30 |
line = line.strip() |
| 31 |
line = line.split("#")[0].strip() |
| 32 |
if line.startswith("import pypy."): |
| 33 |
if " as " not in line: |
| 34 |
refs.append((line[7:].strip(), None)) |
| 35 |
else: |
| 36 |
assert line.count(" as ") == 1 |
| 37 |
line = line.split(" as ") |
| 38 |
refs.append((line[0][7:].strip(), line[1].strip())) |
| 39 |
elif line.startswith("from ") and "pypy" in line: |
| 40 |
line = line[5:] |
| 41 |
if " as " not in line: |
| 42 |
line = line.split(" import ") |
| 43 |
what = line[1].split(",") |
| 44 |
for w in what: |
| 45 |
refs.append((line[0].strip() + "." + w.strip(), None)) |
| 46 |
else: |
| 47 |
if line.count(" as ") != 1 or "," in line: |
| 48 |
print"can't handle this: " + line |
| 49 |
continue |
| 50 |
line = line.split(" as ") |
| 51 |
what = line[0].replace(" import ", ".").replace(" ", "") |
| 52 |
refs.append((what, line[1].strip())) |
| 53 |
return refs |
| 54 |
|
| 55 |
def get_module(ref, imports): |
| 56 |
ref = ref.split(".") |
| 57 |
i = len(ref) |
| 58 |
while i: |
| 59 |
possible_mod = ".".join(ref[:i]) |
| 60 |
if possible_mod in imports: |
| 61 |
return possible_mod |
| 62 |
i -= 1 |
| 63 |
return None |
| 64 |
|
| 65 |
def casteljeau(points, t): |
| 66 |
points = points[:] |
| 67 |
while len(points) > 1: |
| 68 |
for i in range(len(points) - 1): |
| 69 |
points[i] = points[i] * (1 - t) + points[i + 1] * t |
| 70 |
del points[-1] |
| 71 |
return points[0] |
| 72 |
|
| 73 |
def color(t): |
| 74 |
casteljeau([0, 0, 1, 0, 0], t) / 0.375 |
| 75 |
|
| 76 |
class ModuleGraph(object): |
| 77 |
def __init__(self, path): |
| 78 |
self.imports = {} |
| 79 |
self.clusters = {} |
| 80 |
self.mod_to_cluster = {} |
| 81 |
for f in path.visit("*.py"): |
| 82 |
if include_file(f): |
| 83 |
self.imports[get_mod_from_path(f)] = find_references(f) |
| 84 |
self.remove_object_refs() |
| 85 |
self.remove_double_refs() |
| 86 |
self.incoming = {} |
| 87 |
for mod in self.imports: |
| 88 |
self.incoming[mod] = set() |
| 89 |
for mod, refs in self.imports.iteritems(): |
| 90 |
for ref in refs: |
| 91 |
if ref[0] in self.incoming: |
| 92 |
self.incoming[ref[0]].add(mod) |
| 93 |
self.remove_single_nodes() |
| 94 |
self.topgraph_properties = ["rankdir=LR"] |
| 95 |
|
| 96 |
def remove_object_refs(self): |
| 97 |
|
| 98 |
|
| 99 |
for mod, refs in self.imports.iteritems(): |
| 100 |
i = 0 |
| 101 |
while i < len(refs): |
| 102 |
if refs[i][0] in self.imports: |
| 103 |
i += 1 |
| 104 |
else: |
| 105 |
nref = get_module(refs[i][0], self.imports) |
| 106 |
if nref is None: |
| 107 |
print "removing", repr(refs[i]) |
| 108 |
del refs[i] |
| 109 |
else: |
| 110 |
refs[i] = (nref, None) |
| 111 |
i += 1 |
| 112 |
|
| 113 |
def remove_double_refs(self): |
| 114 |
|
| 115 |
for mod, refs in self.imports.iteritems(): |
| 116 |
i = 0 |
| 117 |
seen_refs = set() |
| 118 |
while i < len(refs): |
| 119 |
if refs[i] not in seen_refs: |
| 120 |
seen_refs.add(refs[i]) |
| 121 |
i += 1 |
| 122 |
else: |
| 123 |
del refs[i] |
| 124 |
|
| 125 |
def remove_single_nodes(self): |
| 126 |
|
| 127 |
rem = [] |
| 128 |
for mod, refs in self.imports.iteritems(): |
| 129 |
if len(refs) == 0 and len(self.incoming[mod]) == 0: |
| 130 |
rem.append(mod) |
| 131 |
for m in rem: |
| 132 |
del self.incoming[m] |
| 133 |
del self.imports[m] |
| 134 |
|
| 135 |
def create_clusters(self): |
| 136 |
self.topgraph_properties.append("compound=true;") |
| 137 |
self.clustered = True |
| 138 |
hierarchy = [set() for i in range(6)] |
| 139 |
for mod in self.imports: |
| 140 |
for i, d in enumerate(mod.split(".")): |
| 141 |
hierarchy[i].add(d) |
| 142 |
for i in range(6): |
| 143 |
if len(hierarchy[i]) != 1: |
| 144 |
break |
| 145 |
for mod in self.imports: |
| 146 |
cluster = mod.split(".")[i] |
| 147 |
if i == len(mod.split(".")) - 1: |
| 148 |
continue |
| 149 |
if cluster not in self.clusters: |
| 150 |
self.clusters[cluster] = set() |
| 151 |
self.clusters[cluster].add(mod) |
| 152 |
self.mod_to_cluster[mod] = cluster |
| 153 |
|
| 154 |
def remove_tangling_randomly(self): |
| 155 |
|
| 156 |
tangled = [] |
| 157 |
for mod, incoming in self.incoming.iteritems(): |
| 158 |
if len(incoming) > 10: |
| 159 |
tangled.append(mod) |
| 160 |
for mod in tangled: |
| 161 |
remove = set() |
| 162 |
incoming = self.incoming[mod] |
| 163 |
while len(remove) < len(incoming) * 0.80: |
| 164 |
remove.add(random.choice(list(incoming))) |
| 165 |
for rem in remove: |
| 166 |
for i in range(len(self.imports[rem])): |
| 167 |
if self.imports[rem][i][1] == mod: |
| 168 |
break |
| 169 |
del self.imports[rem][i] |
| 170 |
incoming.remove(rem) |
| 171 |
print "removing", mod, "<-", rem |
| 172 |
self.remove_single_nodes() |
| 173 |
|
| 174 |
def dotfile(self, dot): |
| 175 |
f = dot.open("w") |
| 176 |
f.write("digraph G {\n") |
| 177 |
for prop in self.topgraph_properties: |
| 178 |
f.write("\t%s\n" % prop) |
| 179 |
|
| 180 |
for cluster, nodes in self.clusters.iteritems(): |
| 181 |
f.write("\tsubgraph cluster_%s {\n" % cluster) |
| 182 |
f.write("\t\tstyle=filled;\n\t\tcolor=lightgrey\n") |
| 183 |
for node in nodes: |
| 184 |
f.write('\t\t"%s";\n' % node[5:]) |
| 185 |
for mod, refs in self.imports.iteritems(): |
| 186 |
for ref in refs: |
| 187 |
if mod in nodes and ref[0] in nodes: |
| 188 |
f.write('\t\t"%s" -> "%s";\n' % (mod[5:], ref[0][5:])) |
| 189 |
f.write("\t}\n") |
| 190 |
|
| 191 |
for mod, refs in self.imports.iteritems(): |
| 192 |
try: |
| 193 |
nodes = self.clusters[self.mod_to_cluster[mod]] |
| 194 |
except KeyError: |
| 195 |
nodes = set() |
| 196 |
for ref in refs: |
| 197 |
if ref[0] not in nodes: |
| 198 |
f.write('\t"%s" -> "%s";\n' % (mod[5:], ref[0][5:])) |
| 199 |
f.write("}") |
| 200 |
f.close() |
| 201 |
|
| 202 |
if __name__ == "__main__": |
| 203 |
import sys |
| 204 |
if len(sys.argv) > 1: |
| 205 |
path = py.path.local(sys.argv[1]) |
| 206 |
else: |
| 207 |
path = py.path.local(".") |
| 208 |
gr = ModuleGraph(path) |
| 209 |
gr.create_clusters() |
| 210 |
dot = path.join("import_graph.dot") |
| 211 |
gr.dotfile(dot) |