source: trunk/game/utilities/parpg_check.py @ 349

Revision 349, 3.6 KB checked in by eliedebrauwer, 10 years ago (diff)

Ticket #131. Fix by octavio/eliedebrauwer. Adding octavios frontend script to pylint, with some small default path/helpstring/parpg license/... modifications, requires pylint to be installed. fixes[s:trac, t:131]

  • Property svn:executable set to *
Line 
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
16helpstring = """
17Runs pylint on all python files found in a directory and
18its subdirectories. Prints messages that match
19the type of checks we wish to see.
20
21Usage:
22        parpg-check.py [--check check_type] [/path/to/parpg]
23
24check_types:
25        errors
26        warnings
27        conventions
28        refactors
29        all
30
31If no check_type is given it defaults to showing only possible 'errors'.
32If not path is given it defaults to the current parent directory.
33"""
34
35import os
36import sys
37import re
38import imp
39from optparse import OptionParser
40
41
42def 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
52def 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
61def get_svn_revision(svn_dir):
62    """ Returns the current svn revision for the directory we are checking. """
63    try:
64        pout = os.popen("svn info %s" % svn_dir)
65        svn_rev =  [line for line in pout if re.match('^Revision', line)]
66    except:
67        pass
68    else:
69        return svn_rev[0]
70
71
72def main():
73    # Check if pylint is installed
74    try:
75        imp.find_module('pylint')
76    except:
77        sys.exit("This script requires pylint! Exiting...")
78
79    version = 'Version: %prog 0.1'
80    parser = OptionParser(usage=helpstring, version=version)
81    parser.add_option('-c', '--check', action='store', \
82            type='string', dest='check_type',
83            help='types of code checks to show',
84            default='errors')
85    opts, args = parser.parse_args()
86
87    # Setup the base directory where our script will start
88    try:
89        base_directory = args[0]
90    except IndexError:
91        print "No directory specified, defaulting to parent"
92        base_directory = os.path.abspath("..")
93
94    # Setup the type of checks we want
95    rgx_map = {"warnings": "^W:",
96                "errors":   "^E:",
97                "pep8": "^C:",
98                "refactors": "^R:",
99                "all": "^[WECRF]:"}
100    chk_type = opts.check_type
101
102    # Print the svn revision we are checking
103    try:
104        revision = get_svn_revision(base_directory)
105    except IndexError:
106        print "***** Unable to determine SVN Revision Number\n"
107    else:
108        print "***** Checking %s for %s.\n" % (revision.rstrip('\n'), chk_type)
109
110    # Start crawling for 'py' files
111    for root, dirs, files in os.walk(base_directory):
112        for name in files:
113            if os.path.splitext(name)[1] == '.py':
114                filepath = os.path.join(root, name)
115                check(filepath, rgx_map[chk_type])
116
117if __name__ == "__main__":
118    main()
Note: See TracBrowser for help on using the repository browser.