mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-25 17:36:18 +03:00
Merge pull request #4420 from jdufresne/which
Simplify command discovery with stdlib shutil.which()
This commit is contained in:
commit
9174a4596c
|
@ -4,6 +4,7 @@ Helper functions.
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
|
@ -191,9 +192,12 @@ class PillowTestCase(unittest.TestCase):
|
||||||
raise OSError()
|
raise OSError()
|
||||||
|
|
||||||
outfile = self.tempfile("temp.png")
|
outfile = self.tempfile("temp.png")
|
||||||
if command_succeeds([IMCONVERT, f, outfile]):
|
rc = subprocess.call(
|
||||||
return Image.open(outfile)
|
[IMCONVERT, f, outfile], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT
|
||||||
raise OSError()
|
)
|
||||||
|
if rc:
|
||||||
|
raise OSError
|
||||||
|
return Image.open(outfile)
|
||||||
|
|
||||||
|
|
||||||
@unittest.skipIf(sys.platform.startswith("win32"), "requires Unix or macOS")
|
@unittest.skipIf(sys.platform.startswith("win32"), "requires Unix or macOS")
|
||||||
|
@ -268,34 +272,20 @@ def hopper(mode=None, cache={}):
|
||||||
return im.copy()
|
return im.copy()
|
||||||
|
|
||||||
|
|
||||||
def command_succeeds(cmd):
|
|
||||||
"""
|
|
||||||
Runs the command, which must be a list of strings. Returns True if the
|
|
||||||
command succeeds, or False if an OSError was raised by subprocess.Popen.
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
subprocess.call(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
|
|
||||||
except OSError:
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def djpeg_available():
|
def djpeg_available():
|
||||||
return command_succeeds(["djpeg", "-version"])
|
return bool(shutil.which("djpeg"))
|
||||||
|
|
||||||
|
|
||||||
def cjpeg_available():
|
def cjpeg_available():
|
||||||
return command_succeeds(["cjpeg", "-version"])
|
return bool(shutil.which("cjpeg"))
|
||||||
|
|
||||||
|
|
||||||
def netpbm_available():
|
def netpbm_available():
|
||||||
return command_succeeds(["ppmquant", "--version"]) and command_succeeds(
|
return bool(shutil.which("ppmquant") and shutil.which("ppmtogif"))
|
||||||
["ppmtogif", "--version"]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def imagemagick_available():
|
def imagemagick_available():
|
||||||
return IMCONVERT and command_succeeds([IMCONVERT, "-version"])
|
return bool(IMCONVERT and shutil.which(IMCONVERT))
|
||||||
|
|
||||||
|
|
||||||
def on_appveyor():
|
def on_appveyor():
|
||||||
|
|
Loading…
Reference in New Issue
Block a user