def delete(self, template: str, target: str): """ Delete a rendered template """ if os.path.exists(target): force_remove(target) context.log.warning("%s removed", target) caches.get(self._cache_key).pop(target) events.file.deleted(target)
def execute(self, template: str, target: str, migrate_retries_count=0, original_template=None): """ Render a template """ if os.path.abspath(os.path.join(os.getcwd(), target)) in config.files: # Never write a file that match ddb configuration file return if original_template is None: original_template = template try: for rendered, destination in self._render_template( template, target): if original_template != template and 'autofix' in config.args and config.args.autofix: context.logger.info("[autofix]: %s", original_template) copy_if_different(template, original_template, log=True) written = False if not isinstance(rendered, bool): is_bynary = isinstance(rendered, (bytes, bytearray)) written = write_if_different(destination, rendered, 'rb' if is_bynary else 'r', 'wb' if is_bynary else 'w', log_source=original_template) caches.get(self._cache_key).set(target, rendered) context.mark_as_processed(template, destination) if written or rendered is True or config.eject: events.file.generated(source=original_template, target=destination) except Exception as render_error: # pylint:disable=broad-except if migrate_retries_count > 50: raise render_error try: template = self._autofix_render_error(template, target, original_template, render_error) except Exception as migrate_error: context.log.warning( "Automatic template migration has failed for \"%s\": %s", template, migrate_error) raise render_error from migrate_error if not template: raise render_error migrate_retries_count += 1 self.execute(template, target, migrate_retries_count, original_template)
def _target_is_modified(self, template: str, target: str) -> bool: rendered = caches.get(self._cache_key).get(target) if rendered is None: return True if not os.path.exists(target): caches.get(self._cache_key).pop(target) return True is_bynary = isinstance(rendered, (bytes, bytearray)) read_encoding = None if is_bynary else "utf-8" with open(target, mode='rb' if is_bynary else 'r', encoding=read_encoding) as read_file: existing_data = read_file.read() return existing_data != rendered
def prepare(self): """ This should be called before processing extra-services configuration. """ cache = caches.get(self.cache_name) for key in self.keys: self.cached[key] = cache.get(key, self.type()) self.previous[key] = self.type(self.cached[key]) self.current[key] = self.type()
def get_removed(self): """ This should be called after processing. Generates (key, value) to remove. """ cache = caches.get(self.cache_name) for key in self.keys: to_remove_values = self.previous[key] - self.current[key] for to_remove in to_remove_values: yield key, to_remove cache.set(key, self.current[key])
def execute(command: Command): """ Check for updates :param command command name :return: """ if not command.avoid_stdout and config.data.get('core.check_updates'): cache = caches.get('core.check_for_update.version') last_check = cache.get('last_check', None) today = date.today() if last_check is None or last_check < today: github_repository = config.data.get('core.github_repository') check_for_update(github_repository, True, True) cache.set('last_check', today)
def enforced(): """ Handle the list of enforced actions """ files = config.data.get('gitignore.enforce') cache = caches.get("gitignore") cached_files = cache.get('enforced', list()) for file in cached_files: if file not in files: UpdateGitignoreAction.remove(file) for file in files: UpdateGitignoreAction.execute(file) if file not in cached_files: cached_files.append(file) cache.set('enforced', cached_files) cache.flush()
def copy_from_url(source, destination, filename=None): """ Copy from an URL source. """ cache = caches.get(requests_cache_name) response = cache.get(source) if not response: response = requests.get(source, allow_redirects=True) response.raise_for_status() cache.set(source, response) if not filename: content_disposition = response.headers['content-disposition'] filename = re.findall("filename=(.+)", content_disposition)[0] target_path = os.path.join(destination, filename) if write_if_different( target_path, response.content, 'rb', 'wb', log_source=source) or config.eject: return target_path return None
def execute(self): """ Execute action """ cache = caches.get("file") found_files = set() for file in self.file_walker.items: cache.set(file, None) found_files.add(file) for cached_file in cache.keys(): if cached_file not in found_files: cache.pop(cached_file) events.file.deleted(cached_file) for file in self.file_walker.items: events.file.found(file) cache.flush()