diff --git a/gmap.py b/gmap.py index 512a25a..ec0719d 100755 --- a/gmap.py +++ b/gmap.py @@ -27,6 +27,7 @@ import re import multiprocessing import time import logging +import optimizeimages logging.basicConfig(level=logging.INFO,format="%(asctime)s [%(levelname)s] %(message)s") @@ -103,7 +104,8 @@ def main(): imgformat = 'png' if options.optimizeimg: - optimizeimg = options.optimizeimg + optimizeimg = int(options.optimizeimg) + optimizeimages.check_programs(optimizeimg) else: optimizeimg = None diff --git a/optimizeimages.py b/optimizeimages.py index 120b60b..7bb35a3 100644 --- a/optimizeimages.py +++ b/optimizeimages.py @@ -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]