mozlint package

Subpackages

Submodules

mozlint.cli module

class mozlint.cli.MozlintParser(**kwargs)

Bases: ArgumentParser

arguments = [[['paths'], {'default': None, 'help': "Paths to file or directories to lint, like 'browser/components/loop' or 'mobile/android'. If not provided, defaults to the files changed according to --outgoing and --workdir.", 'nargs': '*'}], [['-l', '--linter'], {'action': 'append', 'default': [], 'dest': 'linters', 'help': "Linters to run, e.g 'eslint'. By default all linters are run for all the appropriate files."}], [['--list'], {'action': 'store_true', 'default': False, 'dest': 'list_linters', 'help': 'List all available linters and exit.'}], [['-W', '--warnings'], {'choices': ['soft'], 'const': True, 'dest': 'show_warnings', 'help': 'Display and fail on warnings in addition to errors. --warnings=soft can be used to report warnings but only fail on errors.', 'nargs': '?'}], [['-v', '--verbose'], {'action': 'store_true', 'default': False, 'dest': 'show_verbose', 'help': 'Enable verbose logging.'}], [['-f', '--format'], {'action': 'append', 'dest': 'formats', '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'], {'action': 'store_false', 'default': True, 'dest': 'use_filters', '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'], {'action': 'store_true', 'default': False, 'dest': 'include_third-party', 'help': 'Also run the linter(s) on third-party code'}], [['-o', '--outgoing'], {'const': True, '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.', 'nargs': '?'}], [['-w', '--workdir'], {'choices': ['staged', 'all'], 'const': '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.", 'nargs': '?'}], [['-r', '--rev'], {'default': None, '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.', 'type': <class 'str'>}], [['--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', 'help': 'Number of worker processes to spawn when running linters. Defaults to the number of cores in your CPU.', 'type': <class 'int'>}], [['--config-path'], {'action': 'append', 'default': [], 'dest': 'config_paths', 'help': '==SUPPRESS=='}], [['--check-exclude-list'], {'action': 'store_true', 'default': False, 'dest': 'check_exclude_list', 'help': 'Run linters for all the paths in the exclude list.'}], [['extra_args'], {'help': 'Extra arguments that will be forwarded to the underlying linter.', 'nargs': '...'}]]
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.main() int
mozlint.cli.run(paths, linters, formats, outgoing, workdir, rev, edit, check_exclude_list, setup=False, list_linters=False, num_procs=None, virtualenv_manager=None, setupargs=None, **lintargs)

mozlint.editor module

mozlint.editor.edit_issues(result)
mozlint.editor.get_editor()

mozlint.errors module

exception mozlint.errors.LintException

Bases: Exception

exception mozlint.errors.LinterNotFound(path)

Bases: LintException

exception mozlint.errors.LinterParseError(path, message)

Bases: LintException

exception mozlint.errors.LintersNotConfigured

Bases: LintException

exception mozlint.errors.NoValidLinter

Bases: 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, exclude_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. Note that at most one of extensions or exclude_extensions should be provided (ie not both).

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).

  • exclude_extensions – A list of file extensions which should not 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: JSONEncoder

Class for encoding Issue 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 a TypeError).

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, fail_on_warnings=True)

Bases: object

Represents overall result state from an entire lint run.

property failed
has_issues_failure()

Returns true in case issues were detected during the lint run. Do not consider warning issues in case self.fail_on_warnings is set to False.

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: 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, setupargs=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

should_lint_entire_tree(vcs_paths: Set[str], linter: Dict) bool

Return True if the linter should be run on the entire tree.

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.ExternalFileType

Bases: ExternalType

batch = False
class mozlint.types.ExternalType

Bases: 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: 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: 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: LogHandler

lint(data)
class mozlint.types.RegexType

Bases: LineType

Linter type that checks whether a regex match is found.

condition(payload, line, config)
class mozlint.types.StringType

Bases: LineType

Linter type that checks whether a substring is found.

condition(payload, line, config)
class mozlint.types.StructuredLogType

Bases: BaseType

batch = True
mozlint.types.supported_types = {'external': <mozlint.types.ExternalType object>, 'external-file': <mozlint.types.ExternalFileType 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.

Module contents