Skip to content

Commit 82069c3

Browse files
committed
Improved error handling related to files
Ticket: ENT-12737 Signed-off-by: Victor Moene <victor.moene@northern.tech>
1 parent 2dc0483 commit 82069c3

File tree

1 file changed

+58
-40
lines changed

1 file changed

+58
-40
lines changed

scripts/markdown-code-checker.py

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from cfbs.pretty import pretty_file
2+
from cfbs.utils import user_error
3+
import json
24
from shutil import which
35
import markdown_it
46
import os
57
import argparse
6-
import sys
78
import subprocess
89

910

@@ -66,36 +67,47 @@ def get_markdown_files(start, languages):
6667

6768
def extract(origin_path, snippet_path, _language, first_line, last_line):
6869

69-
with open(origin_path, "r") as f:
70-
content = f.read()
70+
try:
71+
with open(origin_path, "r") as f:
72+
content = f.read()
7173

72-
code_snippet = "\n".join(content.split("\n")[first_line + 1 : last_line - 1])
74+
code_snippet = "\n".join(content.split("\n")[first_line + 1 : last_line - 1])
7375

74-
with open(snippet_path, "w") as f:
75-
f.write(code_snippet)
76+
with open(snippet_path, "w") as f:
77+
f.write(code_snippet)
78+
except IOError:
79+
user_error(f"Couldn't open '{origin_path}' or '{snippet_path}'")
7680

7781

7882
def check_syntax(origin_path, snippet_path, language, first_line, _last_line):
7983
snippet_abs_path = os.path.abspath(snippet_path)
8084

8185
if not os.path.exists(snippet_path):
82-
print(
83-
f"[error] Couldn't find the file '{snippet_path}'. Run --extract to extract the inline code."
86+
user_error(
87+
f"Couldn't find the file '{snippet_path}'. Run --extract to extract the inline code."
8488
)
85-
return
8689

8790
match language:
8891
case "cf":
89-
p = subprocess.run(
90-
["/var/cfengine/bin/cf-promises", snippet_abs_path],
91-
capture_output=True,
92-
text=True,
93-
)
94-
err = p.stderr
92+
try:
93+
p = subprocess.run(
94+
["/var/cfengine/bin/cf-promises", snippet_abs_path],
95+
capture_output=True,
96+
text=True,
97+
)
98+
err = p.stderr
9599

96-
if err:
97-
err = err.replace(snippet_abs_path, f"{origin_path}:{first_line}")
98-
print(err)
100+
if err:
101+
err = err.replace(snippet_abs_path, f"{origin_path}:{first_line}")
102+
print(err)
103+
except OSError:
104+
user_error(f"'{snippet_abs_path}' doesn't exist")
105+
except ValueError:
106+
user_error("Invalid subprocess arguments")
107+
except subprocess.CalledProcessError:
108+
user_error(f"Couldn't run cf-promises on '{snippet_abs_path}'")
109+
except subprocess.TimeoutExpired:
110+
user_error("Timed out")
99111

100112

101113
def check_output():
@@ -107,22 +119,25 @@ def replace(origin_path, snippet_path, _language, first_line, last_line):
107119
try:
108120
with open(snippet_path, "r") as f:
109121
pretty_content = f.read()
110-
except:
111-
print(
112-
f"[error] Couldn't find the file '{snippet_path}'. Run --extract to extract the inline code."
113-
)
114-
return
115122

116-
with open(origin_path, "r") as f:
117-
origin_lines = f.read().split("\n")
118-
pretty_lines = pretty_content.split("\n")
123+
with open(origin_path, "r") as f:
124+
origin_lines = f.read().split("\n")
125+
pretty_lines = pretty_content.split("\n")
119126

120-
offset = len(pretty_lines) - len(origin_lines[first_line + 1 : last_line - 1])
127+
offset = len(pretty_lines) - len(
128+
origin_lines[first_line + 1 : last_line - 1]
129+
)
121130

122-
origin_lines[first_line + 1 : last_line - 1] = pretty_lines
131+
origin_lines[first_line + 1 : last_line - 1] = pretty_lines
123132

124-
with open(origin_path, "w") as f:
125-
f.write("\n".join(origin_lines))
133+
with open(origin_path, "w") as f:
134+
f.write("\n".join(origin_lines))
135+
except FileNotFoundError:
136+
user_error(
137+
f"Couldn't find the file '{snippet_path}'. Run --extract to extract the inline code."
138+
)
139+
except IOError:
140+
user_error(f"Couldn't open '{origin_path}' or '{snippet_path}'")
126141

127142
return offset
128143

@@ -133,10 +148,16 @@ def autoformat(_origin_path, snippet_path, language, _first_line, _last_line):
133148
case "json":
134149
try:
135150
pretty_file(snippet_path)
136-
except:
137-
print(
138-
f"[error] Couldn't find the file '{snippet_path}'. Run --extract to extract the inline code."
151+
except FileNotFoundError:
152+
user_error(
153+
f"Couldn't find the file '{snippet_path}'. Run --extract to extract the inline code."
139154
)
155+
except PermissionError:
156+
user_error(f"Not enough permissions to open '{snippet_path}'")
157+
except IOError:
158+
user_error(f"Couldn't open '{snippet_path}'")
159+
except json.decoder.JSONDecodeError:
160+
user_error(f"Invalid json")
140161

141162

142163
def parse_args():
@@ -197,23 +218,20 @@ def parse_args():
197218
args = parse_args()
198219

199220
if not os.path.exists(args.path):
200-
print("[error] This path doesn't exist")
201-
sys.exit(-1)
221+
user_error("This path doesn't exist")
202222

203223
if (
204224
args.syntax_check
205225
and "cf3" in args.languages
206226
and not which("/var/cfengine/bin/cf-promises")
207227
):
208-
print("[error] cf-promises is not installed")
209-
sys.exit(-1)
228+
user_error("cf-promises is not installed")
210229

211230
for language in args.languages:
212231
if language not in supported_languages:
213-
print(
214-
f"[error] Unsupported language '{language}'. The supported languages are: {", ".join(supported_languages.keys())}"
232+
user_error(
233+
f"Unsupported language '{language}'. The supported languages are: {", ".join(supported_languages.keys())}"
215234
)
216-
sys.exit(-1)
217235

218236
parsed_markdowns = get_markdown_files(args.path, args.languages)
219237

0 commit comments

Comments
 (0)