Skip to content

Commit 94c0857

Browse files
committed
introduce openarc list prunem which will remove stale config entries
1 parent 058bab9 commit 94c0857

1 file changed

Lines changed: 55 additions & 3 deletions

File tree

src/cli/groups/list.py

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""
22
List command - List saved model configurations.
33
"""
4+
from pathlib import Path
5+
46
import click
57
from rich.panel import Panel
68
from rich.table import Table
@@ -9,6 +11,26 @@
911
from ..main import cli, console
1012

1113

14+
def _path_has_bin_or_xml(model_path):
15+
"""Return True when model_path contains at least one .bin or .xml file."""
16+
if not model_path:
17+
return False
18+
19+
path = Path(model_path)
20+
if not path.exists():
21+
return False
22+
23+
search_dir = path.parent if path.is_file() else path
24+
try:
25+
for file_path in search_dir.rglob("*"):
26+
if file_path.is_file() and file_path.suffix.lower() in (".bin", ".xml"):
27+
return True
28+
except (OSError, PermissionError):
29+
return False
30+
31+
return False
32+
33+
1234
@cli.command("list")
1335
@click.argument('model_name', required=False)
1436
@click.option('--v', 'verbose', is_flag=True, help='Show config for model_name.')
@@ -23,6 +45,7 @@ def list(ctx, model_name, verbose, remove):
2345
openarc list # List all model names
2446
openarc list model_name --v # Show metadata for specific model
2547
openarc list model_name --rm # Remove a model configuration
48+
openarc list prune # Remove stale configs with no .bin/.xml files
2649
"""
2750
if remove:
2851
if not model_name:
@@ -42,14 +65,42 @@ def list(ctx, model_name, verbose, remove):
4265
console.print(f"[red]Failed to remove model configuration:[/red] {model_name}")
4366
ctx.exit(1)
4467
return
45-
68+
4669
models = ctx.obj.server_config.get_all_models()
47-
70+
4871
if not models:
4972
console.print("[yellow]No model configurations found.[/yellow]")
5073
console.print("[dim]Use 'openarc add --help' to see how to save configurations.[/dim]")
5174
return
52-
75+
76+
# Reserved command mode: openarc list prune
77+
if model_name == "prune" and not verbose:
78+
stale_models = []
79+
for name, model_config in models.items():
80+
model_path = model_config.get("model_path")
81+
if not _path_has_bin_or_xml(model_path):
82+
stale_models.append((name, model_path))
83+
84+
if not stale_models:
85+
console.print("[green]No stale model configurations found.[/green]")
86+
return
87+
88+
console.print(f"[yellow]Found {len(stale_models)} stale model configuration(s):[/yellow]")
89+
for name, path in stale_models:
90+
console.print(f" [cyan]{name}[/cyan] [dim](model_path: {path})[/dim]")
91+
92+
if not click.confirm("Delete these entries from config?", default=False):
93+
console.print("[blue]Prune cancelled.[/blue]")
94+
return
95+
96+
removed = 0
97+
for name, _ in stale_models:
98+
if ctx.obj.server_config.remove_model_config(name):
99+
removed += 1
100+
101+
console.print(f"[green]Prune complete.[/green] Removed {removed}/{len(stale_models)} entries.")
102+
return
103+
53104
# Case 1: Show metadata for specific model with -v flag
54105
if model_name and verbose:
55106
if model_name not in models:
@@ -100,3 +151,4 @@ def list(ctx, model_name, verbose, remove):
100151

101152
console.print("[dim]Use 'openarc list <model_name> --v' to see model metadata.[/dim]")
102153
console.print("[dim]Use 'openarc list <model_name> --rm' to remove a configuration.[/dim]")
154+
console.print("[dim]Use 'openarc list prune' to remove stale configurations.[/dim]")

0 commit comments

Comments
 (0)