gecko-dev/tools/lint/mach_commands.py
Andrew Halberstadt b1445349ea Bug 1270506 - [mozlint] Add python flake8 linter, r=smacleod
For now, only the following two directories will be linted:
python/mozlint
tools/lint

New directories can be added by adding them to the 'include'
directive in tools/lint/flake8.lint. They all default to the
configuration specified in topsrcdir/.flake8. Subdirectories
can override this configuration by creating their own .flake8
file.

MozReview-Commit-ID: Eag48Lnkp3l

--HG--
extra : rebase_source : 6d98c9fef80055a48cc2622848aa04c3045b747e
2016-05-05 17:21:12 -04:00

91 lines
2.6 KiB
Python

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import absolute_import, print_function, unicode_literals
import os
from mozbuild.base import (
MachCommandBase,
)
from mach.decorators import (
CommandArgument,
CommandProvider,
Command,
SubCommand,
)
here = os.path.abspath(os.path.dirname(__file__))
@CommandProvider
class MachCommands(MachCommandBase):
@Command(
'lint', category='devenv',
description='Run linters.')
@CommandArgument(
'paths', nargs='*', default=None,
help="Paths to file or directories to lint, like "
"'browser/components/loop' or 'mobile/android'. "
"Defaults to the current directory if not given.")
@CommandArgument(
'-l', '--linter', dest='linters', default=None, action='append',
help="Linters to run, e.g 'eslint'. By default all linters are run "
"for all the appropriate files.")
@CommandArgument(
'-f', '--format', dest='fmt', default='stylish',
help="Formatter to use. Defaults to 'stylish'.")
def lint(self, paths, linters=None, fmt='stylish', **lintargs):
"""Run linters."""
from mozlint import LintRoller, formatters
paths = paths or ['.']
lint_files = self.find_linters(linters)
lintargs['exclude'] = ['obj*']
lint = LintRoller(**lintargs)
lint.read(lint_files)
# run all linters
results = lint.roll(paths)
formatter = formatters.get(fmt)
print(formatter(results))
@SubCommand('lint', 'setup',
"Setup required libraries for specified lints.")
@CommandArgument(
'-l', '--linter', dest='linters', default=None, action='append',
help="Linters to run, e.g 'eslint'. By default all linters are run "
"for all the appropriate files.")
def lint_setup(self, linters=None, **lintargs):
from mozlint import LintRoller
lint_files = self.find_linters(linters)
lint = LintRoller(lintargs=lintargs)
lint.read(lint_files)
for l in lint.linters:
if 'setup' in l:
l['setup']()
def find_linters(self, linters=None):
lints = []
files = os.listdir(here)
for f in files:
name, ext = os.path.splitext(f)
if ext != '.lint':
continue
if linters and name not in linters:
continue
lints.append(os.path.join(here, f))
return lints