forked from j2kun/chktex-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_action.py
83 lines (61 loc) · 2.11 KB
/
run_action.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""Find all *tex files and run chktex on them"""
import os
import subprocess
import sys
SKIP_DIRS = set(["venv", ".git", "__pycache__"])
GITHUB_WORKSPACE = os.environ.get("GITHUB_WORKSPACE")
if not GITHUB_WORKSPACE:
print("No GITHUB_WORKSPACE environment variable set.")
sys.exit(1)
os.chdir(GITHUB_WORKSPACE)
CHKTEXRC = os.path.abspath(".chktexrc")
if os.path.exists(CHKTEXRC):
print("found local chktexrc")
CHKTEX_COMMAND = lambda file: ["chktex", "-q", "-l", CHKTEXRC, file]
else:
CHKTEX_COMMAND = lambda file: ["chktex", "-q", file]
def main():
"""main function"""
all_files_in_tree = []
for root, dirs, files in os.walk(".", topdown=True):
for skip_dir in SKIP_DIRS:
if skip_dir in dirs:
dirs.remove(skip_dir)
for file in files:
all_files_in_tree.append(os.path.join(root, file))
files_to_process = [file for file in all_files_in_tree if file.endswith(".tex")]
if not files_to_process:
print("Found no .tex files to process")
print("Complete tree found:")
for file in all_files_in_tree:
print(file)
sys.exit(0)
files_with_errors = 0
for file in files_to_process:
print(f"Linting {file}")
directory = os.path.dirname(file)
relative_file = os.path.basename(file)
# run process inside the file's folder
completed_process = subprocess.run(
CHKTEX_COMMAND(relative_file),
cwd=directory,
capture_output=True,
text=True,
check=False,
)
stdout = completed_process.stdout
stderr = completed_process.stderr
if stdout:
files_with_errors += 1
print(
"----------------------------------------",
stdout,
"----------------------------------------",
sep="\n",
)
if stderr:
print("chktex run into errors:", stderr, sep="\n")
print(f"found {files_with_errors} files with errors")
sys.exit(files_with_errors)
if __name__ == "__main__":
main()