~yacinechaouche/+junk/BZR

« back to all changes in this revision

Viewing changes to PROG/TAHAR/ARCHIVES/naive_tahar_r2.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
 
 
5
MAX_LENGTH = 25
 
6
 
 
7
def transform_path(module_name):
 
8
    return module_name.replace(os.sep,'.')
 
9
 
 
10
def getImportArgs(module_name):
 
11
    dotted_path   = transform_path(module_name)
 
12
    exploded_path = dotted_path.split(".") 
 
13
    module_name   = exploded_path[-1]
 
14
    from_list     = exploded_path[:-1]
 
15
    return {"name":dotted_path,"fromlist":from_list}
 
16
 
 
17
def get_module(file_name):
 
18
    module_name = file_name.split(".py")[0]
 
19
    kwargs      = getImportArgs(module_name)
 
20
    #print "__import__(name = \"%(name)s\", fromlist = %(fromlist)s)" % (kwargs)
 
21
    return __import__(kwargs["name"],fromlist=kwargs["fromlist"])
 
22
 
 
23
def main():
 
24
    modules_names = [modulename.split(".py")[0] for modulename in sys.argv[1:]]
 
25
    if not modules_names:
 
26
        print "usage : %s source1.py [source2.py] ... " % sys.argv[0]
 
27
        exit(0)
 
28
    modules       = [get_module(file_name) for file_name in sys.argv[1:]]
 
29
    functions     = map(lambda x:x[1],reduce(lambda x,y:x+y,[inspect.getmembers(module,inspect.isfunction) for module in modules]))
 
30
    classes       = map(lambda x:x[1],reduce(lambda x,y:x+y,[inspect.getmembers(module,inspect.isclass) for module in modules],[]))
 
31
    methods       = map(lambda x:x[1],reduce(lambda x,y:x+y,[inspect.getmembers(klass,inspect.ismethod) for klass in classes],[]))
 
32
    print "your app has %s functions and methods in %s classes across %s modules" % (len(methods)+len(functions),len(classes),len(modules))
 
33
    all_ok = True
 
34
    padding = max([len(function.func_name) for function in methods+functions])
 
35
    for method in methods+functions :
 
36
        lines = inspect.getsourcelines(method)
 
37
        code_length = len(filter(lambda line:line.strip() and line.strip()[0] not in "#",inspect.getsourcelines(method)[0]))
 
38
        if code_length > MAX_LENGTH :
 
39
            print "warning : method %s has %s lines" % (method.func_name, code_length)
 
40
            all_ok = False
 
41
        else:
 
42
            print method.func_name.ljust(padding),":",code_length
 
43
    if all_ok :
 
44
        print "All your code should be readable. -max length configured is %s-" % MAX_LENGTH
 
45
if __name__ == "__main__":
 
46
    main()
 
47