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:
4
gmap.py
4
gmap.py
@@ -27,6 +27,7 @@ import re
|
|||||||
import multiprocessing
|
import multiprocessing
|
||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
|
import optimizeimages
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO,format="%(asctime)s [%(levelname)s] %(message)s")
|
logging.basicConfig(level=logging.INFO,format="%(asctime)s [%(levelname)s] %(message)s")
|
||||||
|
|
||||||
@@ -103,7 +104,8 @@ def main():
|
|||||||
imgformat = 'png'
|
imgformat = 'png'
|
||||||
|
|
||||||
if options.optimizeimg:
|
if options.optimizeimg:
|
||||||
optimizeimg = options.optimizeimg
|
optimizeimg = int(options.optimizeimg)
|
||||||
|
optimizeimages.check_programs(optimizeimg)
|
||||||
else:
|
else:
|
||||||
optimizeimg = None
|
optimizeimg = None
|
||||||
|
|
||||||
|
|||||||
@@ -17,20 +17,33 @@ import os
|
|||||||
import subprocess
|
import subprocess
|
||||||
import shlex
|
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):
|
def optimize_image(imgpath, imgformat, optimizeimg):
|
||||||
if imgformat == 'png':
|
if imgformat == 'png':
|
||||||
if optimizeimg == "1" or optimizeimg == "2":
|
if optimizeimg >= 1:
|
||||||
# we can't do an atomic replace here because windows is terrible
|
# we can't do an atomic replace here because windows is terrible
|
||||||
# so instead, we make temp files, delete the old ones, and rename
|
# so instead, we make temp files, delete the old ones, and rename
|
||||||
# the temp files. go windows!
|
# 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]
|
stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0]
|
||||||
os.remove(imgpath)
|
os.remove(imgpath)
|
||||||
os.rename(imgpath+".tmp", imgpath)
|
os.rename(imgpath+".tmp", imgpath)
|
||||||
|
|
||||||
if optimizeimg == "2":
|
if optimizeimg >= 2:
|
||||||
subprocess.Popen(shlex.split("optipng " + imgpath), stderr=subprocess.STDOUT,
|
subprocess.Popen(shlex.split(optipng + " " + imgpath), stderr=subprocess.STDOUT,
|
||||||
stdout=subprocess.PIPE).communicate()[0]
|
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]
|
stdout=subprocess.PIPE).communicate()[0]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user