From 6bf0a5cb5034a7e684dcc3500e841785237ce2dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 19:32:43 +0200 Subject: Adding upstream version 1:115.7.0. Signed-off-by: Daniel Baumann --- comm/tools/lint/commlint/__init__.py | 111 +++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 comm/tools/lint/commlint/__init__.py (limited to 'comm/tools/lint/commlint/__init__.py') diff --git a/comm/tools/lint/commlint/__init__.py b/comm/tools/lint/commlint/__init__.py new file mode 100644 index 0000000000..19d887e109 --- /dev/null +++ b/comm/tools/lint/commlint/__init__.py @@ -0,0 +1,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) -- cgit v1.2.3