1
1
from cfbs .pretty import pretty_file
2
+ from cfbs .utils import user_error
3
+ import json
2
4
from shutil import which
3
5
import markdown_it
4
6
import os
5
7
import argparse
6
- import sys
7
8
import subprocess
8
9
9
10
@@ -66,36 +67,47 @@ def get_markdown_files(start, languages):
66
67
67
68
def extract (origin_path , snippet_path , _language , first_line , last_line ):
68
69
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 ()
71
73
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 ])
73
75
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 } '" )
76
80
77
81
78
82
def check_syntax (origin_path , snippet_path , language , first_line , _last_line ):
79
83
snippet_abs_path = os .path .abspath (snippet_path )
80
84
81
85
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."
84
88
)
85
- return
86
89
87
90
match language :
88
91
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
95
99
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" )
99
111
100
112
101
113
def check_output ():
@@ -107,22 +119,25 @@ def replace(origin_path, snippet_path, _language, first_line, last_line):
107
119
try :
108
120
with open (snippet_path , "r" ) as f :
109
121
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
115
122
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 " )
119
126
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
+ )
121
130
122
- origin_lines [first_line + 1 : last_line - 1 ] = pretty_lines
131
+ origin_lines [first_line + 1 : last_line - 1 ] = pretty_lines
123
132
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 } '" )
126
141
127
142
return offset
128
143
@@ -133,10 +148,16 @@ def autoformat(_origin_path, snippet_path, language, _first_line, _last_line):
133
148
case "json" :
134
149
try :
135
150
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."
139
154
)
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" )
140
161
141
162
142
163
def parse_args ():
@@ -197,23 +218,20 @@ def parse_args():
197
218
args = parse_args ()
198
219
199
220
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" )
202
222
203
223
if (
204
224
args .syntax_check
205
225
and "cf3" in args .languages
206
226
and not which ("/var/cfengine/bin/cf-promises" )
207
227
):
208
- print ("[error] cf-promises is not installed" )
209
- sys .exit (- 1 )
228
+ user_error ("cf-promises is not installed" )
210
229
211
230
for language in args .languages :
212
231
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 ())} "
215
234
)
216
- sys .exit (- 1 )
217
235
218
236
parsed_markdowns = get_markdown_files (args .path , args .languages )
219
237
0 commit comments