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 generate_html(self): """ Loop through all files in the input directory and create an HTML preview page for the swf and static version. """ num_files = 0 # Loop through all SWF files in the input directory files = [f for f in os.listdir(self.input_dir) if f.endswith('.swf')] for filen in files: filepath = os.path.join(self.input_dir, filen) if os.path.isfile(filepath): # get filename and extension basename = os.path.basename(filepath) name = os.path.splitext(basename)[0] # create a filename filename = os.path.join(self.input_dir, name+'.html') # do not overwrite existing HTML files (that would be bad) if os.path.isfile(filename): if self.verbose: basen = os.path.basename(filename) logmsg.warning('"{0}" already exists'.format(basen)) continue self.create_html(filename) num_files += 1 logmsg.success('Generated {0} HTML files'.format(num_files))
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)
def run(self): """Run script.""" config = self.get_config() args = self.get_parser() if args.log: self.create_logger() self.logger.debug('-' * 10) # Set some vars self.input_dir = os.path.join(config.get('html5', 'input')).rstrip('/') self.user = config.get('upload', 'user') self.ip = config.get('upload', 'ip') self.remote_dir = config.get('upload', 'remote_dir') self.url = config.get('upload', 'url') self.exclude_list = self.create_list(config.get('upload', 'exclude_list')) # Do the stuff we came here to do # Check if the input dir exists if not os.path.isdir(self.input_dir): logmsg.error('"{0}" does nots exist'.format(self.input_dir)) sys.exit() # Upload preview files self.upload() # Get links links = self.get_links() logmsg.success('%s ads uploaded' % len(links), self.logger) # Ask question if args.browser and logmsg.confirm('Open all ads in browser'): for l in links: webbrowser.open(l)
def run(self): """Run script.""" config = self.get_config() args = self.get_parser() if args.log: self.create_logger() self.logger.debug('-' * 10) self.input_dir = config.get('html5', 'input') self.images_dir = config.get('css', 'images_dir') self.style_dir = config.get('css', 'style_dir') self.ignore_list = self.create_list(config.get('css', 'exclude_list')) # Do the stuff we came here to do timer = Timer().start() dirs = self.find_ad_dirs() for d in dirs: # TODO: Trim needs work...screws up our pngs # self.trim_pngs(d) self.generate_css(d) logmsg.success('CSS Generated (Elapsed time: %s)' % timer.stop().elapsed())
def run(self): """Run script.""" # config = self.get_config() args = self.get_parser() if args.log: self.create_logger() self.logger.debug('-' * 10) # Do the stuff we came here to do self.copy_files() logmsg.success('DONE!')
def image_optim(self, files): """Optimize images using ImageOptim GUI. Very time intensive.""" logmsg.header('Running ImageOptim GUI on all images...', self.logger) logmsg.debug('Unable to provide progress for the GUI app. This can take a while...') timer = Timer().start() # start_time = time.time() cmd = '/Applications/ImageOptim.app/Contents/MacOS/ImageOptim %s' % (' '.join(files)) # print(cmd) proc = Popen(shlex.split(cmd), stdout=PIPE, stderr=PIPE) stdout, stderr = proc.communicate() status = not bool(proc.returncode) if status: # end_time = time.time() # logmsg.success('Time Elapsed: {0}'.format(end_time - start_time)) logmsg.success('Optimized {0} images (Time Elapsed: {1})'.format(len(files), timer.stop().elapsed())) else: logmsg.error(stderr.strip())
def upload(self): """Upload HTML5 ads files.""" logmsg.header('Uploading HTML5 ad files...', self.logger) timer = Timer().start() exclude = self.create_rsync_exclude() cmd = 'rsync -avzhP{exclude} "{from_dir}" {user}@{ip}:{to_dir}'.format( exclude=exclude, from_dir=self.input_dir, user=self.user, ip=self.ip, to_dir=self.remote_dir) self.logger.debug(cmd) proc = Popen(shlex.split(cmd), stdout=PIPE, stderr=PIPE) stdout, stderr = proc.communicate() status = not bool(proc.returncode) if status: logmsg.success('Uploaded HTML ad files (Time Elapsed: {0})'.format( timer.stop().elapsed()), self.logger)
def run(self): """Run script.""" config = self.get_config() args = self.get_parser() if args.log: self.create_logger() self.logger.debug('-' * 10) self.input_dir = config.get('html5', 'input') # Check if the input dir exists if not os.path.isdir(self.input_dir): logmsg.error('"{0}" does not exist'.format(self.input_dir)) sys.exit() # Do the stuff we came here to do timer = Timer().start() logmsg.success('HTML Generated (Elapsed time: %s)' % timer.stop().elapsed())
def generate_css(self, dirpath): """ Loop through all files in the input directory and create CSS styles for them. """ logmsg.header('Create CSS for ad "%s"' % os.path.basename(dirpath)) css = '' num_styles = 0 # ---------- # Try to parse existing CSS file images_dir = os.path.join(dirpath, self.images_dir) files = self.get_image_files(images_dir) css_dir = os.path.join(dirpath, self.style_dir) css_file = os.path.join(css_dir, 'style.css') if os.path.isfile(css_file): css_parser = cssutils.parseFile(css_file) # CSS file exists, update it # logmsg.info('CSS file exists, update it') # Loop through all rules for rule in css_parser.cssRules: if isinstance(rule, cssutils.css.CSSCharsetRule): css += rule.cssText + '\n' if isinstance(rule, cssutils.css.CSSComment): css += rule.cssText + '\n' if isinstance(rule, cssutils.css.CSSStyleRule): # If one of our images is used in the style, update it for filen in files: filepath = os.path.join(images_dir, filen) img = self.get_image_info(filepath) if img['basename'] in rule.style.background: files.remove(filen) # print('Updating style: %s' % rule.selectorText) rule.style.width = '%dpx' % img['width'] rule.style.height = '%dpx' % img['height'] num_styles += 1 css += rule.cssText + '\n\n' # num_styles += 1 # Write CSS to file # css_file = os.path.join(css_dir, 'style_new.css') with open(css_file, 'w') as tf: tf.write(css) logmsg.success('Updated {0} CSS styles'.format(num_styles)) return # ---------- # CSS file does not exists, create it # logmsg.info('CSS file does not exist, create it') self.create_base_css(dirpath, css_file) # Loop through all image files in the input directory for filen in files: filepath = os.path.join(images_dir, filen) img = self.get_image_info(filepath) # Create a new rule rule = cssutils.css.CSSStyleRule() rule.selectorText = '#%s' % img['name'] rule.style.background = 'url(%s) no-repeat' % img['basename'] rule.style.width = '%dpx' % img['width'] rule.style.height = '%dpx' % img['height'] rule.style.position = 'absolute' rule.style.top = '0px' rule.style.left = '0px' if 'bg' not in img['basename']: rule.style.zIndex = '100' # print(rule.cssText) css += rule.cssText + '\n\n' num_styles += 1 # Write CSS to file # css_file = os.path.join(css_dir, 'style_new.css') with open(css_file, 'a') as tf: tf.write(css) logmsg.success('Generated {0} CSS styles'.format(num_styles))