Skip to content

Commit

Permalink
Merge pull request containers#58 from rhatdan/except
Browse files Browse the repository at this point in the history
Raise errors do not sys.exit
  • Loading branch information
ericcurtin authored Aug 22, 2024
2 parents fc9bef0 + 585faaa commit fac4cd7
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 29 deletions.
14 changes: 7 additions & 7 deletions ramalama/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from pathlib import Path


def usage():
def usage(exit=0):
print("Usage:")
print(f" {os.path.basename(__file__)} COMMAND")
print()
Expand All @@ -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):
Expand Down Expand Up @@ -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/")
Expand Down Expand Up @@ -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}")
Expand All @@ -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)
Expand All @@ -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",
Expand All @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion ramalama/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 1 addition & 5 deletions ramalama/huggingface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 5 additions & 11 deletions ramalama/oci.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand All @@ -61,15 +56,14 @@ 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:
# Push the model using omlmd, using cwd the model's file parent directory
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
6 changes: 1 addition & 5 deletions ramalama/ollama.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit fac4cd7

Please sign in to comment.