Package IDAscope :: Module recursive_pylint
[hide private]
[frames] | no frames]

Source Code for Module IDAscope.recursive_pylint

  1  #! /usr/bin/env python 
  2  ''' 
  3  this module runs pylint on all python scripts found in a directory tree 
  4  ''' 
  5   
  6  import os 
  7  import re 
  8  import sys 
  9   
 10  total = 0.0 
 11  count = 0 
 12   
 13  num_code_lines = 0 
 14  num_comment_lines = 0 
 15  num_blank_lines = 0 
 16  num_all_lines = 0 
 17   
 18  EXCLUDED_FILENAMES = ["PatternManager.py"] 
 19   
20 -def check(module, full_comment=False):
21 ''' 22 apply pylint to the file specified if it is a *.py file 23 ''' 24 global total, count 25 26 if module[-3:] == ".py": 27 28 f_pyfile = open(module, "r") 29 pyfile_lines = f_pyfile.readlines() 30 is_continued_comment = False 31 for line in pyfile_lines: 32 global num_code_lines 33 global num_comment_lines 34 global num_blank_lines 35 global num_all_lines 36 stripped = line.strip() 37 if len(stripped) == 0: 38 num_blank_lines += 1 39 elif is_continued_comment: 40 if "\"\"\"" in stripped: 41 is_continued_comment = False 42 num_comment_lines += 1 43 elif "\"\"\"" in stripped: 44 is_continued_comment = True 45 num_comment_lines += 1 46 elif stripped[0] == "#": 47 num_comment_lines += 1 48 else: 49 num_code_lines += 1 50 num_all_lines += 1 51 # we don't check empty files 52 if len(pyfile_lines) == 0: 53 return 54 # we don't check idascope.core.PatternManager because it contains huge lines (signatures) that distort the rating 55 if module in EXCLUDED_FILENAMES: 56 return 57 print "CHECKING ", module 58 # E1101 - %s %r has no %r member - will bug around a lot about Qt widgets 59 # W0201 - Attribute %r defined outside __init__ - ignored because IMHO code is better organized with the setup methods 60 # F0401 - Unable to import %r (%s) - we are evaluating code for Windows on another machine so this is not taken into concern 61 # R0903 - Too few public methods (%s/%s) - we don't care about that. 62 # we use line length: 120 63 pout = os.popen('pylint --method-rgx=\'[a-z_][a-zA-Z0-9_]{2,30}$\' --disable=E1101,W0201,F0401,R0903 --max-line-length=120 %s'% module, 'r') 64 for line in pout: 65 if full_comment: 66 sys.stdout.write(line) 67 sys.stdout.flush() 68 if re.match("E....:.", line): 69 if not full_comment: 70 print line 71 if "Your code has been rated at" in line: 72 if not full_comment: 73 print line 74 score = re.findall("\d.\d\d", line)[0] 75 total += float(score) 76 count += 1
77
78 -def kill_pyc(filepath):
79 if filepath[-4:] == ".pyc": 80 os.remove(filepath)
81
82 -def walk_files(base_directory):
83 print "looking for *.py scripts in subdirectories of ", base_directory 84 for root, dirs, files in os.walk(base_directory): 85 for name in files: 86 filepath = os.path.join(root, name) 87 check(filepath) 88 kill_pyc(filepath)
89 90 if __name__ == "__main__": 91 try: 92 print sys.argv 93 base_directory = sys.argv[1] 94 if os.path.isdir(base_directory): 95 walk_files(base_directory) 96 else: 97 check(base_directory, True) 98 except IndexError: 99 print "no directory specified, defaulting to current working directory" 100 base_directory = os.getcwd() 101 walk_files(base_directory) 102 103 print "==" * 50 104 print "%d modules rated"% count 105 print "AVERAGE SCORE = %.02f"% (total / count) 106 print "==" * 50 107 print "in total %d lines: %d code (%2.2f %%), %d comments (%2.2f %%) %d blank (%2.2f %%)" % \ 108 (num_all_lines, 109 num_code_lines, (100.0 * num_code_lines / num_all_lines), 110 num_comment_lines, (100.0 * num_comment_lines / num_all_lines), 111 num_blank_lines, (100.0 * num_blank_lines / num_all_lines)) 112