Skip to content

Commit

Permalink
Add cli flag to suppress pull progress
Browse files Browse the repository at this point in the history
  • Loading branch information
mkesper committed Jan 31, 2025
1 parent 3f90013 commit 94bd95e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
6 changes: 6 additions & 0 deletions ramalama/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,12 @@ def pull_parser(subparsers):
default=True,
help="require HTTPS and verify certificates when contacting registries",
)
parser.add_argument(
"--quiet",
default=False,
action="store",
help="Do not show progress bar during download",
)
parser.add_argument("MODEL") # positional argument
parser.set_defaults(func=pull_cli)

Expand Down
2 changes: 1 addition & 1 deletion ramalama/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def download_file(url, dest_path, headers=None, show_progress=True):
show_progress = False

try:
http_client.init(url=url, headers=headers, output_file=dest_path, progress=show_progress)
http_client.init(url=url, headers=headers, output_file=dest_path, show_progress=show_progress)
except urllib.error.HTTPError as e:
if e.code == 416: # Range not satisfiable
if show_progress:
Expand Down
2 changes: 2 additions & 0 deletions ramalama/oci.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ def pull(self, args):
raise NotImplementedError("OCI images require a container engine like Podman or Docker")

conman_args = [args.engine, "pull"]
if args.quiet:
conman_args.extend(['--quiet'])
if str(args.tlsverify).lower() == "false":
conman_args.extend([f"--tls-verify={args.tlsverify}"])
if args.authfile:
Expand Down
13 changes: 8 additions & 5 deletions ramalama/ollama.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ def pull_config_blob(repos, accept, registry_head, manifest_data):
download_file(url, config_blob_path, headers=headers, show_progress=False)


def pull_blob(repos, layer_digest, accept, registry_head, models, model_name, model_tag, model_path):
def pull_blob(repos, layer_digest, accept, registry_head, models, model_name, model_tag, model_path, show_progress):
layer_blob_path = os.path.join(repos, "blobs", layer_digest)
url = f"{registry_head}/blobs/{layer_digest}"
headers = {"Accept": accept}
download_file(url, layer_blob_path, headers=headers, show_progress=True)
download_file(url, layer_blob_path, headers=headers, show_progress=show_progress)

# Verify checksum after downloading the blob
if not verify_checksum(layer_blob_path):
Expand All @@ -45,9 +45,9 @@ def pull_blob(repos, layer_digest, accept, registry_head, models, model_name, mo
run_cmd(["ln", "-sf", relative_target_path, model_path])


def init_pull(repos, accept, registry_head, model_name, model_tag, models, model_path, model):
def init_pull(repos, accept, registry_head, model_name, model_tag, models, model_path, model, show_progress):
manifest_data = fetch_manifest_data(registry_head, model_tag, accept)
pull_config_blob(repos, accept, registry_head, manifest_data)
pull_config_blob(repos, accept, registry_head, manifest_data, show_progress)
for layer in manifest_data["layers"]:
layer_digest = layer["digest"]
if layer["mediaType"] != "application/vnd.ollama.image.model":
Expand Down Expand Up @@ -101,11 +101,14 @@ def pull(self, args):
if os.path.exists(model_path):
return model_path

show_progress = not args.quiet
registry = "https://registry.ollama.ai"
accept = "Accept: application/vnd.docker.distribution.manifest.v2+json"
registry_head = f"{registry}/v2/{model_name}"
try:
return init_pull(repos, accept, registry_head, model_name, model_tag, models, model_path, self.model)
return init_pull(
repos, accept, registry_head, model_name, model_tag, models, model_path, self.model, show_progress
)
except urllib.error.HTTPError as e:
if "Not Found" in e.reason:
raise KeyError(f"{self.model} was not found in the Ollama registry")
Expand Down
3 changes: 2 additions & 1 deletion ramalama/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ def pull(self, args):
os.symlink(self.model, os.path.join(symlink_dir, self.filename))
os.symlink(self.model, target_path)
else:
show_progress = not args.quiet
url = self.type + "://" + self.model
# Download the model file to the target path
download_file(url, target_path, headers={}, show_progress=True)
download_file(url, target_path, headers={}, show_progress=show_progress)
relative_target_path = os.path.relpath(target_path, start=os.path.dirname(model_path))
if self.check_valid_model_path(relative_target_path, model_path):
# Symlink is already correct, no need to update it
Expand Down

0 comments on commit 94bd95e

Please sign in to comment.