summaryrefslogtreecommitdiffstats
path: root/tools/lint/rst
diff options
context:
space:
mode:
Diffstat (limited to 'tools/lint/rst')
-rw-r--r--tools/lint/rst/__init__.py107
-rw-r--r--tools/lint/rst/requirements.in2
-rw-r--r--tools/lint/rst/requirements.txt18
3 files changed, 127 insertions, 0 deletions
diff --git a/tools/lint/rst/__init__.py b/tools/lint/rst/__init__.py
new file mode 100644
index 0000000000..1852d0f530
--- /dev/null
+++ b/tools/lint/rst/__init__.py
@@ -0,0 +1,107 @@
+# 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
+import re
+import subprocess
+
+from mozlint import result
+from mozlint.pathutils import expand_exclusions
+from mozfile import which
+
+# Error Levels
+# (0, 'debug')
+# (1, 'info')
+# (2, 'warning')
+# (3, 'error')
+# (4, 'severe')
+
+abspath = os.path.abspath(os.path.dirname(__file__))
+rstcheck_requirements_file = os.path.join(abspath, "requirements.txt")
+
+results = []
+
+RSTCHECK_NOT_FOUND = """
+Could not find rstcheck! Install rstcheck and try again.
+
+ $ pip install -U --require-hashes -r {}
+""".strip().format(
+ rstcheck_requirements_file
+)
+
+RSTCHECK_INSTALL_ERROR = """
+Unable to install required version of rstcheck
+Try to install it manually with:
+ $ pip install -U --require-hashes -r {}
+""".strip().format(
+ rstcheck_requirements_file
+)
+
+RSTCHECK_FORMAT_REGEX = re.compile(r"(.*):(.*): \(.*/([0-9]*)\) (.*)$")
+
+
+def setup(root, **lintargs):
+ virtualenv_manager = lintargs["virtualenv_manager"]
+ try:
+ virtualenv_manager.install_pip_requirements(
+ rstcheck_requirements_file, quiet=True
+ )
+ except subprocess.CalledProcessError:
+ print(RSTCHECK_INSTALL_ERROR)
+ return 1
+
+
+def get_rstcheck_binary():
+ """
+ Returns the path of the first rstcheck binary available
+ if not found returns None
+ """
+ binary = os.environ.get("RSTCHECK")
+ if binary:
+ return binary
+
+ return which("rstcheck")
+
+
+def parse_with_split(errors):
+ match = RSTCHECK_FORMAT_REGEX.match(errors)
+ filename, lineno, level, message = match.groups()
+
+ return filename, lineno, level, message
+
+
+def lint(files, config, **lintargs):
+ log = lintargs["log"]
+ config["root"] = lintargs["root"]
+ paths = expand_exclusions(files, config, config["root"])
+ paths = list(paths)
+ chunk_size = 50
+ binary = get_rstcheck_binary()
+ rstcheck_options = "--ignore-language=cpp,json"
+
+ while paths:
+ cmdargs = [which("python"), binary, rstcheck_options] + paths[:chunk_size]
+ log.debug("Command: {}".format(" ".join(cmdargs)))
+
+ proc = subprocess.Popen(
+ cmdargs,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ env=os.environ,
+ universal_newlines=True,
+ )
+ all_errors = proc.communicate()[1]
+ for errors in all_errors.split("\n"):
+ if len(errors) > 1:
+ filename, lineno, level, message = parse_with_split(errors)
+ res = {
+ "path": filename,
+ "message": message,
+ "lineno": lineno,
+ "level": "error" if int(level) >= 2 else "warning",
+ }
+ results.append(result.from_config(config, **res))
+ paths = paths[chunk_size:]
+
+ return results
diff --git a/tools/lint/rst/requirements.in b/tools/lint/rst/requirements.in
new file mode 100644
index 0000000000..72419556fd
--- /dev/null
+++ b/tools/lint/rst/requirements.in
@@ -0,0 +1,2 @@
+rstcheck==3.3.1
+Pygments==2.6.1
diff --git a/tools/lint/rst/requirements.txt b/tools/lint/rst/requirements.txt
new file mode 100644
index 0000000000..45fe3ea82f
--- /dev/null
+++ b/tools/lint/rst/requirements.txt
@@ -0,0 +1,18 @@
+#
+# This file is autogenerated by pip-compile
+# To update, run:
+#
+# pip-compile --generate-hashes --output-file=tools/lint/rst/requirements.txt tools/lint/rst/requirements.in
+#
+docutils==0.14 \
+ --hash=sha256:02aec4bd92ab067f6ff27a38a38a41173bf01bed8f89157768c1573f53e474a6 \
+ --hash=sha256:51e64ef2ebfb29cae1faa133b3710143496eca21c530f3f71424d77687764274 \
+ --hash=sha256:7a4bd47eaf6596e1295ecb11361139febe29b084a87bf005bf899f9a42edc3c6 \
+ # via rstcheck
+pygments==2.6.1 \
+ --hash=sha256:647344a061c249a3b74e230c739f434d7ea4d8b1d5f3721bc0f3558049b38f44 \
+ --hash=sha256:ff7a40b4860b727ab48fad6360eb351cc1b33cbf9b15a0f689ca5353e9463324 \
+ # via -r tools/lint/rst/requirements.in
+rstcheck==3.3.1 \
+ --hash=sha256:92c4f79256a54270e0402ba16a2f92d0b3c15c8f4410cb9c57127067c215741f \
+ # via -r tools/lint/rst/requirements.in