|
1 | | -"""Sanity test using rstcheck and sphinx.""" |
| 1 | +"""Sanity test using rstcheck-core and sphinx.""" |
2 | 2 | from __future__ import annotations |
3 | 3 |
|
4 | | -import re |
5 | | -import subprocess |
| 4 | +from rstcheck_core.runner import RstcheckMainRunner |
| 5 | +from rstcheck_core.config import RstcheckConfig |
| 6 | +import pathlib |
6 | 7 | import sys |
7 | 8 |
|
8 | 9 |
|
9 | 10 | def main(): |
10 | | - paths = sys.argv[1:] or sys.stdin.read().splitlines() |
11 | | - |
12 | | - encoding = 'utf-8' |
13 | | - |
14 | | - ignore_substitutions = ( |
15 | | - 'br', |
| 11 | + # Read input paths from CLI arguments or stdin |
| 12 | + if sys.argv[1:]: |
| 13 | + raw_paths = sys.argv[1:] |
| 14 | + else: |
| 15 | + raw_paths = sys.stdin.read().splitlines() |
| 16 | + |
| 17 | + # Convert to pathlib.Path objects |
| 18 | + paths = [pathlib.Path(p) for p in raw_paths] |
| 19 | + |
| 20 | + # Handle case where no paths are provided |
| 21 | + if not paths: |
| 22 | + print("No files or directories provided for checking.", file=sys.stderr) |
| 23 | + sys.exit(1) |
| 24 | + |
| 25 | + # Define the configuration for rstcheck |
| 26 | + config = RstcheckConfig( |
| 27 | + ignore_roles=[ |
| 28 | + "ansplugin", "ansopt", "ansretval", "ansval", "ansenvvar", "ansenvvarref" |
| 29 | + ], |
| 30 | + ignore_substitutions=["br"], |
| 31 | + report_level="warning", # Adjust report level as needed: "info", "warning", "error", "severe", "none" |
| 32 | + recursive=True, # Set to True to check directories recursively |
16 | 33 | ) |
17 | 34 |
|
18 | | - cmd = [ |
19 | | - sys.executable, |
20 | | - '-c', 'import rstcheck; rstcheck.main();', |
21 | | - '--report', 'warning', |
22 | | - '--ignore-roles', 'ansplugin,ansopt,ansretval,ansval,ansenvvar,ansenvvarref', |
23 | | - '--ignore-substitutions', ','.join(ignore_substitutions), |
24 | | - ] + paths |
25 | | - |
26 | | - process = subprocess.run(cmd, |
27 | | - stdin=subprocess.DEVNULL, |
28 | | - stdout=subprocess.PIPE, |
29 | | - stderr=subprocess.PIPE, |
30 | | - check=False, |
31 | | - ) |
32 | | - |
33 | | - if process.stdout: |
34 | | - raise Exception(process.stdout) |
35 | | - |
36 | | - pattern = re.compile(r'^(?P<path>[^:]*):(?P<line>[0-9]+): \((?P<level>INFO|WARNING|ERROR|SEVERE)/[0-4]\) (?P<message>.*)$') |
37 | | - |
38 | | - results = parse_to_list_of_dict(pattern, process.stderr.decode(encoding)) |
39 | | - |
40 | | - for result in results: |
41 | | - print('%s:%s:%s: %s' % (result['path'], result['line'], 0, result['message'])) |
42 | | - |
43 | | - |
44 | | -def parse_to_list_of_dict(pattern, value): |
45 | | - matched = [] |
46 | | - unmatched = [] |
47 | | - |
48 | | - for line in value.splitlines(): |
49 | | - match = re.search(pattern, line) |
50 | | - |
51 | | - if match: |
52 | | - matched.append(match.groupdict()) |
53 | | - else: |
54 | | - unmatched.append(line) |
55 | | - |
56 | | - if unmatched: |
57 | | - raise Exception('Pattern "%s" did not match values:\n%s' % (pattern, '\n'.join(unmatched))) |
58 | | - |
59 | | - return matched |
60 | | - |
| 35 | + # Initialize the runner |
| 36 | + runner = RstcheckMainRunner( |
| 37 | + check_paths=paths, |
| 38 | + rstcheck_config=config, |
| 39 | + overwrite_config=True, |
| 40 | + ) |
| 41 | + |
| 42 | + # Run the checks |
| 43 | + exit_code = runner.run() |
| 44 | + |
| 45 | + # Exit with the appropriate code |
| 46 | + sys.exit(exit_code) |
61 | 47 |
|
62 | 48 | if __name__ == '__main__': |
63 | 49 | main() |
0 commit comments