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)
def trim_pngs(self, dirpath): """Trim all PNG files of any extra transparency.""" logmsg.header('Trim PNGs...', self.logger) images_dir = os.path.join(dirpath, self.images_dir) files = self.get_image_files(images_dir, ['.png']) for f in files: filepath = os.path.join(images_dir, f) self.trim_png(filepath)
def get_links(self): """Get all links of the files we uploaded.""" logmsg.header('Generating links', self.logger) links = [] for root, dirnames, filenames in os.walk(self.input_dir): for name in fnmatch.filter(filenames, 'index.html'): url = '{0}{1}'.format(self.url, os.path.join(root, name)) links.append(url) print(url) return links
def copy_files(self): """Copy quickstart files from data folder to the CWD.""" logmsg.header('Copy quickstart files...', self.logger) # Loop through all non-hidden files in quickstart directory files = [f for f in os.listdir(self.get_data('quickstart')) if not f.startswith('.')] for filename in files: src = os.path.join(self.get_data('quickstart'), filename) dst = os.path.join(os.getcwd(), filename) if not os.path.isfile(filename): shutil.copy2(src, dst) logmsg.info('Copied "{0}"'.format(filename), self.logger) else: logmsg.warning('"{0}" already exists'.format(filename), self.logger)
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 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))