Created
January 6, 2011 23:03
-
-
Save pburleson/768794 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
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) | |
s += "\t\"%s\" -> {%s};\n" % (k, deps) | |
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