~yacinechaouche/+junk/BZR

« back to all changes in this revision

Viewing changes to PROG/TAHAR/ARCHIVES/naive_tahar_r5.py

  • Committer: yacinechaouche at yahoo
  • Date: 2015-01-14 22:23:03 UTC
  • Revision ID: yacinechaouche@yahoo.com-20150114222303-6gbtqqxii717vyka
Ajout de CODE et PROD. Il faudra ensuite ajouter ce qu'il y avait dan TMP

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import inspect
 
2
import sys
 
3
import os
 
4
import dircache
 
5
 
 
6
MAX_LENGTH = 25
 
7
 
 
8
def pretty_box(string):
 
9
    print "##"+"#"*(len(string))+"##"
 
10
    print "#",string,"#"
 
11
    print "##"+"#"*(len(string))+"##"
 
12
    
 
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]
 
18
    
 
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])
 
23
        ## if package:
 
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)
 
31
 
 
32
def main():
 
33
    modules_names = [modulename.split(".py")[0] for modulename in sys.argv[1:]]
 
34
    if not modules_names:
 
35
        print "usage : %s source1.py [source2.py] ... " % sys.argv[0]
 
36
        exit(0)
 
37
 
 
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],[]))
 
42
    loc           = 0
 
43
    all_ok        = True
 
44
    padding       = max([len(function.func_name) for function in methods+functions])
 
45
 
 
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]))
 
50
        loc += code_length
 
51
        if code_length > MAX_LENGTH :
 
52
            print "warning : method %s has %s lines" % (method.func_name, code_length)
 
53
            all_ok = False
 
54
        else:
 
55
            print method.func_name.ljust(padding),":",code_length
 
56
    if all_ok :
 
57
        print "All your code should be readable. -max length is configured to %s-" % MAX_LENGTH
 
58
 
 
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))
 
60
 
 
61
    
 
62
if __name__ == "__main__":
 
63
    main()
 
64