def test_smush_dir_recursive (self): smush = Smush(strip_jpg_meta=False, list_only=False, quiet=True, exclude='.bzr,.git,.hg,.svn,.DS_Store') smush.process(self.working_dir, True) for each_file in self.working_files: src_size = os.path.getsize(os.path.join(materials_dir, each_file)) dest_size = os.path.getsize(os.path.join(self.working_dir, each_file)) self.assertTrue(src_size > dest_size) return True
def test_smush_dir_recursive(self): smush = Smush(strip_jpg_meta=False, list_only=False, quiet=True, exclude='.bzr,.git,.hg,.svn,.DS_Store') smush.process(self.working_dir, True) for each_file in self.working_files: src_size = os.path.getsize(os.path.join(materials_dir, each_file)) dest_size = os.path.getsize( os.path.join(self.working_dir, each_file)) self.assertTrue(src_size > dest_size) return True
def test_smush_file (self): smushing_path = os.path.join(self.working_dir, filename_gif) smush = Smush(strip_jpg_meta=False, list_only=False, quiet=True, exclude='.bzr,.git,.hg,.svn,.DS_Store') smush.process(smushing_path, False) for each_file in self.working_files: if each_file == filename_gif: src_size = os.path.getsize(os.path.join(materials_dir, each_file)) dest_size = os.path.getsize(smushing_path) self.assertTrue(src_size > dest_size) else: src_size = os.path.getsize(os.path.join(materials_dir, each_file)) dest_size = os.path.getsize(os.path.join(self.working_dir, each_file)) self.assertTrue(src_size == dest_size) return True
def test_smush_file(self): smushing_path = os.path.join(self.working_dir, filename_gif) smush = Smush(strip_jpg_meta=False, list_only=False, quiet=True, exclude='.bzr,.git,.hg,.svn,.DS_Store') smush.process(smushing_path, False) for each_file in self.working_files: if each_file == filename_gif: src_size = os.path.getsize( os.path.join(materials_dir, each_file)) dest_size = os.path.getsize(smushing_path) self.assertTrue(src_size > dest_size) else: src_size = os.path.getsize( os.path.join(materials_dir, each_file)) dest_size = os.path.getsize( os.path.join(self.working_dir, each_file)) self.assertTrue(src_size == dest_size) return True
''' A wrapper around the smush.py library the library itself has been modifed to make it work with the binaries included in most package managers (to custom patches + recompiling ''' from config import STRIP_JPG_META from utils import file_sig from smush import Smush from path import path smusher = Smush(strip_jpg_meta=STRIP_JPG_META, list_only=False, quiet=True, identify_mime=True) optimize_image = smusher.smush def optimize_with_touch(pth, smusher=smusher): ''' dirty, but we need to guarantee that the file gets touched to make sure watchdog fires an event when the file gets optimized ''' pth = path(pth).abspath() sig = file_sig(pth) optimize_image(pth) if file_sig(pth) == sig: # not modified, force it ourselves pth.touch()
def main(): try: opts, args = getopt.getopt(sys.argv[1:], 'hrqs', [ 'help', 'recursive', 'quiet', 'strip-meta', 'exclude=', 'list-only', 'identify-mime' ]) except getopt.GetoptError: usage() sys.exit(2) if len(args) == 0: usage() sys.exit() recursive = False quiet = False strip_jpg_meta = False exclude = ['.bzr', '.git', '.hg', '.svn'] list_only = False identify_mime = False for opt, arg in opts: if opt in ('-h', '--help'): usage() sys.exit() elif opt in ('-r', '--recursive'): recursive = True elif opt in ('-q', '--quiet'): quiet = True elif opt in ('-s', '--strip-meta'): strip_jpg_meta = True elif opt in ('--identify-mime'): identify_mime = True elif opt in ('--exclude'): exclude.extend(arg.strip().split(',')) elif opt in ('--list-only'): list_only = True # quiet = True else: # unsupported option given usage() sys.exit(2) if quiet == True: logging.basicConfig(level=logging.WARNING, format='%(asctime)s %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S') else: logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S') smush = Smush(strip_jpg_meta=strip_jpg_meta, exclude=exclude, list_only=list_only, quiet=quiet, identify_mime=identify_mime) for arg in args: try: smush.process(arg, recursive) logging.info('\nSmushing Finished') except KeyboardInterrupt: logging.info('\nSmushing aborted') result = smush.stats() if list_only and len(result['modified']) > 0: logging.error(result['output']) sys.exit(1) print result['output'] sys.exit(0)
def main(): try: opts, args = getopt.getopt(sys.argv[1:], 'hrqs', ['help', 'recursive', 'quiet', 'strip-meta', 'exclude=', 'list-only' ,'identify-mime']) except getopt.GetoptError: usage() sys.exit(2) if len(args) == 0: usage() sys.exit() recursive = False quiet = False strip_jpg_meta = False exclude = ['.bzr', '.git', '.hg', '.svn'] list_only = False identify_mime = False for opt, arg in opts: if opt in ('-h', '--help'): usage() sys.exit() elif opt in ('-r', '--recursive'): recursive = True elif opt in ('-q', '--quiet'): quiet = True elif opt in ('-s', '--strip-meta'): strip_jpg_meta = True elif opt in ('--identify-mime'): identify_mime = True elif opt in ('--exclude'): exclude.extend(arg.strip().split(',')) elif opt in ('--list-only'): list_only = True # quiet = True else: # unsupported option given usage() sys.exit(2) if quiet == True: logging.basicConfig( level=logging.WARNING, format='%(asctime)s %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S') else: logging.basicConfig( level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S') smush = Smush(strip_jpg_meta=strip_jpg_meta, exclude=exclude, list_only=list_only, quiet=quiet, identify_mime=identify_mime) for arg in args: try: smush.process(arg, recursive) logging.info('\nSmushing Finished') except KeyboardInterrupt: logging.info('\nSmushing aborted') result = smush.stats() if list_only and len(result['modified']) > 0: logging.error(result['output']) sys.exit(1) print result['output'] sys.exit(0)