2017-08-19 02:21:11 +02:00
|
|
|
"""Sanity test for PEP 8 style guidelines using pycodestyle."""
|
2019-07-12 08:46:20 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2017-08-19 02:21:11 +02:00
|
|
|
|
|
|
|
import os
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .. import types as t
|
2019-07-26 08:29:23 +02:00
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..sanity import (
|
2017-08-19 02:21:11 +02:00
|
|
|
SanitySingleVersion,
|
|
|
|
SanityMessage,
|
|
|
|
SanityFailure,
|
|
|
|
SanitySuccess,
|
2019-08-06 02:40:00 +02:00
|
|
|
SANITY_ROOT,
|
2017-08-19 02:21:11 +02:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..target import (
|
2019-07-30 20:10:55 +02:00
|
|
|
TestTarget,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..util import (
|
2017-08-19 02:21:11 +02:00
|
|
|
SubprocessError,
|
2018-09-20 08:20:27 +02:00
|
|
|
read_lines_without_comments,
|
2018-09-21 07:20:08 +02:00
|
|
|
parse_to_list_of_dict,
|
2019-07-27 01:46:52 +02:00
|
|
|
find_python,
|
2019-07-30 20:10:55 +02:00
|
|
|
is_subdir,
|
2017-08-19 02:21:11 +02:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..util_common import (
|
2019-07-11 07:00:34 +02:00
|
|
|
run_command,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..config import (
|
2017-08-19 02:21:11 +02:00
|
|
|
SanityConfig,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class Pep8Test(SanitySingleVersion):
|
|
|
|
"""Sanity test for PEP 8 style guidelines using pycodestyle."""
|
2019-07-26 16:53:53 +02:00
|
|
|
@property
|
2019-07-26 08:29:23 +02:00
|
|
|
def error_code(self): # type: () -> t.Optional[str]
|
|
|
|
"""Error code for ansible-test matching the format used by the underlying test program, or None if the program does not use error codes."""
|
|
|
|
return 'A100'
|
|
|
|
|
2019-07-30 20:10:55 +02:00
|
|
|
def filter_targets(self, targets): # type: (t.List[TestTarget]) -> t.List[TestTarget]
|
|
|
|
"""Return the given list of test targets, filtered to include only those relevant for the test."""
|
|
|
|
return [target for target in targets if os.path.splitext(target.path)[1] == '.py' or is_subdir(target.path, 'bin')]
|
|
|
|
|
2019-07-27 01:46:52 +02:00
|
|
|
def test(self, args, targets, python_version):
|
2017-08-19 02:21:11 +02:00
|
|
|
"""
|
|
|
|
:type args: SanityConfig
|
|
|
|
:type targets: SanityTargets
|
2019-07-27 01:46:52 +02:00
|
|
|
:type python_version: str
|
2018-04-04 03:53:53 +02:00
|
|
|
:rtype: TestResult
|
2017-08-19 02:21:11 +02:00
|
|
|
"""
|
2019-08-06 02:40:00 +02:00
|
|
|
current_ignore_file = os.path.join(SANITY_ROOT, 'pep8', 'current-ignore.txt')
|
2018-09-20 08:20:27 +02:00
|
|
|
current_ignore = sorted(read_lines_without_comments(current_ignore_file, remove_blank_lines=True))
|
2017-08-19 02:21:11 +02:00
|
|
|
|
2019-07-26 08:29:23 +02:00
|
|
|
settings = self.load_processor(args)
|
2017-08-19 02:21:11 +02:00
|
|
|
|
2019-07-30 20:10:55 +02:00
|
|
|
paths = [target.path for target in targets.include]
|
2017-08-19 02:21:11 +02:00
|
|
|
|
|
|
|
cmd = [
|
2019-07-27 01:46:52 +02:00
|
|
|
find_python(python_version),
|
2018-03-14 19:35:59 +01:00
|
|
|
'-m', 'pycodestyle',
|
2017-08-19 02:21:11 +02:00
|
|
|
'--max-line-length', '160',
|
|
|
|
'--config', '/dev/null',
|
|
|
|
'--ignore', ','.join(sorted(current_ignore)),
|
|
|
|
] + paths
|
|
|
|
|
|
|
|
if paths:
|
|
|
|
try:
|
|
|
|
stdout, stderr = run_command(args, cmd, capture=True)
|
|
|
|
status = 0
|
|
|
|
except SubprocessError as ex:
|
|
|
|
stdout = ex.stdout
|
|
|
|
stderr = ex.stderr
|
|
|
|
status = ex.status
|
|
|
|
|
|
|
|
if stderr:
|
|
|
|
raise SubprocessError(cmd=cmd, status=status, stderr=stderr, stdout=stdout)
|
|
|
|
else:
|
|
|
|
stdout = None
|
|
|
|
|
|
|
|
if args.explain:
|
|
|
|
return SanitySuccess(self.name)
|
|
|
|
|
|
|
|
if stdout:
|
|
|
|
pattern = '^(?P<path>[^:]*):(?P<line>[0-9]+):(?P<column>[0-9]+): (?P<code>[WE][0-9]{3}) (?P<message>.*)$'
|
|
|
|
|
2018-09-21 07:20:08 +02:00
|
|
|
results = parse_to_list_of_dict(pattern, stdout)
|
2017-08-19 02:21:11 +02:00
|
|
|
else:
|
|
|
|
results = []
|
|
|
|
|
|
|
|
results = [SanityMessage(
|
|
|
|
message=r['message'],
|
|
|
|
path=r['path'],
|
|
|
|
line=int(r['line']),
|
|
|
|
column=int(r['column']),
|
|
|
|
level='warning' if r['code'].startswith('W') else 'error',
|
|
|
|
code=r['code'],
|
|
|
|
) for r in results]
|
|
|
|
|
2019-07-23 22:31:54 +02:00
|
|
|
errors = settings.process_errors(results, paths)
|
2017-08-19 02:21:11 +02:00
|
|
|
|
|
|
|
if errors:
|
|
|
|
return SanityFailure(self.name, messages=errors)
|
|
|
|
|
|
|
|
return SanitySuccess(self.name)
|