def validate_container_options(options: dict) -> ContainerOpts: """Validate incoming option types to provide base sanity Args: options: Dictionary of options to be used with docker. (Typically fed from a YAML config file. Returns: A filled ContainerOpts information holder. Raises: ConfigError: If type of data is incorrect or data is missing. """ validated = ContainerOpts() for key in validated.keys(): if key in options: data = options[key] if isinstance(data, (bytes, str)): data = fops.makestr(data) validated.set_with_type(key, data) if not validated.image: raise ConfigError("'image' not defined in container options.") if not validated.name: raise ConfigError("'name' not defined in container options.") return validated
def encode_base64(path: str) -> str: """Reads ``path`` and converts the data to a base64 encode string Args: path: Full path to file to base64 encode contents of Returns: Base64 encoded data as a string Raises: FileNotFoundError: When ``path`` does not exist IsADirectoryError: When ``path`` is a directory """ LOGGER.debug("base64 encoding data from %s", path) with open(path, 'rb') as file_data: return fops.makestr(base64.b64encode(file_data.read()))
def hash_sha512(path: str) -> str: """Creates a SHA512 hash for ``path`` Args: path: Full path to file to create hash for. Returns: string form of SHA256 hash Raises: FileNotFoundError: When ``path`` does not exist IsADirectoryError: When ``path`` is a directory """ LOGGER.debug("Generating SHA512 hash for %s", path) h_sha512 = hashlib.sha512() with open(path, 'rb') as hash_file: buf = hash_file.read(BLOCK_SIZE) while buf: h_sha512.update(buf) buf = hash_file.read(BLOCK_SIZE) return fops.makestr(h_sha512.hexdigest())