summaryrefslogtreecommitdiffstats
path: root/comm/tools/lint/commlint/__init__.py
blob: 19d887e1090d183be54be7d350c08f171b1b92a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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/.

import os
from contextlib import contextmanager
from pathlib import Path

from mozlint.pathutils import expand_exclusions
from mozlint.types import supported_types
from mozpack import path as mozpath

COMM_EXCLUSION_FILES = [
    os.path.join("comm", "tools", "lint", "ThirdPartyPaths.txt"),
    os.path.join("comm", "tools", "lint", "Generated.txt"),
]

TASKCLUSTER_EXCLUDE_PATHS = (os.path.join("comm", "suite"),)


@contextmanager
def pushd(dest_path: Path):
    """
    Sets the cwd within the context
    :param Path dest_path: The path to the cwd
    """
    origin = Path().absolute()
    try:
        os.chdir(dest_path)
        yield
    finally:
        os.chdir(origin)


def _apply_global_excludes(root, config):
    exclude = config.get("exclude", [])

    for path in COMM_EXCLUSION_FILES:
        with open(os.path.join(root, path), "r") as fh:
            exclude.extend([mozpath.join(root, f.strip()) for f in fh.readlines()])

    if os.environ.get("MOZLINT_NO_SUITE", None):
        # Ignore Seamonkey-only paths when run from Taskcluster
        suite_excludes = [mozpath.join(root, path) for path in TASKCLUSTER_EXCLUDE_PATHS]
        exclude.extend(suite_excludes)

    config["exclude"] = exclude


# This makes support file paths absolute, allowing lintpref to find StaticPrefList.yaml
def _expand_support_files(root, config):
    support_files = config.get("support-files", [])
    absolute_support_files = [mozpath.join(root, f) for f in support_files]
    config["support-files"] = absolute_support_files


def eslint_wrapper(paths, config, **lintargs):
    comm_root = Path(lintargs["root"]) / "comm"
    with pushd(comm_root):
        rv = lint_wrapper(paths, config, **lintargs)

    return rv


def stylelint_wrapper(paths, config, **lintargs):
    comm_root = Path(lintargs["root"]) / "comm"

    ignore_file = str(comm_root / ".stylelintignore")
    lintargs.setdefault("extra_args", [])
    lintargs["extra_args"].extend(["--ignore-path", ignore_file])

    with pushd(comm_root):
        rv = lint_wrapper(paths, config, **lintargs)

    return rv


def black_lint(paths, config, fix=None, **lintargs):
    from python.black import run_black

    files = list(expand_exclusions(paths, config, lintargs["root"]))

    # prepend "--line-length 99" to files, it will be processed as an argument
    black_args = ["-l", "99"] + files

    return run_black(
        config,
        black_args,
        fix=fix,
        log=lintargs["log"],
        virtualenv_bin_path=lintargs.get("virtualenv_bin_path"),
    )


def lint_wrapper(paths, config, **lintargs):
    _apply_global_excludes(lintargs["root"], config)
    _expand_support_files(lintargs["root"], config)

    payload = supported_types[config.get("wrappedType", config["type"])]
    config["payload"] = config["wraps"]
    del config["wraps"]

    if config.get("wrappedType", ""):
        config["type"] = config["wrappedType"]
        del config["wrappedType"]

    if config.get("commroot", False):
        lintargs["root"] = os.path.join(lintargs["root"], "comm")
        del config["commroot"]

    return payload(paths, config, **lintargs)