From 0b51704052ef8d2801d5a2dad033734657f4963c Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Mon, 16 Sep 2024 17:04:31 -0400 Subject: [PATCH] Test for --ignore and fixes for list --json All raised errors should begin with lower case errors. Caught errors will be prefixed with "Error: " Signed-off-by: Daniel J Walsh --- ramalama.py | 8 ++++---- ramalama/cli.py | 8 ++++---- ramalama/common.py | 4 ++-- ramalama/model.py | 6 +++--- ramalama/oci.py | 6 +++--- test/system/010-list.bats | 34 +++++++--------------------------- 6 files changed, 23 insertions(+), 43 deletions(-) diff --git a/ramalama.py b/ramalama.py index a06e7398..c179a4cd 100755 --- a/ramalama.py +++ b/ramalama.py @@ -19,16 +19,16 @@ def main(args): try: ramalama.init_cli() except IndexError as e: - ramalama.perror(str(e).strip("'")) + ramalama.perror("Error: " + str(e).strip("'")) sys.exit(errno.EINVAL) except KeyError as e: - ramalama.perror(str(e).strip("'")) + ramalama.perror("Error: " + str(e).strip("'")) sys.exit(1) except NotImplementedError as e: - ramalama.perror(str(e).strip("'")) + ramalama.perror("Error: " + str(e).strip("'")) sys.exit(errno.ENOTSUP) except subprocess.CalledProcessError as e: - ramalama.perror(str(e).strip("'")) + ramalama.perror("Error: " + str(e).strip("'")) sys.exit(e.returncode) diff --git a/ramalama/cli.py b/ramalama/cli.py index 66a0074c..69a72b51 100644 --- a/ramalama/cli.py +++ b/ramalama/cli.py @@ -231,8 +231,7 @@ def _list_models(args): if path.is_symlink(): name = str(path).replace("/", "://", 1) file_epoch = path.lstat().st_mtime - diff = int(time.time() - file_epoch) - modified = human_duration(diff) + " ago" + modified = int(time.time() - file_epoch) size = subprocess.run(["du", "-h", str(path.resolve())], capture_output=True, text=True).stdout.split()[0] # Store data for later use @@ -255,8 +254,9 @@ def list_cli(args): modified_width = len("MODIFIED") size_width = len("SIZE") for model in models: + modified = human_duration(model["modified"]) + " ago" name_width = max(name_width, len(model["name"])) - modified_width = max(modified_width, len(model["modified"])) + modified_width = max(modified_width, len(modified)) size_width = max(size_width, len(model["size"])) if not args.quiet and not args.noheading and not args.json: @@ -266,7 +266,7 @@ def list_cli(args): if args.quiet: print(model["name"]) else: - print(f"{model['name']:<{name_width}} {model['modified']:<{modified_width}} {model['size']:<{size_width}}") + print(f"{model['name']:<{name_width}} {modified:<{modified_width}} {model['size']:<{size_width}}") def help_parser(subparsers): diff --git a/ramalama/common.py b/ramalama/common.py index e3841ce8..03a0879a 100644 --- a/ramalama/common.py +++ b/ramalama/common.py @@ -77,12 +77,12 @@ def verify_checksum(filename): # Check if the filename starts with "sha256:" fn_base = os.path.basename(filename) if not fn_base.startswith("sha256:"): - raise ValueError(f"Filename does not start with 'sha256:': {fn_base}") + raise ValueError(f"filename does not start with 'sha256:': {fn_base}") # Extract the expected checksum from the filename expected_checksum = fn_base.split(":")[1] if len(expected_checksum) != 64: - raise ValueError("Invalid checksum length in filename") + raise ValueError("invalid checksum length in filename") # Calculate the SHA-256 checksum of the file contents sha256_hash = hashlib.sha256() diff --git a/ramalama/model.py b/ramalama/model.py index 9abbd66e..4a01f99b 100644 --- a/ramalama/model.py +++ b/ramalama/model.py @@ -1,6 +1,6 @@ import os import sys -from ramalama.common import container_manager, exec_cmd, perror +from ramalama.common import container_manager, exec_cmd class Model: @@ -66,10 +66,10 @@ def remove(self, args): print(f"Untagged: {self.model}") except OSError as e: if not args.ignore: - perror(f"Error removing {self.model}: {e}") + raise KeyError(f"removing {self.model}: {e}") else: if not args.ignore: - perror(f"Model {self.model} not found") + raise KeyError(f"model {self.model} not found") self.garbage_collection(args) diff --git a/ramalama/oci.py b/ramalama/oci.py index 1caa9a15..91710a20 100644 --- a/ramalama/oci.py +++ b/ramalama/oci.py @@ -48,7 +48,7 @@ def push(self, args): # Validate the model exists locally local_model_path = os.path.join(args.store, "models/oci", registry, reference_dir) if not os.path.exists(local_model_path): - raise KeyError(f"Model {self.model} not found locally. Cannot push.") + raise KeyError(f"model {self.model} not found locally. Cannot push.") model_file = Path(local_model_path).resolve() try: @@ -74,7 +74,7 @@ def pull(self, args): run_cmd(["omlmd", "pull", self.model, "--output", outdir]) ggufs = [file for file in os.listdir(outdir) if file.endswith(".gguf")] if len(ggufs) != 1: - raise KeyError(f"Error: Unable to identify .gguf file in: {outdir}") + raise KeyError(f"unable to identify .gguf file in: {outdir}") directory = f"{args.store}/models/oci/{registry}/{reference_dir}" os.makedirs(directory, exist_ok=True) @@ -94,6 +94,6 @@ def get_symlink_path(self, args): directory = f"{args.store}/models/oci/{registry}/{reference_dir}" ggufs = [file for file in os.listdir(directory) if file.endswith(".gguf")] if len(ggufs) != 1: - raise KeyError(f"Error: Unable to identify .gguf file in: {directory}") + raise KeyError(f"unable to identify .gguf file in: {directory}") return f"{directory}/{ggufs[0]}" diff --git a/test/system/010-list.bats b/test/system/010-list.bats index d11c5512..fb115a6d 100644 --- a/test/system/010-list.bats +++ b/test/system/010-list.bats @@ -29,6 +29,7 @@ modified | [0-9]\\\+ size | [0-9]\\\+ " + run_ramalama pull ollama://tinyllama run_ramalama list --json while read field expect; do @@ -39,39 +40,18 @@ size | [0-9]\\\+ } -#@test "ramalama list - rm -af removes all models" { -#FIXME run_ramalama rm -af -# is "$output" "Untagged: $IMAGE -#Untagged: $pauseImage -#Deleted: $imageID -#Deleted: $pauseID" "infra list gets removed as well" - -# run_ramalama list --noheading -# is "$output" "" -#} +@test "ramalama list - rm -a removes all models" { + run_ramalama rm -a + run_ramalama list --noheading + is "$output" "" +} @test "ramalama rm --ignore" { random_image_name=i_$(safename) run_ramalama 1 rm $random_image_name - is "$output" "Error: $random_image_name: image not known.*" + is "$output" "Error: model $random_image_name not found.*" run_ramalama rm --ignore $random_image_name is "$output" "" } -#FIXME -#@test "ramalama rm --force bogus" { -# run_ramalama 1 rm bogus -# is "$output" "Error: bogus: image not known" "Should print error" -# run_ramalama rm --force bogus -# is "$output" "" "Should print no output" - -# random_image_name=i_$(safename) -# run_ramalama image tag $IMAGE $random_image_name -# run_ramalama rm --force bogus $random_image_name -# assert "$output" = "Untagged: localhost/$random_image_name:latest" "removed image" - -# run_ramalama list -# assert "$output" !~ "$random_image_name" "image must be removed" -#} - # vim: filetype=sh