summaryrefslogtreecommitdiffstats
path: root/tools/lint/python/compat.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /tools/lint/python/compat.py
parentInitial commit. (diff)
downloadfirefox-upstream.tar.xz
firefox-upstream.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tools/lint/python/compat.py')
-rw-r--r--tools/lint/python/compat.py91
1 files changed, 91 insertions, 0 deletions
diff --git a/tools/lint/python/compat.py b/tools/lint/python/compat.py
new file mode 100644
index 0000000000..dd541bd66e
--- /dev/null
+++ b/tools/lint/python/compat.py
@@ -0,0 +1,91 @@
+# 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 json
+import os
+from distutils.spawn import find_executable
+
+import mozfile
+from mozprocess import ProcessHandlerMixin
+
+from mozlint import result
+from mozlint.pathutils import expand_exclusions
+
+here = os.path.abspath(os.path.dirname(__file__))
+
+results = []
+
+
+class PyCompatProcess(ProcessHandlerMixin):
+ def __init__(self, config, *args, **kwargs):
+ self.config = config
+ kwargs["processOutputLine"] = [self.process_line]
+ ProcessHandlerMixin.__init__(self, *args, **kwargs)
+
+ def process_line(self, line):
+ try:
+ res = json.loads(line)
+ except ValueError:
+ print(
+ "Non JSON output from {} linter: {}".format(self.config["name"], line)
+ )
+ return
+
+ res["level"] = "error"
+ results.append(result.from_config(self.config, **res))
+
+
+def setup(python):
+ """Setup doesn't currently do any bootstrapping. For now, this function
+ is only used to print the warning message.
+ """
+ binary = find_executable(python)
+ if not binary:
+ # TODO Bootstrap python2/python3 if not available
+ print("warning: {} not detected, skipping py-compat check".format(python))
+
+
+def run_linter(python, paths, config, **lintargs):
+ log = lintargs["log"]
+ binary = find_executable(python)
+ if not binary:
+ # If we're in automation, this is fatal. Otherwise, the warning in the
+ # setup method was already printed.
+ if "MOZ_AUTOMATION" in os.environ:
+ return 1
+ return []
+
+ files = expand_exclusions(paths, config, lintargs["root"])
+
+ with mozfile.NamedTemporaryFile(mode="w") as fh:
+ fh.write("\n".join(files))
+ fh.flush()
+
+ cmd = [binary, os.path.join(here, "check_compat.py"), fh.name]
+ log.debug("Command: {}".format(" ".join(cmd)))
+
+ proc = PyCompatProcess(config, cmd)
+ proc.run()
+ try:
+ proc.wait()
+ except KeyboardInterrupt:
+ proc.kill()
+
+ return results
+
+
+def setuppy2(**lintargs):
+ return setup("python2")
+
+
+def lintpy2(*args, **kwargs):
+ return run_linter("python2", *args, **kwargs)
+
+
+def setuppy3(**lintargs):
+ return setup("python3")
+
+
+def lintpy3(*args, **kwargs):
+ return run_linter("python3", *args, **kwargs)