Skip to content

Commit

Permalink
Merge pull request #155 from rhatdan/rm
Browse files Browse the repository at this point in the history
Test for --ignore and fixes for list --json
  • Loading branch information
rhatdan authored Sep 18, 2024
2 parents 255588f + 0b51704 commit 924651d
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 43 deletions.
8 changes: 4 additions & 4 deletions ramalama.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
8 changes: 4 additions & 4 deletions ramalama/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions ramalama/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions ramalama/model.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions ramalama/oci.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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]}"
34 changes: 7 additions & 27 deletions test/system/010-list.bats
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ modified | [0-9]\\\+
size | [0-9]\\\+
"

run_ramalama pull ollama://tinyllama
run_ramalama list --json

while read field expect; do
Expand All @@ -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

0 comments on commit 924651d

Please sign in to comment.