1 | #!/usr/bin/python |
---|
2 | |
---|
3 | # This program is free software: you can redistribute it and/or modify |
---|
4 | # it under the terms of the GNU General Public License as published by |
---|
5 | # the Free Software Foundation, either version 3 of the License, or |
---|
6 | # (at your option) any later version. |
---|
7 | |
---|
8 | # This program is distributed in the hope that it will be useful, |
---|
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
11 | # GNU General Public License for more details. |
---|
12 | |
---|
13 | # You should have received a copy of the GNU General Public License |
---|
14 | # along with this program. If not, see <http://www.gnu.org/licenses/> |
---|
15 | |
---|
16 | helpstring = """ |
---|
17 | Runs pylint on all python files found in a directory and |
---|
18 | its subdirectories. Prints messages that match |
---|
19 | the type of checks we wish to see. |
---|
20 | |
---|
21 | Usage: |
---|
22 | parpg-check.py [--check check_type] [/path/to/parpg] |
---|
23 | |
---|
24 | check_types: |
---|
25 | errors |
---|
26 | warnings |
---|
27 | conventions |
---|
28 | refactors |
---|
29 | all |
---|
30 | |
---|
31 | If no check_type is given it defaults to showing only possible 'errors'. |
---|
32 | If not path is given it defaults to the current parent directory. |
---|
33 | """ |
---|
34 | |
---|
35 | import os |
---|
36 | import sys |
---|
37 | import re |
---|
38 | import imp |
---|
39 | from optparse import OptionParser |
---|
40 | |
---|
41 | |
---|
42 | def check(module, regx): |
---|
43 | """ Applies pylint to the file specified and displays only the |
---|
44 | messages that match our type of code check. |
---|
45 | """ |
---|
46 | pout = os.popen('pylint --reports=no %s 2> %s' % (module, os.devnull), 'r') |
---|
47 | errs = [line for line in pout if re.match(regx, line)] |
---|
48 | if errs: |
---|
49 | print_matches(module, errs) |
---|
50 | |
---|
51 | |
---|
52 | def print_matches(mod, matches): |
---|
53 | """ Prints the matching messages for our type of |
---|
54 | code check. |
---|
55 | """ |
---|
56 | print "Found %s errors in %s :" % (len(matches), mod) |
---|
57 | for match in matches: |
---|
58 | print "\t %s" % match.rstrip('\n') |
---|
59 | print |
---|
60 | |
---|
61 | def main(): |
---|
62 | # Check if pylint is installed |
---|
63 | try: |
---|
64 | imp.find_module('pylint') |
---|
65 | except: |
---|
66 | sys.exit("This script requires pylint! Exiting...") |
---|
67 | |
---|
68 | version = 'Version: %prog 0.1' |
---|
69 | parser = OptionParser(usage=helpstring, version=version) |
---|
70 | parser.add_option('-c', '--check', action='store', \ |
---|
71 | type='string', dest='check_type', |
---|
72 | help='types of code checks to show', |
---|
73 | default='errors') |
---|
74 | opts, args = parser.parse_args() |
---|
75 | |
---|
76 | # Setup the base directory where our script will start |
---|
77 | try: |
---|
78 | base_directory = args[0] |
---|
79 | except IndexError: |
---|
80 | print "No directory specified, defaulting to parent" |
---|
81 | base_directory = os.path.abspath("..") |
---|
82 | |
---|
83 | # Setup the type of checks we want |
---|
84 | rgx_map = {"warnings": "^W:", |
---|
85 | "errors": "^E:", |
---|
86 | "pep8": "^C:", |
---|
87 | "refactors": "^R:", |
---|
88 | "all": "^[WECRF]:"} |
---|
89 | chk_type = opts.check_type |
---|
90 | |
---|
91 | print "***** Checking %s for %s.\n" % (os.path.abspath(base_directory), chk_type) |
---|
92 | |
---|
93 | # Start crawling for 'py' files |
---|
94 | for root, dirs, files in os.walk(base_directory): |
---|
95 | for name in files: |
---|
96 | if os.path.splitext(name)[1] == '.py': |
---|
97 | filepath = os.path.join(root, name) |
---|
98 | check(filepath, rgx_map[chk_type]) |
---|
99 | |
---|
100 | if __name__ == "__main__": |
---|
101 | main() |
---|