diff --git a/middleware/decrypt.py b/middleware/decrypt.py index 335aac5..c7e4173 100644 --- a/middleware/decrypt.py +++ b/middleware/decrypt.py @@ -8,6 +8,7 @@ python3 decrypt.py --output-dir /outputs/ file.txt file.c4gh sk.sec pk.pub """ from argparse import ArgumentParser +import logging import os from pathlib import Path import shutil @@ -17,6 +18,8 @@ from crypt4gh.lib import decrypt # type: ignore from crypt4gh.keys import get_private_key # type: ignore +logger = logging.getLogger(__name__) + def get_private_keys(file_paths: list[Path]) -> list[bytes]: """Retrieve private keys from a list of files. @@ -33,7 +36,9 @@ def get_private_keys(file_paths: list[Path]) -> list[bytes]: # Callback returns password of sk key = get_private_key(filepath=file_path, callback=lambda x: '') private_keys.append(key) + logger.debug(f"{file_path} identified as a private key", ) except ValueError: + logger.debug(f"{file_path} not identified as a private key") continue return private_keys @@ -56,6 +61,7 @@ def decrypt_files(file_paths: list[Path], private_keys: list[bytes]): try: decrypt(keys=key_tuples, infile=f_in, outfile=f_out) # Checks for magic shutil.move(f_out.name, file_path) + logger.info(f"Decrypted {file_path} successfully") except ValueError as e: if str(e) != "Not a CRYPT4GH formatted file": print(f"Private key for {file_path} not provided") @@ -81,6 +87,7 @@ def move_files(file_paths: list[Path], output_dir: Path) -> list[Path]: existing_names.add(file_path.name) for src, dest in zip(file_paths, output_paths): shutil.move(src, dest) + logger.debug(f"Moved {src} to {dest}") return output_paths @@ -97,6 +104,7 @@ def remove_files(directory: Path): raise ValueError(f"Could not remove files: {directory} is not a directory.") for file in directory.iterdir(): subprocess.run(["rm", "-P", str(file)], check=True) + logger.debug(f"Removed {file.name}") def get_args(): @@ -123,6 +131,8 @@ def get_args(): def main(): """Coordinate execution of script.""" args = get_args() + logger.debug(f"File paths: {", ".join([f.name for f in args.file_paths])}") + logger.debug(f"Output directory: {args.output_dir}") new_paths = move_files(file_paths=args.file_paths, output_dir=args.output_dir) keys = get_private_keys(file_paths=new_paths) try: