8
def pretty_box(string):
9
print "##"+"#"*(len(string))+"##"
11
print "##"+"#"*(len(string))+"##"
13
def get_module(file_name):
14
# if it's a directory return all modules and submodules recursively
15
if os.path.isdir(file_name):
16
files = dircache.listdir(file_name)
17
return [get_module(one_file) for one_file in files]
19
if ".py" in file_name:
20
exploded_path = file_name.split(".py")[0].split(os.sep)
21
module_name = exploded_path[-1]
22
package = os.sep.join(exploded_path[:-1])
24
## sys.path.append(package)
25
## print "added %s to the path -filename was %s- " % (package,file_name)
26
if package not in sys.path:
27
sys.path.append(package)
28
print "added '%s' to the path" % (package)
29
print "importing module",module_name
30
return __import__(module_name)
33
modules_names = [modulename.split(".py")[0] for modulename in sys.argv[1:]]
35
print "usage : %s source1.py [source2.py] ... " % sys.argv[0]
38
modules = [get_module(file_name) for file_name in sys.argv[1:]]
39
functions = map(lambda x:x[1],reduce(lambda x,y:x+y,[inspect.getmembers(module,inspect.isfunction) for module in modules]))
40
classes = map(lambda x:x[1],reduce(lambda x,y:x+y,[inspect.getmembers(module,inspect.isclass) for module in modules],[]))
41
methods = map(lambda x:x[1],reduce(lambda x,y:x+y,[inspect.getmembers(klass,inspect.ismethod) for klass in classes],[]))
44
padding = max([len(function.func_name) for function in methods+functions])
46
pretty_box("Functions and methods lengths (in lines of code)")
47
for method in methods+functions :
48
#lines = inspect.getsourcelines(method)
49
code_length = len(filter(lambda line:line.strip() and line.strip()[0] not in "#",inspect.getsourcelines(method)[0]))
51
if code_length > MAX_LENGTH :
52
print "warning : method %s has %s lines" % (method.func_name, code_length)
55
print method.func_name.ljust(padding),":",code_length
57
print "All your code should be readable. -max length is configured to %s-" % MAX_LENGTH
59
print "your app has %s lines of code, %s functions and methods in %s classes across %s modules" % (loc,len(methods)+len(functions),len(classes),len(modules))
62
if __name__ == "__main__":