1
# DistUtilsExtra.command.check - check command for DistUtilsExtra
3
# Author: Rodney Dawes <rodney.dawes@canonical.com>
5
# Copyright 2009 Canonical Ltd.
7
# This program is free software: you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License version 3, as published
9
# by the Free Software Foundation.
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranties of
13
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
14
# PURPOSE. See the GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License along
17
# with this program. If not, see <http://www.gnu.org/licenses/>.
19
"""DistUtilsExtra.command.check
21
Implements the DistUtilsExtra 'check' command.
27
from distutils.core import Command
30
class check (Command):
31
"""Command to run lint and tests on a module."""
33
description = "integrate pylint checks"
35
user_options = [("config-file=", None,
36
"pylint config file to use"),
37
("exclude-files=", None,
38
"list of files to exclude from lint checks"),
40
"list of modules or packages to run lint checks on")
43
def initialize_options (self):
44
self.config_file = None
45
self.exclude_files = None
46
self.lint_files = None
48
def finalize_options (self):
49
if self.config_file is None:
51
if self.exclude_files is None:
52
self.exclude_files = "[]"
53
if self.lint_files is None:
54
self.lint_files = "[" + self.__find_files() + "]"
57
pylint_args = ["--output-format=parseable",
61
pylint_args.append("--rcfile=" + self.config_file)
63
for file in eval(self.lint_files):
64
pylint_args.append(file)
66
p = subprocess.Popen(["pylint"] + pylint_args,
67
bufsize=4096, stdout=subprocess.PIPE)
70
output = "".join(notices.readlines())
72
print "== Pylint notices =="
73
print self.__group_lines_by_file(output)
75
def __group_lines_by_file(self, input):
76
"""Format file:line:message output as lines grouped by file."""
79
excludes = eval(self.exclude_files)
80
for line in input.splitlines():
81
current = line.split(":", 3)
82
if line.startswith(" "):
83
outputs.append(" " + current[0] + "")
84
elif line.startswith("build/") or current[0] in excludes or \
87
elif filename == current[0]:
88
outputs.append(" " + current[1] + ": " + current[2])
89
elif filename != current[0]:
92
outputs.append(filename + ":")
93
outputs.append(" " + current[1] + ": " + current[2])
95
return "\n".join(outputs)
97
def __find_files(self):
98
"""Find all Python files under the current tree."""
100
for root, dirs, files in os.walk(os.getcwd(), topdown=False):
102
if file.endswith(".py"):
103
pyfiles.append("'" + os.path.join(root, file) + "'")
105
return ",".join(pyfiles)