Commits

Etienne Perot committed d850cc5

Add support for crushing with both pngcrush and pngout when both are present

Comments (0)

Files changed (3)

 import os, sys
 import subprocess
 from callbackfs import CallbackSystem
-from crushfs import PNGCrusher_pngcrush, PNGCrusher_pngout, JPEGCrusher, programExists
+from crushfs import PNGCrusher_pngcrush, PNGCrusher_pngout, PNGCrusher_pngout_pngcrush, JPEGCrusher, programExists
 
 if len(sys.argv) < 2:
 	print('Usage:', sys.argv[0], 'file_or_directory_1', 'file_or_directory_2', '...', 'file_or_directory_n')
 
 callbackSys = CallbackSystem()
 enabled = False
-if programExists('pngout'):
+if programExists('pngout') and programExists('pngcrush'):
+	callbackSys.addCallback(r'(?<!\.crush)\.png$', PNGCrusher_pngout_pngcrush)
+	enabled = True
+elif programExists('pngout'):
 	callbackSys.addCallback(r'(?<!\.crush)\.png$', PNGCrusher_pngout)
 	enabled = True
 elif programExists('pngcrush'):
 	def write(self, data, offset):
 		self.hasBeenWritten = True
 	def crushSub(self):
-		return (subprocess.Popen(self.getArguments(), stdout=subprocess.PIPE, stderr=subprocess.PIPE).wait(), self.getCrushPath())
+		return subprocess.Popen(self.getArguments(), stdout=subprocess.PIPE, stderr=subprocess.PIPE).wait(), self.getCrushPath()
 	def crush(self, attempt=0):
 		print('Crushing', self.getPath())
 		if attempt > 5:
 		def getResult(self):
 			return self.result
 		def pngoutCrush(self, blockSize):
-			return 
+			return
 		def run(self):
 			shutil.copyfile(self.parent.getPath(), self.filename)
 			result = 0
 				os.remove(lowPath)
 			except:
 				pass
-			return (result, None)
+			return result, None
 		lowSize = os.path.getsize(lowPath)
 		highSize = os.path.getsize(highPath)
 		if lowSize < highSize:
 		else:
 			self.bestFile = highPath
 			os.remove(lowPath)
-		return (result, self.bestFile)
+		return result, self.bestFile
+
+class PNGCrusher_pngout_pngcrush(Crusher):
+	def __init__(self, *args, **kwargs):
+		super().__init__(*args, **kwargs)
+		self.pngout = PNGCrusher_pngout(*args, **kwargs)
+		self.pngcrush = PNGCrusher_pngcrush(*args, **kwargs)
+	def crushSub(self):
+		result1, bestFile1 = self.pngout.crushSub()
+		if result1:
+			try:
+				os.remove(self.getCrushPath())
+			except:
+				pass
+		else:
+			os.remove(self.getPath())
+			shutil.move(bestFile1, self.getPath())
+		return self.pngcrush.crushSub()
+
 
 class JPEGCrusher(Crusher):
 	arguments = ['jpegtran', '-optimize', '-copy', 'none', '-progressive', '-outfile']
 		if 'enqueue' in kwargs:
 			del kwargs['enqueue']
 		super().__init__(*args, **kwargs)
-		if programExists('pngout'):
+		if programExists('pngout') and programExists('pngcrush'):
+			self.addCallback(r'(?<!\.crush)\.png$', PNGCrusher_pngout_pngcrush)
+		elif programExists('pngout'):
 			self.addCallback(r'(?<!\.crush)\.png$', PNGCrusher_pngout)
 		elif programExists('pngcrush'):
 			self.addCallback(r'(?<!\.crush)\.png$', PNGCrusher_pngcrush)

loopbackfs.py

File contents unchanged.