Skip to content

Add --show-action option to print each action before executing it (#390) - #522

Open
slvs26 wants to merge 1 commit into
pydoit:masterfrom
slvs26:feature/show-action
Open

slvs26 wants to merge 1 commit into
pydoit:masterfrom
slvs26:feature/show-action

Conversation

@slvs26

@slvs26 slvs26 commented Dec 1, 2026

Copy link
Copy Markdown

Closes #390. Continues the work started in #392.

What it does

$ doit --verbosity 2 --show-action
.  demo
 +  echo abc efg
abc efg
 +  py_action(2, b=4)
in py_action: a=2, b=4
 +  echo 'list form'
list form
 +  echo bye
bye

Follows the TODO list in #390:

  • new command line option --show-action (also settable from DOIT_CONFIG as show_action)
  • title() method for actions
    • BaseAction.title() returns str(self), so custom actions work and can override it
    • CmdAction: the expanded command line (%(targets)s etc. substituted); list commands are shown with shlex.join
    • PythonAction: func(arg, key=value) using the args/kwargs from the task definition
  • reporter method called before each action: execute_action(task, action)
    • Task.execute() / execute_teardown() take an optional reporter and call it before each action, including teardown actions
    • ConsoleReporter prints + <title> only when show_action is set; it skips hidden _ tasks, like execute_task
    • ZeroReporter / ErrorOnlyReporter / JsonReporter: no-op
    • reporters that don't define execute_action (existing plugins) keep working; it is looked up with getattr

Differences from #392

  • No isinstance(reporter, ConsoleReporter) check in Task. Any reporter can implement the hook, and task.py doesn't import reporter.py.
  • Works with -n (process and thread runners). MReporter sends the action title from the sub-process (actions may not be picklable), and the main process passes a small stand-in object with a title() method to the real reporter.
  • A CmdAction whose command is generated by a callable is described, not called (Cmd: make_cmd('x')). Calling it just to print it would run user code twice, the problem fixed in CmdAction calls callable more than once #437.
  • title() never raises. If expansion fails, it shows the raw action, and the real error is still reported when the action runs.

Also fixed

CmdAction.__str__ raised TypeError when the action was a tuple ("Cmd: %s" % self._action). It now uses % (self._action,).

Known limitation (documented)

With --process/-n, reporter calls go through the result queue, so the + lines can be printed out of order with the actions' own output. The task title line . name already behaves this way. Thread mode and the default runner keep the order.

While testing I also noticed that, on master, teardown actions run twice with -n 2 -P thread (bye is printed twice). This PR doesn't change that. I can open a separate issue for it.

Tests

  • tests/test_action.py: titles for str/expanded/list/callable/invalid CmdAction and PythonAction
  • tests/test_reporter.py: console on/off/hidden task, zero/error-only/json no-op
  • tests/test_task.py: called before each action and teardown action, stops after failure, reporter without the method
  • tests/test_runner.py: Runner, MReporter, MRunner (process) and MThreadRunner
  • tests/test_cmd_run.py: end-to-end option on/off

rut: 861 passed. pyflakes clean, no new pycodestyle warnings. Docs added in doc/cmd-run.rst and an entry in CHANGES.

🤖 Generated with Claude Code

…doit#390)

- BaseAction.title(): short description of an action, overridable.
  CmdAction shows the expanded command line (callables that generate the
  command are described, never called, see pydoit#437). PythonAction shows
  `func(args, key=value)`.
- Reporters get `execute_action(task, action)`, called by Task.execute()
  and Task.execute_teardown() before each action. ConsoleReporter prints
  ` +  <title>` when `show_action` is set. Reporters that do not
  implement it keep working.
- MReporter forwards the action title from sub-processes.
- fix CmdAction.__str__ raising TypeError when the action is a tuple.

Builds on the design started in pydoit#392.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ability to print action before executing it.

1 participant