def create_zip_files(self, dirs): """Create zip files.""" logmsg.header('Creating zip files...', self.logger) # Create output directory to hold our zips output_dir = 'Zips' if not os.path.exists(output_dir): os.mkdir(output_dir) num_zips = 0 # Create a progress bar pbar = ProgressBar(term_width=80, maxval=len(dirs)).start() for d in dirs: self.logger.debug('Zipping: "%s"' % d) parent_dir = os.path.join(os.path.dirname(d).split(os.path.sep)[-1], '') if parent_dir == self.input_dir: parent_dir = '' output_file = os.path.join(output_dir, parent_dir, os.path.basename(d)) shutil.make_archive(output_file, format="zip", root_dir=d) num_zips += 1 # Update progress bar pbar.update(num_zips) # Ensure progress bar is finished pbar.finish() time_elapsed = "(Time Elapsed: {0})".format(secs_to_mins(pbar.seconds_elapsed)) logmsg.success('Created {0} zip files {1}'.format(num_zips, time_elapsed), self.logger)
def png_crush(self, files): """Crush all PNG files provided.""" logmsg.header('Crushing PNGs...', self.logger) num_files = 0 processed_files = 0 # create a progress bar pbar = ProgressBar(term_width=80, maxval=len(files)).start() for f in files: if os.path.isfile(f): # crush png cmd = 'pngquant --quality={0} --skip-if-larger --ext .png --force "{1}"'.format(self.png_quality, f) self.logger.debug(cmd) proc = Popen(shlex.split(cmd), stdout=PIPE, stderr=PIPE) stdout, stderr = proc.communicate() status = not bool(proc.returncode) if status: processed_files += 1 # update progress bar pbar.update(num_files) num_files += 1 # ensure progress bar is finished pbar.finish() time_elapsed = "(Time Elapsed: {0})".format(secs_to_mins(pbar.seconds_elapsed)) result = 'Crushed {0} of {1} files {2}'.format(processed_files, len(files), time_elapsed) logmsg.success(result, self.logger)