Created
January 6, 2011 22:03
-
-
Save JensAyton/768691 to your computer and use it in GitHub Desktop.
Header dependency mapper for Objective-C. This fork randomly colours arcs, for better tracing of spaghetti.
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
#!/usr/bin/python | |
# Nicolas Seriot | |
# 2011-01-06 | |
# http://github.com/nst/objc_dep | |
""" | |
Input: path of an Objective-C project | |
Output: import dependancies Graphviz format | |
Usage: $ python objc_dep.py /path/to/project > graph.dot | |
The .dot file can be opened with Graphviz or OmniGraffle. | |
""" | |
import sys | |
import os | |
import sets | |
import re | |
import random | |
regex_import = re.compile("#import \"(?P<filename>\S*)\.h") | |
def filenames_imported_in_file(path): | |
imports = set() | |
f = open(path) | |
for line in f.xreadlines(): | |
results = re.search(regex_import, line) | |
if results: | |
filename = results.group('filename') | |
imports.add(filename) | |
f.close() | |
return imports | |
def add_imports_from_files(d, dir, files): | |
for f in files: | |
if f.endswith('.h') or f.endswith('.m'): | |
base_name = f.split('.')[0] | |
if base_name not in d: | |
d[base_name] = set() | |
path = os.path.join(dir, f) | |
imports = filenames_imported_in_file(path) | |
d[base_name] = d[base_name].union(imports) | |
return d | |
def dependancies_in_dot_format(d): | |
s = "digraph G {\n" | |
for k, set in d.iteritems(): | |
if set: | |
set.discard(k) | |
deps = map(lambda s:'"%s"' % s, set) | |
deps = '; '.join(deps) | |
red = random.randrange(0, 200) | |
green = random.randrange(0, 200) | |
blue = random.randrange(0, 200) | |
s += "\t\"%s\" -> {%s} [color=\"#%2X%2X%2X\"]\n" % (k, deps, red, green, blue) | |
s += "}\n" | |
return s | |
def dependancies_in_project(path): | |
d = {} | |
os.path.walk(path, add_imports_from_files, d) | |
return d | |
def main(): | |
d = dependancies_in_project(sys.argv[1]) | |
s = dependancies_in_dot_format(d) | |
print s | |
if __name__=='__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment