def download(url, target=None, force=False): """ Download and storage file from a url """ if not is_url(url): raise TypeError("Str '%s' is not a valid url." % url) storage_folder = target or settings.get('STORAGE_TEMPLATES') auth = str(settings.get('REPOSITORY_AUTH')).replace("'", '') file_name = path.basename(urlsplit(url).path) file_path = storage_folder + file_name if force or not path.isfile(file_path): Storage.create_folders(storage_folder) try: request = Request(url) if auth: request.add_header('Authorization', auth) with urlopen(request) as response: Storage.file(file_path, response.read(), 'wb') logger.log(url, 'download DONE!') except HTTPError as e: e.msg += ": URL '%s' cannot be downloaded" % url raise e else: logger.log(url, 'from CACHE!') return file_path
def generate(key_pass): """ Generate secret key from key pass """ b_key_pass = key_pass.encode(settings.get('ENCODING')) sha256 = SHA256.new(b_key_pass) secret_key = sha256.digest() secret_store = binascii.hexlify(secret_key).decode( settings.get('ENCODING')) Storage.file(settings.get('STORAGE_KEY_PATH'), secret_store) logger.log("Generated secret key '{0}' " "and saved at '{1}'".format( secret_store, settings.get('STORAGE_KEY_PATH'))) return secret_store
def values(custom_only=False): """ List of settings custom and defaults """ if custom_only and Path(_BAKERC_PATH).is_file(): lines = Storage.file(_BAKERC_PATH).split('\n') configs = {} for line in lines: if line: key, val = line.split('=') configs[key] = val return configs return globals()['BAKER_SETTINGS']
def replace(self): """ Replace variables in template file based on recipe instructions """ for instruction in self.instructions: target = instruction.template template_path = instruction.template replaced = Storage.file(template_path) if instruction.variables: template = BakerTemplate(replaced) replaced = template.replace(instruction.variables) if hasattr(instruction, 'path'): target = instruction.path if settings.get('TEMPLATE_EXT') and target.endswith( settings.get('TEMPLATE_EXT')): ext_size = len(settings.get('TEMPLATE_EXT')) + 1 target = target[:-ext_size] Storage.file(target, content=replaced) self._add_file_permission(instruction, target) logger.log(instruction.name, instruction.template, target)
def key(self): """ Read secret key from storage file """ return Storage.file(settings.get('STORAGE_KEY_PATH'))