Skip to content

Commit dfd7b2a

Browse files
committed
cli: Publish/unpublish recordings after import
1 parent 169efc3 commit dfd7b2a

2 files changed

Lines changed: 87 additions & 22 deletions

File tree

bbblb/cli/recording.py

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,40 @@ async def unpublish(obj: ServiceRegistry, record_id):
6767
await _change_publish_flag(obj, record_id, model.RecordingState.UNPUBLISHED)
6868

6969

70-
async def _change_publish_flag(obj: ServiceRegistry, record_id, state:model.RecordingState):
70+
async def _change_publish_flag(
71+
obj: ServiceRegistry, record_id, state: model.RecordingState
72+
):
7173
importer = await obj.use("importer", RecordingManager)
7274
db = await obj.use("db", DBContext)
7375

7476
async with db.session() as session:
75-
stmt = model.Recording.select(model.Recording.record_id.in_(record_id), model.Recording.state != state).options(sqlalchemy.orm.joinedload(model.Recording.tenant))
77+
stmt = model.Recording.select(
78+
model.Recording.record_id.in_(record_id), model.Recording.state != state
79+
).options(sqlalchemy.orm.joinedload(model.Recording.tenant))
7680
records = (await session.execute(stmt)).scalars().all()
7781
for record in records:
7882
record.state = state
7983
await session.commit()
8084
if state == model.RecordingState.PUBLISHED:
81-
await asyncio.to_thread(importer.publish, record.tenant.name, record.record_id)
85+
await asyncio.to_thread(
86+
importer.publish, record.tenant.name, record.record_id
87+
)
8288
else:
83-
await asyncio.to_thread(importer.unpublish, record.tenant.name, record.record_id)
84-
89+
await asyncio.to_thread(
90+
importer.unpublish, record.tenant.name, record.record_id
91+
)
8592

8693

8794
@recording.command("import")
8895
@click.option("--tenant", help="Override the tenant found in the recording")
96+
@click.option(
97+
"--publish/--unpublish",
98+
help="Publish or unpublsh recording after import",
99+
default=None,
100+
)
89101
@click.argument("FILE", type=click.Path(dir_okay=True), default="-")
90102
@async_command()
91-
async def _import(obj: ServiceRegistry, tenant: str, file: str):
103+
async def _import(obj: ServiceRegistry, tenant: str, publish: bool | None, file: str):
92104
"""Import one or more recordings from a tar archive"""
93105
importer = await obj.use("importer", RecordingManager)
94106

@@ -99,16 +111,34 @@ async def reader(file):
99111

100112
task = await importer.start_import(reader(file), force_tenant=tenant)
101113
await task.wait()
102-
if task.error:
103-
click.echo(f"ERROR {task.error}")
104-
raise SystemExit(1)
105114

106-
click.echo("OK")
115+
for format in task.formats:
116+
click.echo(
117+
f"Imported: {format.recording.tenant.name}/{format.recording.record_id} ({format.format})"
118+
)
119+
if (
120+
publish is True
121+
and format.recording.started != model.RecordingState.PUBLISHED
122+
):
123+
await _change_publish_flag(
124+
obj, [format.recording.record_id], model.RecordingState.PUBLISHED
125+
)
126+
elif (
127+
publish is False
128+
and format.recording.started != model.RecordingState.UNPUBLISHED
129+
):
130+
await _change_publish_flag(
131+
obj, [format.recording.record_id], model.RecordingState.UNPUBLISHED
132+
)
133+
for error in task.errors:
134+
click.echo(f"ERROR: {error}")
135+
if task.errors:
136+
raise SystemExit(1)
107137

108138

109139
@recording.command()
110140
@click.option(
111-
"--dry-run", "-n", help="Simulate changes without changing anything.", is_flag=True
141+
"--dry-run", "-n", help="Do not actually remove any recordings.", is_flag=True
112142
)
113143
@async_command()
114144
async def remove_orphans(obj: ServiceRegistry, dry_run: bool):

docs/_click.rst

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ Recording management.
107107
============== ======================================================
108108
list List all recordings and their formats
109109
delete Delete recordings (all formats)
110+
publish Publish recordings
111+
unpublish Unpublish recordings
110112
import Import one or more recordings from a tar archive
111113
remove-orphans Remove recording DB entries that do not exist on disk.
112114
============== ======================================================
@@ -125,6 +127,38 @@ recording delete
125127

126128
Delete recordings (all formats)
127129

130+
.. table:: Options
131+
:width: 100%
132+
133+
========= =================
134+
Option Help
135+
========= =================
136+
RECORD_ID Optional argument
137+
========= =================
138+
139+
recording publish
140+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
141+
142+
``Usage: bbblb recording publish [OPTIONS] [RECORD_ID]...``
143+
144+
Publish recordings
145+
146+
.. table:: Options
147+
:width: 100%
148+
149+
========= =================
150+
Option Help
151+
========= =================
152+
RECORD_ID Optional argument
153+
========= =================
154+
155+
recording unpublish
156+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
157+
158+
``Usage: bbblb recording unpublish [OPTIONS] [RECORD_ID]...``
159+
160+
Unpublish recordings
161+
128162
.. table:: Options
129163
:width: 100%
130164

@@ -144,12 +178,13 @@ Import one or more recordings from a tar archive
144178
.. table:: Options
145179
:width: 100%
146180

147-
============= ==========================================
148-
Option Help
149-
============= ==========================================
150-
--tenant TEXT Override the tenant found in the recording
151-
FILE Optional argument
152-
============= ==========================================
181+
======================= ==========================================
182+
Option Help
183+
======================= ==========================================
184+
--tenant TEXT Override the tenant found in the recording
185+
--publish / --unpublish Publish or unpublsh recording after import
186+
FILE Optional argument
187+
======================= ==========================================
153188

154189
recording remove-orphans
155190
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -161,11 +196,11 @@ Remove recording DB entries that do not exist on disk.
161196
.. table:: Options
162197
:width: 100%
163198

164-
============= ===========================================
165-
Option Help
166-
============= ===========================================
167-
-n, --dry-run Simulate changes without changing anything.
168-
============= ===========================================
199+
============= ======================================
200+
Option Help
201+
============= ======================================
202+
-n, --dry-run Do not actually remove any recordings.
203+
============= ======================================
169204

170205
server
171206
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)