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/.
9 from mozfile
import which
10 from mozlint
import result
11 from mozlint
.pathutils
import expand_exclusions
20 abspath
= os
.path
.abspath(os
.path
.dirname(__file__
))
21 rstcheck_requirements_file
= os
.path
.join(abspath
, "requirements.txt")
25 RSTCHECK_NOT_FOUND
= """
26 Could not find rstcheck! Install rstcheck and try again.
28 $ pip install -U --require-hashes -r {}
30 rstcheck_requirements_file
33 RSTCHECK_INSTALL_ERROR
= """
34 Unable to install required version of rstcheck
35 Try to install it manually with:
36 $ pip install -U --require-hashes -r {}
38 rstcheck_requirements_file
41 RSTCHECK_FORMAT_REGEX
= re
.compile(r
"(.*):(.*): \(.*/([0-9]*)\) (.*)$")
44 def get_rstcheck_binary():
46 Returns the path of the first rstcheck binary available
47 if not found returns None
49 binary
= os
.environ
.get("RSTCHECK")
53 return which("rstcheck")
56 def parse_with_split(errors
):
57 match
= RSTCHECK_FORMAT_REGEX
.match(errors
)
60 filename
, lineno
, level
, message
= match
.groups()
62 return filename
, lineno
, level
, message
65 def lint(files
, config
, **lintargs
):
67 config
["root"] = lintargs
["root"]
68 paths
= expand_exclusions(files
, config
, config
["root"])
71 binary
= get_rstcheck_binary()
74 # Config for rstcheck is stored in `/.rstcheck.cfg`.
75 cmdargs
= [which("python"), binary
] + paths
[:chunk_size
]
76 log
.debug("Command: {}".format(" ".join(cmdargs
)))
78 proc
= subprocess
.Popen(
80 stdout
=subprocess
.PIPE
,
81 stderr
=subprocess
.PIPE
,
83 universal_newlines
=True,
85 all_errors
= proc
.communicate()[1]
86 for errors
in all_errors
.split("\n"):
88 split_result
= parse_with_split(errors
)
90 filename
, lineno
, level
, message
= split_result
95 "level": "error" if int(level
) >= 2 else "warning",
97 results
.append(result
.from_config(config
, **res
))
98 paths
= paths
[chunk_size
:]