mozlint package¶
Subpackages¶
Submodules¶
mozlint.cli module¶
- class mozlint.cli.MozlintParser(**kwargs)¶
Bases:
argparse.ArgumentParser
- arguments = [[['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."}], [['-l', '--linter'], {'dest': 'linters', 'default': [], 'action': 'append', 'help': "Linters to run, e.g 'eslint'. By default all linters are run for all the appropriate files."}], [['--list'], {'dest': 'list_linters', 'default': False, 'action': 'store_true', 'help': 'List all available linters and exit.'}], [['-W', '--warnings'], {'dest': 'show_warnings', 'default': False, 'action': 'store_true', 'help': 'Display and fail on warnings in addition to errors.'}], [['-v', '--verbose'], {'dest': 'show_verbose', 'default': False, 'action': 'store_true', 'help': 'Enable verbose logging.'}], [['-f', '--format'], {'dest': 'formats', 'action': 'append', 'help': "Formatter to use. Defaults to 'stylish' on stdout. You can specify an optional path as --format formatter:path that will be used instead of stdout. You can also use multiple formatters at the same time. Formatters available: compact, json, stylish, summary, treeherder, unix."}], [['-n', '--no-filter'], {'dest': 'use_filters', 'default': True, 'action': 'store_false', 'help': "Ignore all filtering. This is useful for quickly testing a directory that otherwise wouldn't be run, without needing to modify the config file."}], [['--include-third-party'], {'dest': 'include_third-party', 'default': False, 'action': 'store_true', 'help': 'Also run the linter(s) on third-party code'}], [['-o', '--outgoing'], {'const': True, 'nargs': '?', 'help': 'Lint files touched by commits that are not on the remote repository. Without arguments, finds the default remote that would be pushed to. The remote branch can also be specified manually. Works with mercurial or git.'}], [['-w', '--workdir'], {'const': 'all', 'nargs': '?', 'choices': ['staged', 'all'], 'help': "Lint files touched by changes in the working directory (i.e haven't been committed yet). On git, --workdir=staged can be used to only consider staged files. Works with mercurial or git."}], [['-r', '--rev'], {'default': None, 'type': <class 'str'>, 'help': 'Lint files touched by changes in revisions described by REV. For mercurial, it may be any revset. For git, it is a single tree-ish.'}], [['--fix'], {'action': 'store_true', 'default': False, 'help': 'Fix lint errors if possible. Any errors that could not be fixed will be printed as normal.'}], [['--edit'], {'action': 'store_true', 'default': False, 'help': 'Each file containing lint errors will be opened in $EDITOR one after the other.'}], [['--setup'], {'action': 'store_true', 'default': False, 'help': 'Bootstrap linter dependencies without running any of the linters.'}], [['-j', '--jobs'], {'default': None, 'dest': 'num_procs', 'type': <class 'int'>, 'help': 'Number of worker processes to spawn when running linters. Defaults to the number of cores in your CPU.'}], [['--config-path'], {'action': 'append', 'default': [], 'dest': 'config_paths', 'help': '==SUPPRESS=='}], [['--check-exclude-list'], {'dest': 'check_exclude_list', 'default': False, 'action': 'store_true', 'help': 'Run linters for all the paths in the exclude list.'}], [['extra_args'], {'nargs': '...', 'help': 'Extra arguments that will be forwarded to the underlying linter.'}]]¶
- parse_known_args(*args, **kwargs)¶
- validate(args)¶
- mozlint.cli.find_linters(config_paths, linters=None)¶
- mozlint.cli.get_exclude_list_output(result, paths)¶
- mozlint.cli.run(paths, linters, formats, outgoing, workdir, rev, edit, check_exclude_list, setup=False, list_linters=False, num_procs=None, virtualenv_manager=None, **lintargs)¶
mozlint.errors module¶
- exception mozlint.errors.LintException¶
Bases:
Exception
- exception mozlint.errors.LinterNotFound(path)¶
Bases:
mozlint.errors.LintException
- exception mozlint.errors.LinterParseError(path, message)¶
Bases:
mozlint.errors.LintException
- exception mozlint.errors.LintersNotConfigured¶
Bases:
mozlint.errors.LintException
- exception mozlint.errors.NoValidLinter¶
Bases:
mozlint.errors.LintException
mozlint.parser module¶
- class mozlint.parser.Parser(root)¶
Bases:
object
Reads and validates lint configuration files.
- parse(path)¶
Read a linter and return its LINTER definition.
- Parameters
path – Path to the linter.
- Returns
List of linter definitions ([dict])
- Raises
LinterNotFound, LinterParseError
- required_attributes = ('name', 'description', 'type', 'payload')¶
mozlint.pathutils module¶
- class mozlint.pathutils.FilterPath(path)¶
Bases:
object
Helper class to make comparing and matching file paths easier.
- contains(other)¶
Return True if other is a subdirectory of self or equals self.
- property exists¶
- property ext¶
- property finder¶
- property isdir¶
- property isfile¶
- join(*args)¶
- match(patterns)¶
- mozlint.pathutils.ancestors(path)¶
- mozlint.pathutils.collapse(paths, base=None, dotfiles=False)¶
Given an iterable of paths, collapse them into the smallest possible set of paths that contain the original set (without containing any extra paths).
For example, if directory ‘a’ contains two files b.txt and c.txt, calling:
collapse([‘a/b.txt’, ‘a/c.txt’])
returns [‘a’]. But if a third file d.txt also exists, then it will return [‘a/b.txt’, ‘a/c.txt’] since [‘a’] would also include that extra file.
- Parameters
paths – An iterable of paths (files and directories) to collapse.
- Returns
The smallest set of paths (files and directories) that contain the original set of paths and only the original set.
- mozlint.pathutils.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.
- Parameters
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.
- mozlint.pathutils.filterpaths(root, paths, include, exclude=None, extensions=None)¶
Filters a list of paths.
Given a list of paths and some filtering rules, return the set of paths that should be linted.
- Parameters
paths – A starting list of paths to possibly lint.
include – A list of paths that should be included (required).
exclude – A list of paths that should be excluded (optional).
extensions – A list of file extensions which should be considered (optional).
- Returns
A tuple containing a list of file paths to lint and a list of paths to exclude.
- mozlint.pathutils.findobject(path)¶
Find a Python object given a path of the form <modulepath>:<objectpath>. Conceptually equivalent to
- def find_object(modulepath, objectpath):
import <modulepath> as mod return mod.<objectpath>
- mozlint.pathutils.get_ancestors_by_name(name, path, root)¶
Returns a list of files called name in path’s ancestors, sorted from closest->furthest. This can be useful for finding relevant configuration files.
mozlint.result module¶
- class mozlint.result.Issue(*, linter, path, lineno=None, column=None, message, hint=None, source=None, level=None, rule=None, lineoffset=None, diff=None)¶
Bases:
object
Represents a single lint issue and its related metadata.
- Parameters
linter – name of the linter that flagged this error
path – path to the file containing the error
message – text describing the error
lineno – line number that contains the error
column – column containing the error
level – severity of the error, either ‘warning’ or ‘error’ (default ‘error’)
hint – suggestion for fixing the error (optional)
source – source code context of the error (optional)
rule – name of the rule that was violated (optional)
lineoffset – denotes an error spans multiple lines, of the form (<lineno offset>, <num lines>) (optional)
diff – a diff describing the changes that need to be made to the code
- column¶
- diff¶
- hint¶
- level¶
- lineno¶
- lineoffset¶
- linter¶
- message¶
- path¶
- relpath¶
- rule¶
- source¶
- class mozlint.result.IssueEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)¶
Bases:
json.encoder.JSONEncoder
Class for encoding :class:`~result.Issue`s to json.
Usage:
json.dumps(results, cls=IssueEncoder)
- default(o)¶
Implement this method in a subclass such that it returns a serializable object for
o
, or calls the base implementation (to raise aTypeError
).For example, to support arbitrary iterators, you could implement default like this:
def default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) # Let the base class default method raise the TypeError return JSONEncoder.default(self, o)
- class mozlint.result.ResultSummary(root)¶
Bases:
object
Represents overall result state from an entire lint run.
- property failed¶
- reset()¶
- property returncode¶
- root = None¶
- property total_fixed¶
- property total_issues¶
- property total_suppressed_warnings¶
- update(other)¶
Merge results from another ResultSummary into this one.
- mozlint.result.from_config(config, **kwargs)¶
Create a
Issue
from a linter config.Convenience method that pulls defaults from a linter config and forwards them.
- Parameters
config – linter config as defined in a .yml file
kwargs – same as
Issue
- Returns
Issue
object
mozlint.roller module¶
- class mozlint.roller.InterruptableQueue(*args, **kwargs)¶
Bases:
multiprocessing.queues.Queue
A multiprocessing.Queue that catches KeyboardInterrupt when a worker is blocking on it and returns None.
This is needed to gracefully handle KeyboardInterrupts when a worker is blocking on ProcessPoolExecutor’s call queue.
- get(*args, **kwargs)¶
- class mozlint.roller.LintRoller(root, exclude=None, **lintargs)¶
Bases:
object
Registers and runs linters.
- Parameters
root – Path to which relative paths will be joined. If unspecified, root will either be determined from version control or cwd.
lintargs – Arguments to pass to the underlying linter(s).
- MAX_PATHS_PER_JOB = 50¶
- read(paths)¶
Parse one or more linters and add them to the registry.
- Parameters
paths – A path or iterable of paths to linter definitions.
- roll(paths=None, outgoing=None, workdir=None, rev=None, num_procs=None)¶
Run all of the registered linters against the specified file paths.
- Parameters
paths – An iterable of files and/or directories to lint.
outgoing – Lint files touched by commits that are not on the remote repository.
workdir – Lint all files touched in the working directory.
num_procs – The number of processes to use. Default: cpu count
- Returns
A
ResultSummary
instance.
- setup(virtualenv_manager=None)¶
Run setup for applicable linters
- mozlint.roller.wrap_futures_atexit()¶
Sometimes futures’ atexit handler can spew tracebacks. This wrapper suppresses them.
mozlint.types module¶
- class mozlint.types.BaseType¶
Bases:
object
Abstract base class for all types of linters.
- batch = False¶
- class mozlint.types.ExternalType¶
Bases:
mozlint.types.BaseType
Linter type that runs an external function.
The function is responsible for properly formatting the results into a list of
Issue
objects.- batch = True¶
- class mozlint.types.GlobalType¶
Bases:
mozlint.types.ExternalType
Linter type that runs an external global linting function just once.
The function is responsible for properly formatting the results into a list of
Issue
objects.- batch = True¶
- class mozlint.types.LineType¶
Bases:
mozlint.types.BaseType
Abstract base class for linter types that check each line individually.
Subclasses of this linter type will read each file and check the provided payload against each line one by one.
- abstract condition(line, config)¶
- class mozlint.types.LintHandler(config)¶
Bases:
mozlog.reader.LogHandler
- lint(data)¶
- class mozlint.types.RegexType¶
Bases:
mozlint.types.LineType
Linter type that checks whether a regex match is found.
- condition(payload, line, config)¶
- class mozlint.types.StringType¶
Bases:
mozlint.types.LineType
Linter type that checks whether a substring is found.
- condition(payload, line, config)¶
- class mozlint.types.StructuredLogType¶
Bases:
mozlint.types.BaseType
- batch = True¶
- mozlint.types.supported_types = {'external': <mozlint.types.ExternalType object>, 'global': <mozlint.types.GlobalType object>, 'regex': <mozlint.types.RegexType object>, 'string': <mozlint.types.StringType object>, 'structured_log': <mozlint.types.StructuredLogType object>}¶
Mapping of type string to an associated instance.