- Introduce CLI using Typer for repository management - Add commands for archiving, cleaning, moving, reviewing, and status - Implement configuration loading and default structure setup - Include utilities for Git status checks and directory size calculation - Lay groundwork for extensible repository operations Signed-off-by: Max P. <Mail@MPassarello.de>
76 lines
2.1 KiB
Python
76 lines
2.1 KiB
Python
from typer import Typer
|
|
from rich.console import Console
|
|
from rich.table import Table
|
|
from pathlib import Path
|
|
from datetime import datetime
|
|
from repocat.config import load_config
|
|
from repocat.models.config import RepoCategory
|
|
|
|
app = Typer()
|
|
console = Console()
|
|
|
|
|
|
def get_dir_size(path: Path) -> int:
|
|
"""Returns size in bytes."""
|
|
return sum(f.stat().st_size for f in path.rglob("*") if f.is_file())
|
|
|
|
|
|
def summarize_category(category: RepoCategory, base_path: Path):
|
|
cat_path = base_path / category.subdir
|
|
if not cat_path.exists():
|
|
return 0, 0.0, None, None
|
|
|
|
now = datetime.now()
|
|
repo_infos = []
|
|
|
|
for repo in cat_path.iterdir():
|
|
if not repo.is_dir():
|
|
continue
|
|
|
|
size_bytes = get_dir_size(repo)
|
|
mtime = datetime.fromtimestamp(repo.stat().st_mtime)
|
|
age_days = (now - mtime).days
|
|
|
|
repo_infos.append((repo.name, size_bytes, age_days, mtime))
|
|
|
|
if not repo_infos:
|
|
return 0, 0.0, None, None
|
|
|
|
total_size = sum(info[1] for info in repo_infos) / 1024 / 1024
|
|
avg_age = sum(info[2] for info in repo_infos) // len(repo_infos)
|
|
|
|
oldest = max(repo_infos, key=lambda x: x[2]) # größtes Alter
|
|
|
|
return len(repo_infos), total_size, avg_age, (oldest[0], oldest[2])
|
|
|
|
|
|
@app.command("status")
|
|
def status():
|
|
"""
|
|
Zeigt eine Übersicht über alle Repository-Kategorien.
|
|
"""
|
|
config = load_config()
|
|
base = config.base_dir
|
|
|
|
table = Table(title="Repository-Status")
|
|
table.add_column("Kategorie")
|
|
table.add_column("Repos", justify="right")
|
|
table.add_column("Gesamtgröße", justify="right")
|
|
table.add_column("Ø Alter", justify="right")
|
|
table.add_column("Ältestes Repo", justify="left")
|
|
|
|
for cat in config.categories:
|
|
if cat.is_archive:
|
|
continue
|
|
count, total_size, avg_age, oldest = summarize_category(cat, base)
|
|
|
|
table.add_row(
|
|
cat.name,
|
|
str(count),
|
|
f"{total_size:.1f} MB",
|
|
f"{avg_age or '–'} d",
|
|
f"{oldest[0]} ({oldest[1]} d)" if oldest else "–"
|
|
)
|
|
|
|
console.print(table)
|