From 585faaa456c7f739ba448cf3b9f3fb5e05873e61 Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Thu, 22 Aug 2024 09:43:46 -0400 Subject: [PATCH] Raise errors do not sys.exit Signed-off-by: Daniel J Walsh --- ramalama/cli.py | 14 +++++++------- ramalama/common.py | 2 +- ramalama/huggingface.py | 6 +----- ramalama/oci.py | 16 +++++----------- ramalama/ollama.py | 6 +----- 5 files changed, 15 insertions(+), 29 deletions(-) diff --git a/ramalama/cli.py b/ramalama/cli.py index 0ebcaa0..43da0c2 100644 --- a/ramalama/cli.py +++ b/ramalama/cli.py @@ -15,7 +15,7 @@ from pathlib import Path -def usage(): +def usage(exit=0): print("Usage:") print(f" {os.path.basename(__file__)} COMMAND") print() @@ -25,7 +25,7 @@ def usage(): print(" push MODEL TARGET Push a model to target") print(" run MODEL Run a model") print(" serve MODEL Serve a model") - sys.exit(1) + sys.exit(exit) def mkdirs(store): @@ -85,7 +85,7 @@ def list_files_by_modification(): def list_cli(store, args, port): if len(args) > 0: - usage() + usage(1) print(f"{'NAME':<67} {'MODIFIED':<15} {'SIZE':<6}") mycwd = os.getcwd() os.chdir(f"{store}/models/") @@ -176,7 +176,7 @@ def list_cli(store, args, port): def pull_cli(store, args, port): if len(args) < 1: - usage() + usage(1) model = args.pop(0) matching_files = glob.glob(f"{store}/models/*/{model}") @@ -203,7 +203,7 @@ def pull_cli(store, args, port): def push_cli(store, args, port): if len(args) < 2: - usage() + usage(1) model = args.pop(0) target = args.pop(0) @@ -218,7 +218,7 @@ def push_cli(store, args, port): def run_cli(store, args, port): if len(args) < 1: - usage() + usage(1) symlink_path = pull_cli(store, args, port) exec_cmd(["llama-cli", "-m", @@ -227,7 +227,7 @@ def run_cli(store, args, port): def serve_cli(store, args, port): if len(args) < 1: - usage() + usage(1) symlink_path = pull_cli(store, args, port) exec_cmd(["llama-server", "--port", port, "-m", symlink_path]) diff --git a/ramalama/common.py b/ramalama/common.py index bb7729a..5a29097 100644 --- a/ramalama/common.py +++ b/ramalama/common.py @@ -54,7 +54,7 @@ def run_curl_cmd(args, filename): except subprocess.CalledProcessError as e: if e.returncode == 22: perror(filename + " not found") - sys.exit(e.returncode) + raise e def verify_checksum(filename): diff --git a/ramalama/huggingface.py b/ramalama/huggingface.py index e983a9d..c372bae 100644 --- a/ramalama/huggingface.py +++ b/ramalama/huggingface.py @@ -27,10 +27,6 @@ def pull(model, ramalama_store): # Symlink is already correct, no need to update it return symlink_path - try: - run_cmd(["ln", "-sf", relative_target_path, symlink_path]) - except subprocess.CalledProcessError as e: - perror(e) - sys.exit(e.returncode) + run_cmd(["ln", "-sf", relative_target_path, symlink_path]) return symlink_path diff --git a/ramalama/oci.py b/ramalama/oci.py index f5e7547..6f5b4e8 100644 --- a/ramalama/oci.py +++ b/ramalama/oci.py @@ -18,8 +18,7 @@ def pull(model, store): run_cmd(["omlmd", "pull", target, "--output", outdir]) ggufs = [file for file in os.listdir(outdir) if file.endswith('.gguf')] if len(ggufs) != 1: - print(f"Error: Unable to identify .gguf file in: {outdir}") - sys.exit(-1) + raise KeyError(f"Error: Unable to identify .gguf file in: {outdir}") directory = f"{store}/models/oci/{registry}/{reference_dir}" os.makedirs(directory, exist_ok=True) @@ -32,11 +31,7 @@ def pull(model, store): # Symlink is already correct, no need to update it return symlink_path - try: - run_cmd(["ln", "-sf", relative_target_path, symlink_path]) - except subprocess.CalledProcessError as e: - perror(e) - sys.exit(e.returncode) + run_cmd(["ln", "-sf", relative_target_path, symlink_path]) return symlink_path @@ -61,8 +56,7 @@ def push(store, model, target): local_model_path = os.path.join( store, 'models/oci', registry, reference_dir) if not os.path.exists(local_model_path): - print_error(f"Model {model} not found locally. Cannot push.") - sys.exit(1) + raise KeyError(f"Model {model} not found locally. Cannot push.") model_file = Path(local_model_path).resolve() try: @@ -70,6 +64,6 @@ def push(store, model, target): run_cmd(["omlmd", "push", target, str(model_file), "--empty-metadata"], cwd=model_file.parent) except subprocess.CalledProcessError as e: - raise subprocess.CalledProcessError( - f"Failed to push model to OCI: {e}") + perror(f"Failed to push model to OCI: {e}") + raise e return local_model_path diff --git a/ramalama/ollama.py b/ramalama/ollama.py index 7f398a8..1f54cc4 100644 --- a/ramalama/ollama.py +++ b/ramalama/ollama.py @@ -37,11 +37,7 @@ def pull_blob(repos, layer_digest, accept, registry_head, models, model_name, mo os.makedirs(models, exist_ok=True) relative_target_path = os.path.relpath( layer_blob_path, start=os.path.dirname(symlink_path)) - try: - run_cmd(["ln", "-sf", relative_target_path, symlink_path]) - except subprocess.CalledProcessError as e: - perror(e) - sys.exit(e.returncode) + run_cmd(["ln", "-sf", relative_target_path, symlink_path]) def pull(model, store):