|
4 | 4 |
|
5 | 5 | import logging |
6 | 6 | import os |
| 7 | +import shutil |
7 | 8 | import subprocess |
8 | 9 | import sys |
9 | 10 | import tempfile |
@@ -191,9 +192,12 @@ def open_withImagemagick(self, f): |
191 | 192 | raise OSError() |
192 | 193 |
|
193 | 194 | outfile = self.tempfile("temp.png") |
194 | | - if command_succeeds([IMCONVERT, f, outfile]): |
195 | | - return Image.open(outfile) |
196 | | - raise OSError() |
| 195 | + rc = subprocess.call( |
| 196 | + [IMCONVERT, f, outfile], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT |
| 197 | + ) |
| 198 | + if rc: |
| 199 | + raise OSError |
| 200 | + return Image.open(outfile) |
197 | 201 |
|
198 | 202 |
|
199 | 203 | @unittest.skipIf(sys.platform.startswith("win32"), "requires Unix or macOS") |
@@ -268,34 +272,20 @@ def hopper(mode=None, cache={}): |
268 | 272 | return im.copy() |
269 | 273 |
|
270 | 274 |
|
271 | | -def command_succeeds(cmd): |
272 | | - """ |
273 | | - Runs the command, which must be a list of strings. Returns True if the |
274 | | - command succeeds, or False if an OSError was raised by subprocess.Popen. |
275 | | - """ |
276 | | - try: |
277 | | - subprocess.call(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) |
278 | | - except OSError: |
279 | | - return False |
280 | | - return True |
281 | | - |
282 | | - |
283 | 275 | def djpeg_available(): |
284 | | - return command_succeeds(["djpeg", "-version"]) |
| 276 | + return bool(shutil.which("djpeg")) |
285 | 277 |
|
286 | 278 |
|
287 | 279 | def cjpeg_available(): |
288 | | - return command_succeeds(["cjpeg", "-version"]) |
| 280 | + return bool(shutil.which("cjpeg")) |
289 | 281 |
|
290 | 282 |
|
291 | 283 | def netpbm_available(): |
292 | | - return command_succeeds(["ppmquant", "--version"]) and command_succeeds( |
293 | | - ["ppmtogif", "--version"] |
294 | | - ) |
| 284 | + return bool(shutil.which("ppmquant") and shutil.which("ppmtogif")) |
295 | 285 |
|
296 | 286 |
|
297 | 287 | def imagemagick_available(): |
298 | | - return IMCONVERT and command_succeeds([IMCONVERT, "-version"]) |
| 288 | + return bool(IMCONVERT and shutil.which(IMCONVERT)) |
299 | 289 |
|
300 | 290 |
|
301 | 291 | def on_appveyor(): |
|
0 commit comments