1
# DistUtilsExtra.command.check - check command for DistUtilsExtra
3
# Author: Rodney Dawes <rodney.dawes@canonical.com>
4
# Copyright 2009 Canonical Ltd.
6
"""DistUtilsExtra.command.check
8
Implements the DistUtilsExtra 'check' command.
14
from distutils.core import Command
17
class check (Command):
18
"""Command to run lint and tests on a module."""
20
description = "integrate pylint checks"
22
user_options = [("config-file=", None,
23
"pylint config file to use"),
24
("exclude-files=", None,
25
"list of files to exclude from lint checks"),
27
"list of modules or packages to run lint checks on")
30
def initialize_options (self):
31
self.config_file = None
32
self.exclude_files = None
33
self.lint_files = None
35
def finalize_options (self):
36
if self.config_file is None:
38
if self.exclude_files is None:
39
self.exclude_files = "[]"
40
if self.lint_files is None:
41
self.lint_files = "[" + self.__find_files() + "]"
44
pylint_args = ["--output-format=parseable",
48
pylint_args.append("--rcfile=" + self.config_file)
50
for file in eval(self.lint_files):
51
pylint_args.append(file)
53
p = subprocess.Popen(["pylint"] + pylint_args,
54
bufsize=4096, stdout=subprocess.PIPE)
57
output = "".join(notices.readlines())
59
print "== Pylint notices =="
60
print self.__group_lines_by_file(output)
62
def __group_lines_by_file(self, input):
63
"""Format file:line:message output as lines grouped by file."""
66
excludes = eval(self.exclude_files)
67
for line in input.splitlines():
68
current = line.split(":", 3)
69
if line.startswith(" "):
70
outputs.append(" " + current[0] + "")
71
elif line.startswith("build/") or current[0] in excludes or \
74
elif filename == current[0]:
75
outputs.append(" " + current[1] + ": " + current[2])
76
elif filename != current[0]:
79
outputs.append(filename + ":")
80
outputs.append(" " + current[1] + ": " + current[2])
82
return "\n".join(outputs)
84
def __find_files(self):
85
"""Find all Python files under the current tree."""
87
for root, dirs, files in os.walk(os.getcwd(), topdown=False):
89
if file.endswith(".py"):
90
pyfiles.append("'" + os.path.join(root, file) + "'")
92
return ",".join(pyfiles)