Spaces:
Running on Zero
Running on Zero
Download stemdeck/library.py from AnjaneyaTurai/stemdeck-gpu: direct link, hf CLI and curl.
- Browser
- Download file 2.11 kB
-
https://huggingface.co/spaces/AnjaneyaTurai/stemdeck-gpu/resolve/main/stemdeck/library.py
- Command line
-
hf download hf://spaces/AnjaneyaTurai/stemdeck-gpu/stemdeck/library.py
-
curl -L -o library.py https://huggingface.co/spaces/AnjaneyaTurai/stemdeck-gpu/resolve/main/stemdeck/library.py
2.11 kB
| """Who may see a song or job, and deleting a song with everything that belongs to it. | |
| Shared by the API routes and the public site's cleanup (stemdeck/housekeeping.py). A song of another visitor is | |
| reported as "not found", exactly like a song that doesn't exist, so ids reveal nothing. | |
| """ | |
| from __future__ import annotations | |
| import re | |
| import shutil | |
| from pathlib import Path | |
| from fastapi import HTTPException | |
| from sqlmodel import Session, col, delete, select | |
| from stemdeck.jobrunner import runner | |
| from stemdeck.models import TERMINAL_JOB_STATUSES, Job, Mix, Song, Stem | |
| from stemdeck.settings import get_settings | |
| SONG_ID = re.compile(r"[0-9a-f]{32}") | |
| def song_folder(song_id: str) -> Path: | |
| if not SONG_ID.fullmatch(song_id): | |
| raise HTTPException(404, "Song not found.") | |
| return get_settings().songs_dir / song_id | |
| def owned_song(session: Session, song_id: str, owner: str) -> Song: | |
| song = session.get(Song, song_id) | |
| if song is None or song.owner != owner: | |
| raise HTTPException(404, "Song not found.") | |
| return song | |
| def owned_job(session: Session, job_id: str, owner: str) -> Job: | |
| job = session.get(Job, job_id) | |
| song = session.get(Song, job.song_id) if job is not None else None | |
| if job is None or song is None or song.owner != owner: | |
| raise HTTPException(404, "Job not found.") | |
| return job | |
| def remove_song(session: Session, song_id: str) -> None: | |
| """Cancel the song's unfinished jobs, then delete its rows and its folder.""" | |
| active = session.exec(select(Job).where(Job.song_id == song_id, | |
| col(Job.status).not_in(TERMINAL_JOB_STATUSES))).all() | |
| for job in active: | |
| runner.cancel(job.id) | |
| session.expire_all() | |
| for table in (Stem, Mix, Job): | |
| session.exec(delete(table).where(table.song_id == song_id)) # type: ignore[arg-type, attr-defined] | |
| song = session.get(Song, song_id) | |
| if song is not None: | |
| session.delete(song) | |
| session.commit() | |
| if SONG_ID.fullmatch(song_id): | |
| shutil.rmtree(get_settings().songs_dir / song_id, ignore_errors=True) | |