def applicationcreated_subscriber(event): app = event.app app.registry.settings['assetmutator.mutators'] = mutators if app.registry.settings['assetmutator.mutated_path'] \ and app.registry.settings['assetmutator.purge_mutated_path']: path = get_abspath(app.registry.settings['assetmutator.mutated_path']) if os.path.isdir(path): for file in os.listdir(path): try: file_path = os.path.join(path, file) if os.path.isfile(file_path): os.unlink(file_path) except: pass if app.registry.settings['assetmutator.each_boot']: request = app.request_factory.blank('/') asset_paths = app.registry.settings['assetmutator.asset_paths'] for asset_path in asset_paths: mutant = Mutator(request, asset_path, registry=app.registry, batch=True) mutant.mutate()
def mutate(self): """ Mutate the asset(s) and return the new asset specification path. """ if self.batch: for asset in glob.glob(get_abspath(self.path)): self.path = asset self._configure_paths() self._run_mutator() else: if self.should_mutate: if self.parse_template: self._process_template(self.path) self._run_mutator() self.exists = True return self.new_path
def mutate(self): """ Mutate the asset(s). """ if self.batch == True: batch_path = get_abspath(self.path) for ext, config in self.mutators.items(): for asset in glob.glob(os.path.join(batch_path, '*.%s' % ext)): self.path = asset self._configure_paths() self._run_mutator() else: if not self.exists: if self.parse_template: self._process_template(self.path) self._run_mutator() self.exists = True return self.new_path
def _configure_paths(self): """ Checks/sets the various path settings needed for mutation. """ # Parse source path self.src_fullpath = get_abspath(self.path) self.src_dirpath = os.path.dirname(self.src_fullpath) # Parse dest/mutated path (if specified) self.dest_dirpath = get_abspath(self.mutated_path or self.src_dirpath) # Setup various path variables if self.batch and not os.path.isdir(self.src_dirpath): raise EnvironmentError('Directory does not exist: %s' % self.src_dirpath) else: self.src_filename = os.path.basename(self.src_fullpath) self.src_name = os.path.splitext(self.src_filename)[0] if self.mutated_path and \ os.path.splitext(self.src_filename)[-1] in self.renderers: # This asset uses a template renderer self.parse_template = True self.src_ext = os.path.splitext(self.src_name)[-1][1:] self.src_name = os.path.splitext(self.src_name)[0] else: self.src_ext = os.path.splitext(self.src_filename)[-1][1:] # Get/setup the mutator if self.mutator: if not isinstance(self.mutator, dict): self.mutator = self.mutators.get(self.mutator, {}) else: self.mutator = self.mutators.get(self.src_ext, {}) # Make sure an appropriate mutator is defined if not self.mutator.get('cmd') or not self.mutator.get('ext'): raise ValueError('No mutator found for %s.' % self.src_ext) # Do various check/path settings dest_ext = self.mutator['ext'] if self.check_method == 'exists': self.dest_filename = '%s%s.%s' % (self.prefix, self.src_name, dest_ext) elif self.check_method == 'checksum': if self.batch: self.checksum = self._compute_checksum(self.src_fullpath) else: self.checksum = self.checksum or \ self._compute_checksum(self.src_fullpath) self.dest_filename = '%s%s.%s.%s' % (self.prefix, self.src_name, self.checksum, dest_ext) else: # self.check_method == 'mtime' if self.batch: self.mtime = self._get_mtime(self.src_fullpath) else: self.mtime = self.mtime or \ self._get_mtime(self.src_fullpath) self.dest_filename = '%s%s.%s.%s' % (self.prefix, self.src_name, self.mtime, dest_ext) # Set the full destination/output path self.dest_fullpath = os.path.join( self.dest_dirpath, self.dest_filename ) # Set the new assetpath to be returned to the template if not self.batch: if self.mutated_path: self.new_path = self.mutated_path + self.dest_filename else: self.new_path = re.sub(r'%s$' % self.src_filename, self.dest_filename, self.path)
def _configure_paths(self): """ Checks/sets the various path settings needed for mutation. """ # Setup various path variables self.src_fullpath = get_abspath(self.path) self.src_dirpath = os.path.dirname(self.src_fullpath) self.dest_dirpath = get_abspath(self.mutated_path or self.src_dirpath) self.src_filename = os.path.basename(self.src_fullpath) self.src_name = os.path.splitext(self.src_filename)[0] if self.mutated_path and \ os.path.splitext(self.src_filename)[-1] in self.renderers: # This asset uses a template renderer self.parse_template = True self.src_ext = os.path.splitext(self.src_name)[-1][1:] self.src_name = os.path.splitext(self.src_name)[0] else: self.src_ext = os.path.splitext(self.src_filename)[-1][1:] # Initialize the mutator if self.mutator: if not isinstance(self.mutator, dict): self.mutator = self.mutators.get(self.mutator, {}) else: self.mutator = self.mutators.get(self.src_ext, {}) # Make sure an appropriate mutator is defined if not self.mutator.get('cmd') or not self.mutator.get('ext'): raise RuntimeError('No mutator found for %s.' % self.src_ext) dest_ext = self.mutator['ext'] # Parse the fingerprint if self.check_method == 'exists': fingerprint = hexhashify(self.src_fullpath) elif self.check_method == 'checksum': if self.batch: self.checksum = compute_md5(self.src_fullpath) else: self.checksum = self.checksum or compute_md5(self.src_fullpath) fingerprint = self.checksum else: # self.check_method == 'stat' if self.batch: self.stat = get_stat(self.src_fullpath) else: self.stat = self.stat or get_stat(self.src_fullpath) fingerprint = hexhashify(self.src_fullpath) + hexhashify(self.stat) # Set the destination filename/path self.dest_filename = '%s%s.%s.%s' % (self.prefix, self.src_name, fingerprint, dest_ext) self.dest_fullpath = os.path.join(self.dest_dirpath, self.dest_filename) # Set the new assetpath to be returned to the template if not self.batch: if self.mutated_path: self.new_path = self.mutated_path + self.dest_filename else: self.new_path = re.sub(r'%s$' % self.src_filename, self.dest_filename, self.path)