Send unwanted subprocess output to /dev/null

Avoids unnecessary creation of temporary files as well as unnecessarily
storing process output in memory. The temporary files would sometimes
remain and not be cleaned up.
This commit is contained in:
Jon Dufresne 2016-11-23 17:48:56 -08:00
parent 851aa210df
commit cecdb2af61
4 changed files with 18 additions and 22 deletions

View File

@ -22,6 +22,7 @@
import re import re
import io import io
import os
import sys import sys
from . import Image, ImageFile from . import Image, ImageFile
from ._binary import i32le as i32, o32le as o32 from ._binary import i32le as i32, o32le as o32
@ -57,8 +58,8 @@ def has_ghostscript():
if not sys.platform.startswith('win'): if not sys.platform.startswith('win'):
import subprocess import subprocess
try: try:
gs = subprocess.Popen(['gs', '--version'], stdout=subprocess.PIPE) with open(os.devnull, 'wb') as devnull:
gs.stdout.read() subprocess.check_call(['gs', '--version'], stdout=devnull)
return True return True
except OSError: except OSError:
# no ghostscript # no ghostscript
@ -137,12 +138,8 @@ def Ghostscript(tile, size, fp, scale=1):
# push data through ghostscript # push data through ghostscript
try: try:
gs = subprocess.Popen(command, stdin=subprocess.PIPE, with open(os.devnull, 'w+b') as devnull:
stdout=subprocess.PIPE) subprocess.check_call(command, stdin=devnull, stdout=devnull)
gs.stdin.close()
status = gs.wait()
if status:
raise IOError("gs failed (status %d)" % status)
im = Image.open(outfile) im = Image.open(outfile)
im.load() im.load()
finally: finally:

View File

@ -519,18 +519,17 @@ def _save_netpbm(im, fp, filename):
with open(filename, 'wb') as f: with open(filename, 'wb') as f:
if im.mode != "RGB": if im.mode != "RGB":
with tempfile.TemporaryFile() as stderr: with open(os.devnull, 'wb') as devnull:
check_call(["ppmtogif", file], stdout=f, stderr=stderr) check_call(["ppmtogif", file], stdout=f, stderr=devnull)
else: else:
# Pipe ppmquant output into ppmtogif # Pipe ppmquant output into ppmtogif
# "ppmquant 256 %s | ppmtogif > %s" % (file, filename) # "ppmquant 256 %s | ppmtogif > %s" % (file, filename)
quant_cmd = ["ppmquant", "256", file] quant_cmd = ["ppmquant", "256", file]
togif_cmd = ["ppmtogif"] togif_cmd = ["ppmtogif"]
with tempfile.TemporaryFile() as stderr: with open(os.devnull, 'wb') as devnull:
quant_proc = Popen(quant_cmd, stdout=PIPE, stderr=stderr) quant_proc = Popen(quant_cmd, stdout=PIPE, stderr=devnull)
with tempfile.TemporaryFile() as stderr:
togif_proc = Popen(togif_cmd, stdin=quant_proc.stdout, togif_proc = Popen(togif_cmd, stdin=quant_proc.stdout,
stdout=f, stderr=stderr) stdout=f, stderr=devnull)
# Allow ppmquant to receive SIGPIPE if ppmtogif exits # Allow ppmquant to receive SIGPIPE if ppmtogif exits
quant_proc.stdout.close() quant_proc.stdout.close()

View File

@ -329,8 +329,8 @@ def _save(im, fp, filename):
from subprocess import Popen, PIPE, CalledProcessError from subprocess import Popen, PIPE, CalledProcessError
convert_cmd = ["iconutil", "-c", "icns", "-o", filename, iconset] convert_cmd = ["iconutil", "-c", "icns", "-o", filename, iconset]
with tempfile.TemporaryFile() as stderr: with open(os.devnull, 'wb') as devnull:
convert_proc = Popen(convert_cmd, stdout=PIPE, stderr=stderr) convert_proc = Popen(convert_cmd, stdout=PIPE, stderr=devnull)
convert_proc.stdout.close() convert_proc.stdout.close()

View File

@ -218,25 +218,25 @@ def command_succeeds(cmd):
command succeeds, or False if an OSError was raised by subprocess.Popen. command succeeds, or False if an OSError was raised by subprocess.Popen.
""" """
import subprocess import subprocess
with open(os.devnull, 'w') as f: with open(os.devnull, 'wb') as f:
try: try:
subprocess.Popen(cmd, stdout=f, stderr=subprocess.STDOUT).wait() subprocess.call(cmd, stdout=f, stderr=subprocess.STDOUT)
except OSError: except OSError:
return False return False
return True return True
def djpeg_available(): def djpeg_available():
return command_succeeds(['djpeg', '--help']) return command_succeeds(['djpeg', '-version'])
def cjpeg_available(): def cjpeg_available():
return command_succeeds(['cjpeg', '--help']) return command_succeeds(['cjpeg', '-version'])
def netpbm_available(): def netpbm_available():
return (command_succeeds(["ppmquant", "--help"]) and return (command_succeeds(["ppmquant", "--version"]) and
command_succeeds(["ppmtogif", "--help"])) command_succeeds(["ppmtogif", "--version"]))
def imagemagick_available(): def imagemagick_available():