Bug 1367092 - [lint] Move py2/py3 linter's exludes logic into mozlint, r=egao

This will be re-used by the flake8 linter, so moving it into mozlint for
re-useability.

Depends on D20493

Differential Revision: https://phabricator.services.mozilla.com/D20494

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Andrew Halberstadt 2019-02-22 21:18:22 +00:00
Родитель 034fd32897
Коммит 2152caf120
2 изменённых файлов: 45 добавлений и 16 удалений

Просмотреть файл

@ -242,3 +242,46 @@ def get_ancestors_by_name(name, path, root):
if path == root:
break
return configs
def expand_exclusions(paths, config, root):
"""Returns all files that match patterns and aren't excluded.
This is used by some external linters who receive 'batch' files (e.g dirs)
but aren't capable of applying their own exclusions. There is an argument
to be made that this step should just apply to all linters no matter what.
Args:
paths (list): List of candidate paths to lint.
config (dict): Linter's config object.
root (str): Root of the repository.
Returns:
Generator which generates list of paths that weren't excluded.
"""
extensions = [e.lstrip('.') for e in config['extensions']]
def normalize(path):
path = mozpath.normpath(path)
if os.path.isabs(path):
return path
return mozpath.join(root, path)
exclude = map(normalize, config.get('exclude', []))
for path in paths:
path = mozpath.normsep(path)
if os.path.isfile(path):
if not any(path.startswith(e) for e in exclude):
yield path
continue
ignore = [e[len(path):].lstrip('/') for e in exclude
if mozpath.commonprefix((path, e)) == path]
finder = FileFinder(path, ignore=ignore)
_, ext = os.path.splitext(path)
ext.lstrip('.')
for ext in extensions:
for p, f in finder.find("**/*.{}".format(ext)):
yield os.path.join(path, p)

Просмотреть файл

@ -9,11 +9,10 @@ import os
from distutils.spawn import find_executable
import mozfile
import mozpack.path as mozpath
from mozpack.files import FileFinder
from mozprocess import ProcessHandlerMixin
from mozlint import result
from mozlint.pathutils import expand_exclusions
here = os.path.abspath(os.path.dirname(__file__))
@ -56,20 +55,7 @@ def run_linter(python, paths, config, **lintargs):
return 1
return []
root = lintargs['root']
pattern = "**/*.py"
exclude = [mozpath.join(root, e) for e in config.get('exclude', [])]
files = []
for path in paths:
path = mozpath.normsep(path)
if os.path.isfile(path):
files.append(path)
continue
ignore = [e[len(path):].lstrip('/') for e in exclude
if mozpath.commonprefix((path, e)) == path]
finder = FileFinder(path, ignore=ignore)
files.extend([os.path.join(path, p) for p, f in finder.find(pattern)])
files = expand_exclusions(paths, config, lintargs['root'])
with mozfile.NamedTemporaryFile(mode='w') as fh:
fh.write('\n'.join(files))