204 lines
6.7 KiB
Python
204 lines
6.7 KiB
Python
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
from .. import job_manager as jm
|
|
from ..repositories import jobs as jobs_repo
|
|
from ..repositories import projects as projects_repo
|
|
from .project_metadata import write_project_metadata
|
|
from .project_service_common import project_output_dir
|
|
|
|
|
|
def verify_project_stems(projects: list[dict]) -> dict:
|
|
"""Report projects whose tracked stem files are missing on disk."""
|
|
results = []
|
|
for project in projects:
|
|
sep_job = jobs_repo.get(project["sep_job_id"]) or {}
|
|
files = sep_job.get("files", [])
|
|
missing = [
|
|
file_entry["path"]
|
|
for file_entry in files
|
|
if file_entry.get("type") == "stem" and not Path(file_entry.get("path", "")).exists()
|
|
]
|
|
if missing:
|
|
results.append(
|
|
{
|
|
"project_id": project["id"],
|
|
"name": project["name"],
|
|
"missing_files": missing,
|
|
}
|
|
)
|
|
return {"projects_with_missing_files": results, "total": len(results)}
|
|
|
|
|
|
def write_project_metadata_snapshots(projects: list[dict]) -> dict:
|
|
"""Write or refresh project metadata files for all projects with directories on disk."""
|
|
written = 0
|
|
errors = []
|
|
|
|
for project in projects:
|
|
try:
|
|
project_dir = project_output_dir(project)
|
|
if not project_dir.exists():
|
|
continue
|
|
|
|
sep_job = jobs_repo.get(project["sep_job_id"]) or {}
|
|
files = sep_job.get("files", [])
|
|
stem_files = {
|
|
file_entry["stem"]: file_entry["path"]
|
|
for file_entry in files
|
|
if file_entry.get("type") == "stem" and file_entry.get("stem")
|
|
}
|
|
source_file = next(
|
|
(file_entry["path"] for file_entry in files if file_entry.get("type") == "source"),
|
|
"",
|
|
)
|
|
result = sep_job.get("result") or {}
|
|
|
|
write_project_metadata(
|
|
project_dir,
|
|
project_id=project["id"],
|
|
name=project["name"],
|
|
model=result.get("model_label", ""),
|
|
model_label=result.get("model_label", ""),
|
|
format=result.get("format_label", ""),
|
|
format_label=result.get("format_label", ""),
|
|
source_file=source_file,
|
|
stems=stem_files,
|
|
sep_job_id=project["sep_job_id"],
|
|
trans_jobs=project.get("trans_jobs") or {},
|
|
abc_jobs=project.get("abc_jobs") or {},
|
|
lrc_jobs=project.get("lrc_jobs") or {},
|
|
mix_job_id=project.get("mix_job_id"),
|
|
created_at=project.get("created_at"),
|
|
)
|
|
written += 1
|
|
except Exception as exc:
|
|
errors.append({"project": project.get("name", project["id"]), "error": str(exc)})
|
|
|
|
return {"written": written, "errors": errors}
|
|
|
|
|
|
def cleanup_rename_project_stems(projects: list[dict]) -> dict:
|
|
"""Rename tracked stem files into the canonical stem filename format."""
|
|
from .separator_service import _stem_filename
|
|
|
|
renamed_total = 0
|
|
errors = []
|
|
|
|
for project in projects:
|
|
sep_job = jobs_repo.get(project["sep_job_id"])
|
|
if not sep_job:
|
|
continue
|
|
|
|
files = list(sep_job.get("files", []))
|
|
changed = False
|
|
|
|
for index, file_entry in enumerate(files):
|
|
if file_entry.get("type") != "stem" or not file_entry.get("stem"):
|
|
continue
|
|
|
|
old_path = Path(file_entry["path"])
|
|
if not old_path.exists():
|
|
continue
|
|
|
|
new_filename = _stem_filename(project["name"], file_entry["stem"], old_path.suffix)
|
|
new_path = old_path.parent / new_filename
|
|
if old_path == new_path:
|
|
continue
|
|
|
|
try:
|
|
old_path.rename(new_path)
|
|
files[index] = {**file_entry, "name": new_filename, "path": str(new_path)}
|
|
changed = True
|
|
renamed_total += 1
|
|
except Exception as exc:
|
|
errors.append(
|
|
{
|
|
"project": project["name"],
|
|
"file": str(old_path),
|
|
"error": str(exc),
|
|
}
|
|
)
|
|
|
|
if changed:
|
|
jobs_repo.update(project["sep_job_id"], files=files)
|
|
|
|
return {"renamed": renamed_total, "errors": errors}
|
|
|
|
|
|
def purge_orphaned_separation_jobs(all_jobs: list[dict]) -> dict:
|
|
"""Delete completed-or-stale separation jobs that are no longer attached to a project."""
|
|
projects = projects_repo.list_persisted()
|
|
project_job_ids = {project["sep_job_id"] for project in projects if project.get("sep_job_id")}
|
|
purged = 0
|
|
|
|
for job in all_jobs:
|
|
if job.get("job_type") == "separate" and job["id"] not in project_job_ids:
|
|
if jm.delete_job(job["id"]):
|
|
purged += 1
|
|
|
|
return {"purged": purged}
|
|
|
|
|
|
def cleanup_lrc_output_files(all_jobs: list[dict], cleaner) -> dict:
|
|
"""Strip section-marker lines from tracked LRC/ELRC output files."""
|
|
files_modified = 0
|
|
lines_removed = 0
|
|
errors = []
|
|
|
|
for job in all_jobs:
|
|
if job.get("job_type") != "lrc":
|
|
continue
|
|
|
|
for file_entry in (job.get("files") or []):
|
|
file_type = file_entry.get("type", "")
|
|
if file_type not in ("elrc", "lrc"):
|
|
continue
|
|
|
|
file_path = file_entry.get("path")
|
|
if not file_path:
|
|
continue
|
|
|
|
path = Path(file_path)
|
|
if not path.exists():
|
|
continue
|
|
|
|
try:
|
|
content = path.read_text(encoding="utf-8")
|
|
cleaned, removed = cleaner(content)
|
|
if removed > 0:
|
|
path.write_text(cleaned, encoding="utf-8")
|
|
files_modified += 1
|
|
lines_removed += removed
|
|
except Exception as exc:
|
|
errors.append({"file": file_path, "error": str(exc)})
|
|
|
|
return {
|
|
"files_modified": files_modified,
|
|
"lines_removed": lines_removed,
|
|
"errors": errors,
|
|
}
|
|
|
|
|
|
def verify_all_project_stems() -> dict:
|
|
return verify_project_stems(projects_repo.list_all())
|
|
|
|
|
|
def write_all_project_metadata_snapshots() -> dict:
|
|
return write_project_metadata_snapshots(projects_repo.list_all())
|
|
|
|
|
|
def cleanup_rename_all_project_stems() -> dict:
|
|
return cleanup_rename_project_stems(projects_repo.list_all())
|
|
|
|
|
|
def purge_orphaned_separation_jobs_from_store() -> dict:
|
|
return purge_orphaned_separation_jobs(jobs_repo.list_all())
|
|
|
|
|
|
def cleanup_all_lrc_output_files() -> dict:
|
|
from .lrc_service import clean_lrc_content
|
|
|
|
return cleanup_lrc_output_files(jobs_repo.list_all(), clean_lrc_content)
|