From 34d925ad8d574e2f1399c3498fc38f0b70b9103c Mon Sep 17 00:00:00 2001 From: Anton Elmiger Date: Tue, 24 Oct 2023 10:45:08 +0200 Subject: [PATCH] Add option for cli install folder definition --- syclops/cli.py | 12 +++++++++-- syclops/utility/setup_utils.py | 38 ++++++++++++++++++++++------------ 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/syclops/cli.py b/syclops/cli.py index ca17088..aaeeb72 100644 --- a/syclops/cli.py +++ b/syclops/cli.py @@ -100,6 +100,13 @@ nargs="*", ) +parser.add_argument( + "-if", + "--install_folder", + help="Path to install folder", + default=None, +) + def _run_subprocess(args, cwd=None, env=None, execution_info="Command"): process_result = subprocess.run(args, cwd=cwd, env=env) @@ -309,13 +316,14 @@ def _ensure_catalog_exists(install_folder: Path): def main(): - install_folder = get_or_create_install_folder() + args = parser.parse_args() + + install_folder = get_or_create_install_folder(args.install_folder) install_blender(BLENDER_VERSION, install_folder) _ensure_catalog_exists(install_folder) console = Console() console.print(f"Syclops folder: {install_folder}", style="bold green") - args = parser.parse_args() if len(sys.argv) == 1: parser.print_help() diff --git a/syclops/utility/setup_utils.py b/syclops/utility/setup_utils.py index 586fbb0..d04fcdf 100644 --- a/syclops/utility/setup_utils.py +++ b/syclops/utility/setup_utils.py @@ -95,25 +95,37 @@ def install_blender(version: str, install_dir: Path) -> None: # Clean up dest_file.unlink() -def get_or_create_install_folder() -> Path: +def get_or_create_install_folder(install_folder_path: Path) -> Path: + """ + Get the install folder from the config file, or ask the user for a folder. + + Args: + install_folder_path (Path): The path to the install folder, if it exists. + + Returns: + Path: The path to the install folder. + """ config = _load_config() + install_folder_key = "install_folder" - # If 'install_folder' is not in the config, ask the user and save it - if "install_folder" not in config: - install_folder = _ask_directory().resolve() - config["install_folder"] = str(install_folder) + def determine_folder(): + if install_folder_path is not None: + return install_folder_path.resolve() + return _ask_directory().resolve() + + # If 'install_folder' is not in the config or the saved folder doesn't exist + if install_folder_key not in config or not Path(config[install_folder_key]).exists(): + install_folder = determine_folder() + config[install_folder_key] = str(install_folder) _write_config(config) else: - install_folder = Path(config["install_folder"]).resolve() - if not install_folder.exists(): - install_folder = _ask_directory().resolve() - config["install_folder"] = str(install_folder) - _write_config(config) - - if not install_folder.exists(): - install_folder.mkdir(parents=True, exist_ok=True) + install_folder = Path(config[install_folder_key]).resolve() + + # Ensure the folder exists + install_folder.mkdir(parents=True, exist_ok=True) return install_folder + def _load_config() -> dict: config_file = _get_or_create_config_file_path() if config_file.exists():