|
| 1 | +import click |
| 2 | +import sqlalchemy.orm |
| 3 | + |
| 4 | +from bbblb import model |
| 5 | + |
| 6 | +from bbblb.services import ServiceRegistry |
| 7 | +from bbblb.services.db import DBContext |
| 8 | +from bbblb.services.recording import RecordingManager |
| 9 | + |
| 10 | +from . import main, async_command |
| 11 | + |
| 12 | + |
| 13 | +@main.group() |
| 14 | +def recording(): |
| 15 | + """Recording management.""" |
| 16 | + |
| 17 | + |
| 18 | +@recording.command("list") |
| 19 | +@async_command() |
| 20 | +async def _list(obj: ServiceRegistry): |
| 21 | + """List all recordings and their formats""" |
| 22 | + db = await obj.use("db", DBContext) |
| 23 | + async with db.session() as session, session.begin(): |
| 24 | + stmt = model.Recording.select().options( |
| 25 | + sqlalchemy.orm.joinedload(model.Recording.tenant), |
| 26 | + sqlalchemy.orm.selectinload(model.Recording.formats), |
| 27 | + ) |
| 28 | + for record in (await session.execute(stmt)).scalars(): |
| 29 | + click.echo( |
| 30 | + f"{record.tenant.name} {record.record_id} {','.join(f.format for f in record.formats)}" |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +@recording.command("delete") |
| 35 | +@click.argument("record_id", nargs=-1) |
| 36 | +@async_command() |
| 37 | +async def _delete(obj: ServiceRegistry, record_id): |
| 38 | + """Delete recordings (all formats)""" |
| 39 | + importer = await obj.use("importer", RecordingManager) |
| 40 | + |
| 41 | + db = await obj.use("db", DBContext) |
| 42 | + async with db.session() as session, session.begin(): |
| 43 | + stmt = model.Recording.select(model.Recording.record_id.in_(record_id)) |
| 44 | + for record in (await session.execute(stmt)).scalars().all(): |
| 45 | + await session.delete(record) |
| 46 | + importer.delete(record.tenant.name, record.record_id) |
| 47 | + click.echo(f"Deleted {record.record_id}") |
| 48 | + |
| 49 | + |
| 50 | +@recording.command("import") |
| 51 | +@click.option("--tenant", help="Override the tenant found in the recording") |
| 52 | +@click.argument("FILE", type=click.Path(dir_okay=True), default="-") |
| 53 | +@async_command() |
| 54 | +async def _import(obj: ServiceRegistry, tenant: str, file: str): |
| 55 | + """Import one or more recordings from a tar archive""" |
| 56 | + obj.get("importer", RecordingManager, uninitialized_ok=True).auto_import = False |
| 57 | + importer = await obj.use("importer", RecordingManager) |
| 58 | + |
| 59 | + async def reader(file): |
| 60 | + with click.open_file(file, "rb") as fp: |
| 61 | + while chunk := fp.read(1024 * 64): |
| 62 | + yield chunk |
| 63 | + |
| 64 | + task = await importer.start_import(reader(file), force_tenant=tenant) |
| 65 | + await task.wait() |
| 66 | + if task.error: |
| 67 | + click.echo(f"ERROR {task.error}") |
| 68 | + raise SystemExit(1) |
| 69 | + click.echo("OK") |
| 70 | + |
| 71 | + |
| 72 | +@recording.command() |
| 73 | +@click.option( |
| 74 | + "--dry-run", "-n", help="Simulate changes without changing anything.", is_flag=True |
| 75 | +) |
| 76 | +@async_command() |
| 77 | +async def remove_orphans(obj: ServiceRegistry, dry_run: bool): |
| 78 | + """Remove recording DB entries that do not exist on disk.""" |
| 79 | + db = await obj.use("db", DBContext) |
| 80 | + importer = await obj.use("importer", RecordingManager) |
| 81 | + async with db.session() as session, session.begin(): |
| 82 | + stmt = model.Recording.select().options( |
| 83 | + sqlalchemy.orm.joinedload(model.Recording.tenant), |
| 84 | + sqlalchemy.orm.selectinload(model.Recording.formats), |
| 85 | + ) |
| 86 | + records = await session.execute(stmt) |
| 87 | + for record in records.scalars(): |
| 88 | + populated = False |
| 89 | + for format in record.formats: |
| 90 | + sdir = importer.get_storage_dir( |
| 91 | + record.tenant.name, |
| 92 | + record.record_id, |
| 93 | + format.format, |
| 94 | + ) |
| 95 | + if sdir.exists(): |
| 96 | + populated = True |
| 97 | + continue |
| 98 | + click.echo( |
| 99 | + f"Deleting orphan format: {record.tenant.name}/{record.record_id}/{format.format}" |
| 100 | + ) |
| 101 | + await session.delete(format) |
| 102 | + if not populated: |
| 103 | + click.echo( |
| 104 | + f"Deleting record without formats: {record.tenant.name}/{record.record_id}" |
| 105 | + ) |
| 106 | + await session.delete(record) |
| 107 | + |
| 108 | + if dry_run: |
| 109 | + click.echo("Rolling back changes (dry run)") |
| 110 | + await session.rollback() |
0 commit comments