0

Check for the optimization programs at the start of execution.

This prevents the annoying case where MCO will fail hours into a run if
pngcrush and friends are missing.
This commit is contained in:
Andrew Chin
2010-12-11 19:44:59 -05:00
parent cebbf4b1b4
commit 97c58212c3
2 changed files with 21 additions and 6 deletions

View File

@@ -17,20 +17,33 @@ import os
import subprocess
import shlex
pngcrush = "pngcrush"
optipng = "optipng"
advdef = "advdef"
def check_programs(level):
path = os.environ.get("PATH").split(os.pathsep)
for prog,l in [(pngcrush,1), (optipng,2), (advdef,2)]:
if l <= level:
result = filter(lambda x: os.path.exists(os.path.join(x, prog)), path)
if len(result) == 0:
raise Exception("Optimization prog %s for level %d not found!" % (prog, l))
def optimize_image(imgpath, imgformat, optimizeimg):
if imgformat == 'png':
if optimizeimg == "1" or optimizeimg == "2":
if optimizeimg >= 1:
# we can't do an atomic replace here because windows is terrible
# so instead, we make temp files, delete the old ones, and rename
# the temp files. go windows!
subprocess.Popen(shlex.split("pngcrush " + imgpath + " " + imgpath + ".tmp"),
subprocess.Popen(shlex.split(pngcrush +" " + imgpath + " " + imgpath + ".tmp"),
stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0]
os.remove(imgpath)
os.rename(imgpath+".tmp", imgpath)
if optimizeimg == "2":
subprocess.Popen(shlex.split("optipng " + imgpath), stderr=subprocess.STDOUT,
if optimizeimg >= 2:
subprocess.Popen(shlex.split(optipng + " " + imgpath), stderr=subprocess.STDOUT,
stdout=subprocess.PIPE).communicate()[0]
subprocess.Popen(shlex.split("advdef -z4 " + imgpath), stderr=subprocess.STDOUT,
subprocess.Popen(shlex.split(advdef + " -z4 " + imgpath), stderr=subprocess.STDOUT,
stdout=subprocess.PIPE).communicate()[0]