@@ -83,13 +83,14 @@ def forward_output(
83
83
84
84
def batch_run (
85
85
args : Iterable [str ],
86
- * images : str ,
86
+ * images : str | Path ,
87
87
batchsize : int = 64 ,
88
88
post_args : list [str ] = [],
89
89
check : bool = True ,
90
90
trim_printout : Optional [Callable [[str ], Optional [str ]]] = None ,
91
91
stdout_write : Optional [Callable ] = None ,
92
92
stderr_write : Optional [Callable ] = None ,
93
+ wrap_errs : Optional [list [Exception ]] = None ,
93
94
** kwargs ,
94
95
):
95
96
if len (images ) > batchsize :
@@ -100,13 +101,14 @@ def batch_run(
100
101
batchsize = batchsize ,
101
102
post_args = post_args ,
102
103
check = check ,
104
+ wrap_errs = wrap_errs ,
103
105
** kwargs ,
104
106
)
105
107
for i in range (0 , len (images ), batchsize )
106
108
]
107
109
108
110
process = subprocess .Popen (
109
- [* args , * images , * post_args ],
111
+ [* args , * map ( str , images ) , * post_args ],
110
112
stdout = subprocess .PIPE ,
111
113
stderr = subprocess .PIPE ,
112
114
** kwargs ,
@@ -133,7 +135,12 @@ def batch_run(
133
135
process .wait ()
134
136
135
137
if check and process .returncode != 0 :
136
- raise subprocess .CalledProcessError (process .returncode , process .args )
138
+ exn = subprocess .CalledProcessError (process .returncode , process .args )
139
+ if wrap_errs is not None :
140
+ stderr_write (f"Error: { exn } " )
141
+ wrap_errs .append (exn )
142
+ else :
143
+ raise exn
137
144
138
145
return process
139
146
@@ -154,6 +161,7 @@ def ect(
154
161
trim_printout : bool = False ,
155
162
stdout_write : Optional [Callable ] = None ,
156
163
stderr_write : Optional [Callable ] = None ,
164
+ wrap_errs : Optional [list [Exception ]] = None ,
157
165
):
158
166
if not images :
159
167
return
@@ -173,6 +181,7 @@ def ect(
173
181
trim_printout = trim_ect if trim_printout else (lambda x : x ),
174
182
stdout_write = stdout_write ,
175
183
stderr_write = stderr_write ,
184
+ wrap_errs = wrap_errs ,
176
185
)
177
186
178
187
@@ -195,6 +204,7 @@ def optipng(
195
204
trim_printout : bool = False ,
196
205
stdout_write : Optional [Callable ] = None ,
197
206
stderr_write : Optional [Callable ] = None ,
207
+ wrap_errs : Optional [list [Exception ]] = None ,
198
208
):
199
209
if not images :
200
210
return
@@ -210,6 +220,7 @@ def optipng(
210
220
trim_printout = trim_optipng if trim_printout else None ,
211
221
stdout_write = stdout_write ,
212
222
stderr_write = stderr_write ,
223
+ wrap_errs = wrap_errs ,
213
224
)
214
225
215
226
@@ -225,6 +236,7 @@ def pngcrush(
225
236
trim_printout : bool = False ,
226
237
stdout_write : Optional [Callable ] = None ,
227
238
stderr_write : Optional [Callable ] = None ,
239
+ wrap_errs : Optional [list [Exception ]] = None ,
228
240
):
229
241
if not images :
230
242
return
@@ -259,6 +271,7 @@ def pngcrush(
259
271
trim_printout = trim_pngcrush if trim_printout else None ,
260
272
stdout_write = stdout_write ,
261
273
stderr_write = stderr_write ,
274
+ wrap_errs = wrap_errs ,
262
275
)
263
276
264
277
# Replace original images with crushed images if they are smaller
@@ -283,6 +296,7 @@ def optimize(
283
296
tqdm_leave : Optional [bool ] = None ,
284
297
stdout_write : Optional [Callable ] = None ,
285
298
stderr_write : Optional [Callable ] = None ,
299
+ wrap_errs : Optional [list [Exception ]] = None ,
286
300
):
287
301
cur_images = images
288
302
cur_sizes = [Path (image ).stat ().st_size for image in cur_images ]
@@ -297,13 +311,15 @@ def optimize(
297
311
trim_printout = trim_printout ,
298
312
stdout_write = partial (tqdm .write , file = sys .stdout ),
299
313
stderr_write = partial (tqdm .write , file = sys .stderr ),
314
+ wrap_errs = wrap_errs ,
300
315
)
301
316
optipng (
302
317
* cur_images ,
303
318
exhaustive = exhaustive ,
304
319
trim_printout = trim_printout ,
305
320
stdout_write = stdout_write ,
306
321
stderr_write = stderr_write ,
322
+ wrap_errs = wrap_errs ,
307
323
)
308
324
pngcrush (
309
325
* cur_images ,
@@ -313,6 +329,7 @@ def optimize(
313
329
trim_printout = trim_printout ,
314
330
stdout_write = stdout_write ,
315
331
stderr_write = stderr_write ,
332
+ wrap_errs = wrap_errs ,
316
333
)
317
334
new_sizes = [Path (image ).stat ().st_size for image in cur_images ]
318
335
cur_images = [
0 commit comments