From 2204f9e57543b1ebc5029f43002eaa27e74d95d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20F=C3=B6rster?= Date: Wed, 19 Mar 2025 13:00:51 +0100 Subject: [PATCH] refactoring for helper functions --- src/pycage/helpers.py | 59 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/pycage/helpers.py diff --git a/src/pycage/helpers.py b/src/pycage/helpers.py new file mode 100644 index 0000000..c648c3c --- /dev/null +++ b/src/pycage/helpers.py @@ -0,0 +1,59 @@ +from __future__ import annotations + +import os +import shutil +import stat +from pathlib import Path + +import click + +from pycage import config, helpers + + +def print_error( + error: Exception, +) -> None: + click.echo(f"During the procedure the following exception occurred: {error}", err=True) + + +def path_verify_existence(path: Path) -> None: + if not path.exists(): + raise FileNotFoundError(f"Path does not exist: >{path}<") + + +def path_verify_directory(path: Path) -> None: + if not path.is_dir(): + raise ValueError(f"Path is not a directory: >{path}<") + + +def delete_folder_recursively( + folder: Path, + verbose: bool = False, +) -> None: + if not folder.exists(): + return + + if verbose: + print( + f"Target folder with name >{folder.name}< already exists and " + "is deleted automatically." + ) + + path_verify_directory(folder) + if not folder.is_absolute(): + folder = folder.absolute() + + if not os.access(folder, os.W_OK): + os.chmod(folder, stat.S_IWUSR) + shutil.rmtree(folder) + + +def get_interpreter() -> Path: + exec_interp = config.CFG.os_info.INTERPRETER + pth_int = Path.cwd() / "python" / exec_interp + try: + helpers.path_verify_existence(pth_int) + except FileNotFoundError as err: + raise RuntimeError("Base interpreter not found") from err + + return pth_int