Move the check for program availability into class
We also actually execute it now. Go us!
This commit is contained in:
@@ -16,6 +16,7 @@
|
|||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import shlex
|
import shlex
|
||||||
|
import logging
|
||||||
|
|
||||||
class Optimizer:
|
class Optimizer:
|
||||||
binaryname = ""
|
binaryname = ""
|
||||||
@@ -29,6 +30,17 @@ class Optimizer:
|
|||||||
def fire_and_forget(self, args):
|
def fire_and_forget(self, args):
|
||||||
subprocess.Popen(args, stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0]
|
subprocess.Popen(args, stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0]
|
||||||
|
|
||||||
|
def check_availability(self):
|
||||||
|
path = os.environ.get("PATH").split(os.pathsep)
|
||||||
|
|
||||||
|
def exists_in_path(prog):
|
||||||
|
result = filter(lambda x: os.path.exists(os.path.join(x, prog)), path)
|
||||||
|
return len(result) != 0
|
||||||
|
|
||||||
|
if (not exists_in_path(self.binaryname)) and (not exists_in_path(self.binaryname + ".exe")):
|
||||||
|
raise Exception("Optimization program '%s' was not found!" % self.binaryname)
|
||||||
|
|
||||||
|
|
||||||
class NonAtomicOptimizer(Optimizer):
|
class NonAtomicOptimizer(Optimizer):
|
||||||
def cleanup(self, img):
|
def cleanup(self, img):
|
||||||
os.rename(img + ".tmp", img)
|
os.rename(img + ".tmp", img)
|
||||||
@@ -91,17 +103,6 @@ class optipng(Optimizer, PNGOptimizer):
|
|||||||
Optimizer.fire_and_forget(self, [self.binaryname, "-o" + str(self.olevel), "-quiet", img])
|
Optimizer.fire_and_forget(self, [self.binaryname, "-o" + str(self.olevel), "-quiet", img])
|
||||||
|
|
||||||
|
|
||||||
def check_programs(optimizers):
|
|
||||||
path = os.environ.get("PATH").split(os.pathsep)
|
|
||||||
|
|
||||||
def exists_in_path(prog):
|
|
||||||
result = filter(lambda x: os.path.exists(os.path.join(x, prog)), path)
|
|
||||||
return len(result) != 0
|
|
||||||
|
|
||||||
for opt in optimizers:
|
|
||||||
if (not exists_in_path(opt.binaryname)) and (not exists_in_path(opt.binaryname + ".exe")):
|
|
||||||
raise Exception("Optimization program '%s' was not found!" % opt.binaryname)
|
|
||||||
|
|
||||||
def optimize_image(imgpath, imgformat, optimizers):
|
def optimize_image(imgpath, imgformat, optimizers):
|
||||||
for opt in optimizers:
|
for opt in optimizers:
|
||||||
if imgformat == 'png':
|
if imgformat == 'png':
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import rendermodes
|
|||||||
import util
|
import util
|
||||||
from optimizeimages import Optimizer
|
from optimizeimages import Optimizer
|
||||||
from world import UPPER_LEFT, UPPER_RIGHT, LOWER_LEFT, LOWER_RIGHT
|
from world import UPPER_LEFT, UPPER_RIGHT, LOWER_LEFT, LOWER_RIGHT
|
||||||
|
import logging
|
||||||
|
|
||||||
class ValidationException(Exception):
|
class ValidationException(Exception):
|
||||||
pass
|
pass
|
||||||
@@ -159,7 +160,6 @@ def validateBGColor(color):
|
|||||||
def validateOptImg(optimizers):
|
def validateOptImg(optimizers):
|
||||||
if isinstance(optimizers, (int, long)):
|
if isinstance(optimizers, (int, long)):
|
||||||
from optimizeimages import pngcrush
|
from optimizeimages import pngcrush
|
||||||
import logging
|
|
||||||
logging.warning("You're using a deprecated definition of optimizeimg. We'll do what you say for now, but please fix this as soon as possible.")
|
logging.warning("You're using a deprecated definition of optimizeimg. We'll do what you say for now, but please fix this as soon as possible.")
|
||||||
optimizers = [pngcrush()]
|
optimizers = [pngcrush()]
|
||||||
if not isinstance(optimizers, list):
|
if not isinstance(optimizers, list):
|
||||||
@@ -167,6 +167,9 @@ def validateOptImg(optimizers):
|
|||||||
for opt in optimizers:
|
for opt in optimizers:
|
||||||
if not isinstance(opt, Optimizer):
|
if not isinstance(opt, Optimizer):
|
||||||
raise ValidationException("Invalid Optimizer!")
|
raise ValidationException("Invalid Optimizer!")
|
||||||
|
|
||||||
|
opt.check_availability()
|
||||||
|
|
||||||
return optimizers
|
return optimizers
|
||||||
|
|
||||||
def validateTexturePath(path):
|
def validateTexturePath(path):
|
||||||
|
|||||||
Reference in New Issue
Block a user