| 1 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
|
| 5 |
import os |
| 6 |
|
| 7 |
import yaml |
| 8 |
|
| 9 |
from .errors import LinterNotFound, LinterParseError |
| 10 |
from .types import supported_types |
| 11 |
|
| 12 |
GLOBAL_SUPPORT_FILES = [] |
| 13 |
|
| 14 |
|
| 15 |
class Parser: |
| 16 |
"""Reads and validates lint configuration files.""" |
| 17 |
|
| 18 |
required_attributes = ( |
| 19 |
"name", |
| 20 |
"description", |
| 21 |
"type", |
| 22 |
"payload", |
| 23 |
) |
| 24 |
|
| 25 |
def __init__(self, root): |
| 26 |
self.root = root |
| 27 |
|
| 28 |
def __call__(self, path): |
| 29 |
return self.parse(path) |
| 30 |
|
| 31 |
def _validate(self, linter): |
| 32 |
relpath = os.path.relpath(linter["path"], self.root) |
| 33 |
|
| 34 |
missing_attrs = [] |
| 35 |
for attr in self.required_attributes: |
| 36 |
if attr not in linter: |
| 37 |
missing_attrs.append(attr) |
| 38 |
|
| 39 |
if missing_attrs: |
| 40 |
raise LinterParseError( |
| 41 |
relpath, |
| 42 |
"Missing required attribute(s): " "{}".format(",".join(missing_attrs)), |
| 43 |
) |
| 44 |
|
| 45 |
if linter["type"] not in supported_types: |
| 46 |
raise LinterParseError(relpath, "Invalid type '{}'".format(linter["type"])) |
| 47 |
|
| 48 |
for attr in ("include", "exclude", "support-files"): |
| 49 |
if attr not in linter: |
| 50 |
continue |
| 51 |
|
| 52 |
if not isinstance(linter[attr], list) or not all( |
| 53 |
isinstance(a, str) for a in linter[attr] |
| 54 |
): |
| 55 |
raise LinterParseError( |
| 56 |
relpath, |
| 57 |
f"The {attr} directive must be a " "list of strings!", |
| 58 |
) |
| 59 |
invalid_paths = set() |
| 60 |
for path in linter[attr]: |
| 61 |
if "*" in path: |
| 62 |
if attr == "include": |
| 63 |
raise LinterParseError( |
| 64 |
relpath, |
| 65 |
"Paths in the include directive cannot " |
| 66 |
f"contain globs:\n {path}", |
| 67 |
) |
| 68 |
continue |
| 69 |
|
| 70 |
abspath = path |
| 71 |
if not os.path.isabs(abspath): |
| 72 |
abspath = os.path.join(self.root, path) |
| 73 |
|
| 74 |
if not os.path.exists(abspath): |
| 75 |
invalid_paths.add(" " + path) |
| 76 |
|
| 77 |
if invalid_paths: |
| 78 |
raise LinterParseError( |
| 79 |
relpath, |
| 80 |
"The {} directive contains the following " |
| 81 |
"paths that don't exist:\n{}".format( |
| 82 |
attr, "\n".join(sorted(invalid_paths)) |
| 83 |
), |
| 84 |
) |
| 85 |
|
| 86 |
if "setup" in linter: |
| 87 |
if linter["setup"].count(":") != 1: |
| 88 |
raise LinterParseError( |
| 89 |
relpath, |
| 90 |
"The setup attribute '{!r}' must have the " |
| 91 |
"form 'module:object'".format(linter["setup"]), |
| 92 |
) |
| 93 |
|
| 94 |
if "extensions" in linter and "exclude_extensions" in linter: |
| 95 |
raise LinterParseError( |
| 96 |
relpath, |
| 97 |
"Can't have both 'extensions' and 'exclude_extensions'!", |
| 98 |
) |
| 99 |
|
| 100 |
for prop in ["extensions", "exclude_extensions"]: |
| 101 |
if prop in linter: |
| 102 |
linter[prop] = [e.strip(".") for e in linter[prop]] |
| 103 |
|
| 104 |
def parse(self, path): |
| 105 |
"""Read a linter and return its LINTER definition. |
| 106 |
|
| 107 |
:param path: Path to the linter. |
| 108 |
:returns: List of linter definitions ([dict]) |
| 109 |
:raises: LinterNotFound, LinterParseError |
| 110 |
""" |
| 111 |
if not os.path.isfile(path): |
| 112 |
raise LinterNotFound(path) |
| 113 |
|
| 114 |
if not path.endswith(".yml"): |
| 115 |
raise LinterParseError( |
| 116 |
path, "Invalid filename, linters must end with '.yml'!" |
| 117 |
) |
| 118 |
|
| 119 |
with open(path) as fh: |
| 120 |
configs = list(yaml.safe_load_all(fh)) |
| 121 |
|
| 122 |
if not configs: |
| 123 |
raise LinterParseError(path, "No lint definitions found!") |
| 124 |
|
| 125 |
linters = [] |
| 126 |
for config in configs: |
| 127 |
for name, linter in config.items(): |
| 128 |
linter["name"] = name |
| 129 |
linter["path"] = path |
| 130 |
self._validate(linter) |
| 131 |
linter.setdefault("support-files", []).extend( |
| 132 |
GLOBAL_SUPPORT_FILES + [path] |
| 133 |
) |
| 134 |
linter.setdefault("include", ["."]) |
| 135 |
linters.append(linter) |
| 136 |
|
| 137 |
return linters |