4343 from _pytask .lockfile import LockfileState
4444
4545
46+ def _add_lock_command_options (
47+ * , dry_run_help : str
48+ ) -> Callable [[Callable [..., None ]], Callable [..., None ]]:
49+ def decorator (func : Callable [..., None ]) -> Callable [..., None ]:
50+ func = click .option (
51+ "--dry-run" ,
52+ is_flag = True ,
53+ default = False ,
54+ help = dry_run_help ,
55+ )(func )
56+ return click .option (
57+ "-y" ,
58+ "--yes" ,
59+ is_flag = True ,
60+ default = False ,
61+ help = "Apply the changes without prompting for confirmation." ,
62+ )(func )
63+
64+ return decorator
65+
66+
4667@dataclass (slots = True )
4768class _PlannedChange :
4869 kind : str
@@ -237,6 +258,7 @@ def _run_lock_command(
237258 planner : Callable [[Session ], list [_PlannedChange ]] | None = None ,
238259 planner_with_tasks : Callable [[Session , list [PTask ]], list [_PlannedChange ]]
239260 | None = None ,
261+ select_tasks : Callable [[Session ], list [PTask ]] | None = None ,
240262 empty_message : str ,
241263) -> int :
242264 _validate_confirmation_options (raw_config )
@@ -259,11 +281,8 @@ def _run_lock_command(
259281 session .dag = create_dag (session = session )
260282
261283 if planner_with_tasks is not None :
262- tasks = (
263- _select_tasks_with_ancestors (session )
264- if raw_config ["subcommand" ] == "accept"
265- else _select_tasks_exact (session )
266- )
284+ assert select_tasks is not None
285+ tasks = select_tasks (session )
267286 planned_changes = planner_with_tasks (session , tasks )
268287 else :
269288 assert planner is not None
@@ -305,70 +324,40 @@ def lock() -> None:
305324
306325
307326@lock .command (cls = ColoredCommand )
308- @click .option (
309- "--dry-run" ,
310- is_flag = True ,
311- default = False ,
312- help = "Show which recorded states would be updated without writing changes." ,
313- )
314- @click .option (
315- "-y" ,
316- "--yes" ,
317- is_flag = True ,
318- default = False ,
319- help = "Apply the changes without prompting for confirmation." ,
327+ @_add_lock_command_options (
328+ dry_run_help = "Show which recorded states would be updated without writing changes."
320329)
321330def accept (** raw_config : Any ) -> None :
322331 """Accept the current state for selected tasks and their ancestors."""
323- raw_config ["subcommand" ] = "accept"
324332 sys .exit (
325333 _run_lock_command (
326334 raw_config ,
327335 planner_with_tasks = _plan_accept_changes ,
336+ select_tasks = _select_tasks_with_ancestors ,
328337 empty_message = "No lockfile entries need updating." ,
329338 )
330339 )
331340
332341
333342@lock .command (cls = ColoredCommand )
334- @click .option (
335- "--dry-run" ,
336- is_flag = True ,
337- default = False ,
338- help = "Show which recorded states would be removed without writing changes." ,
339- )
340- @click .option (
341- "-y" ,
342- "--yes" ,
343- is_flag = True ,
344- default = False ,
345- help = "Apply the changes without prompting for confirmation." ,
343+ @_add_lock_command_options (
344+ dry_run_help = "Show which recorded states would be removed without writing changes."
346345)
347346def reset (** raw_config : Any ) -> None :
348347 """Remove recorded state for selected tasks."""
349- raw_config ["subcommand" ] = "reset"
350348 sys .exit (
351349 _run_lock_command (
352350 raw_config ,
353351 planner_with_tasks = _plan_reset_changes ,
352+ select_tasks = _select_tasks_exact ,
354353 empty_message = "No lockfile entries need removing." ,
355354 )
356355 )
357356
358357
359358@lock .command (cls = ColoredCommand )
360- @click .option (
361- "--dry-run" ,
362- is_flag = True ,
363- default = False ,
364- help = "Show which stale entries would be removed without writing changes." ,
365- )
366- @click .option (
367- "-y" ,
368- "--yes" ,
369- is_flag = True ,
370- default = False ,
371- help = "Apply the changes without prompting for confirmation." ,
359+ @_add_lock_command_options (
360+ dry_run_help = "Show which stale entries would be removed without writing changes."
372361)
373362def clean (** raw_config : Any ) -> None :
374363 """Remove stale lockfile entries which no longer correspond to collected tasks."""
0 commit comments