8d91a6d72e
* Use correct pip version in ansible-test.
* Add git fallback for validate-modules.
* Run sanity tests in a docker container.
* Use correct python version for sanity tests.
* Pin docker completion images and add default.
* Split pylint execution into multiple contexts.
* Only test .py files in use-argspec-type-path test.
* Accept identical python interpeter name or binary.
* Switch cloud tests to default container.
* Remove unused extras from pip install.
* Filter out empty pip commands.
* Don't force running of pip list.
* Support delegation for windows and network tests.
* Fix ansible-test python version usage.
* Fix ansible-test python version skipping.
* Use absolute path for log in ansible-test.
* Run vyos_command test on python 3.
* Fix windows/network instance persistence.
* Add `test/cache` dir to classification.
* Enable more python versions for network tests.
* Fix cs_router test.
(cherry picked from commit cf1337ca9a
)
181 lines
6.1 KiB
Python
181 lines
6.1 KiB
Python
"""Sanity test for PEP 8 style guidelines using pycodestyle."""
|
|
from __future__ import absolute_import, print_function
|
|
|
|
import os
|
|
import re
|
|
|
|
from lib.sanity import (
|
|
SanitySingleVersion,
|
|
SanityMessage,
|
|
SanityFailure,
|
|
SanitySuccess,
|
|
)
|
|
|
|
from lib.util import (
|
|
SubprocessError,
|
|
display,
|
|
run_command,
|
|
find_executable,
|
|
)
|
|
|
|
from lib.config import (
|
|
SanityConfig,
|
|
)
|
|
|
|
from lib.test import (
|
|
calculate_best_confidence,
|
|
)
|
|
|
|
PEP8_SKIP_PATH = 'test/sanity/pep8/skip.txt'
|
|
PEP8_LEGACY_PATH = 'test/sanity/pep8/legacy-files.txt'
|
|
|
|
|
|
class Pep8Test(SanitySingleVersion):
|
|
"""Sanity test for PEP 8 style guidelines using pycodestyle."""
|
|
def test(self, args, targets):
|
|
"""
|
|
:type args: SanityConfig
|
|
:type targets: SanityTargets
|
|
:rtype: SanityResult
|
|
"""
|
|
with open(PEP8_SKIP_PATH, 'r') as skip_fd:
|
|
skip_paths = skip_fd.read().splitlines()
|
|
|
|
with open(PEP8_LEGACY_PATH, 'r') as legacy_fd:
|
|
legacy_paths = legacy_fd.read().splitlines()
|
|
|
|
with open('test/sanity/pep8/legacy-ignore.txt', 'r') as ignore_fd:
|
|
legacy_ignore = set(ignore_fd.read().splitlines())
|
|
|
|
with open('test/sanity/pep8/current-ignore.txt', 'r') as ignore_fd:
|
|
current_ignore = sorted(ignore_fd.read().splitlines())
|
|
|
|
skip_paths_set = set(skip_paths)
|
|
legacy_paths_set = set(legacy_paths)
|
|
|
|
paths = sorted(i.path for i in targets.include if (os.path.splitext(i.path)[1] == '.py' or i.path.startswith('bin/')) and i.path not in skip_paths_set)
|
|
|
|
cmd = [
|
|
'python%s' % args.python_version,
|
|
find_executable('pycodestyle'),
|
|
'--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>.*)$'
|
|
|
|
results = [re.search(pattern, line).groupdict() for line in stdout.splitlines()]
|
|
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]
|
|
|
|
failed_result_paths = set([result.path for result in results])
|
|
used_paths = set(paths)
|
|
|
|
errors = []
|
|
summary = {}
|
|
|
|
line = 0
|
|
|
|
for path in legacy_paths:
|
|
line += 1
|
|
|
|
if not os.path.exists(path):
|
|
# Keep files out of the list which no longer exist in the repo.
|
|
errors.append(SanityMessage(
|
|
code='A101',
|
|
message='Remove "%s" since it does not exist' % path,
|
|
path=PEP8_LEGACY_PATH,
|
|
line=line,
|
|
column=1,
|
|
confidence=calculate_best_confidence(((PEP8_LEGACY_PATH, line), (path, 0)), args.metadata) if args.metadata.changes else None,
|
|
))
|
|
|
|
if path in used_paths and path not in failed_result_paths:
|
|
# Keep files out of the list which no longer require the relaxed rule set.
|
|
errors.append(SanityMessage(
|
|
code='A201',
|
|
message='Remove "%s" since it passes the current rule set' % path,
|
|
path=PEP8_LEGACY_PATH,
|
|
line=line,
|
|
column=1,
|
|
confidence=calculate_best_confidence(((PEP8_LEGACY_PATH, line), (path, 0)), args.metadata) if args.metadata.changes else None,
|
|
))
|
|
|
|
line = 0
|
|
|
|
for path in skip_paths:
|
|
line += 1
|
|
|
|
if not os.path.exists(path):
|
|
# Keep files out of the list which no longer exist in the repo.
|
|
errors.append(SanityMessage(
|
|
code='A101',
|
|
message='Remove "%s" since it does not exist' % path,
|
|
path=PEP8_SKIP_PATH,
|
|
line=line,
|
|
column=1,
|
|
confidence=calculate_best_confidence(((PEP8_SKIP_PATH, line), (path, 0)), args.metadata) if args.metadata.changes else None,
|
|
))
|
|
|
|
for result in results:
|
|
if result.path in legacy_paths_set and result.code in legacy_ignore:
|
|
# Files on the legacy list are permitted to have errors on the legacy ignore list.
|
|
# However, we want to report on their existence to track progress towards eliminating these exceptions.
|
|
display.info('PEP 8: %s (legacy)' % result, verbosity=3)
|
|
|
|
key = '%s %s' % (result.code, re.sub('[0-9]+', 'NNN', result.message))
|
|
|
|
if key not in summary:
|
|
summary[key] = 0
|
|
|
|
summary[key] += 1
|
|
else:
|
|
# Files not on the legacy list and errors not on the legacy ignore list are PEP 8 policy errors.
|
|
errors.append(result)
|
|
|
|
if summary:
|
|
lines = []
|
|
count = 0
|
|
|
|
for key in sorted(summary):
|
|
count += summary[key]
|
|
lines.append('PEP 8: %5d %s' % (summary[key], key))
|
|
|
|
display.info('PEP 8: There were %d different legacy issues found (%d total):' % (len(summary), count), verbosity=1)
|
|
display.info('PEP 8: Count Code Message', verbosity=1)
|
|
|
|
for line in lines:
|
|
display.info(line, verbosity=1)
|
|
|
|
if errors:
|
|
return SanityFailure(self.name, messages=errors)
|
|
|
|
return SanitySuccess(self.name)
|