Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to print action before executing it. #390

Open
Jasha10 opened this issue Apr 8, 2021 · 6 comments
Open

Ability to print action before executing it. #390

Jasha10 opened this issue Apr 8, 2021 · 6 comments

Comments

@Jasha10
Copy link
Contributor

Jasha10 commented Apr 8, 2021

Feature request: ability to print some data about each action before it gets executed.

Motivation:

If my task has multiple actions (and verbosity==2), I can't tell which output came from which action (assuming that the actions all succeed). Some way to tell which output comes from which action would be great. Printing e.g. the shell command that will be run, before running it, would be a good way to achieve this.

If you write echo 123 as part of a recipe in a Makefile (using GNU make), the following is printed:

+ echo 123
123

This is great for debugging purposes, so that you will know what command has produced the output you are seeing. The + symbol prefix makes it easy to tell what is the command, and what is the output from the command.

Possible API design:

Perhaps this could be verbosity level 3?

For CmdAction, it should be easy to print the action that will be executed by Popen, just before it gets executed.

For PythonAction, it is not as clear what should be the output. Perhaps a str representation of the python-action and it's args and kwargs...

Describe alternatives you've considered

I have tried using the title keyword for the task, with a custom title printer:

# dodo.py
import doit

def show_cmd(task):
    result = "EXECUTING " + task.name
    for action in task.actions:
        if isinstance(action, doit.action.CmdAction):
            result += "\n+ " + action.expand_action()
        if isinstance(action, doit.action.PythonAction):
            result += f"\n+ {action.py_callable.__name__}(*{action.args}, **{action._prepare_kwargs()})"
    return result

def py_action(a=1, b=3):
    print(f"in py_action: {a=}, {b=}")

def task_custom_display():
    return {"actions": ["echo abc efg", (py_action, (2,), {"b": 4})], "title": show_cmd}

However, when I run doit --verbosity 2 on this dodo.py file, the results are out of order:

$ doit --verbosity 2
.  EXECUTING custom_display
+ echo abc efg
+ py_action(*(2,), **{'b': 4})
abc efg
in py_action: a=2, b=4

I still cannot tell where the stdout from the first action ("abc efg") end, and the stdout from the second action ("in py_action: a=2, b=4") begins. This is why I'm requesting this feature.

Fund with Polar
@schettino72
Copy link
Member

If it is just for debugging you can easily:

'actions' : ['echo Executing cmd X', 'X',
                 'echo Executing function foo(), 'foo()']

title is for a task, so it wont work for your use-case.

I dont think messing with verbosity it would be a good idea, adding a new parameter would be ok.

For PythonAction, it is not as clear what should be the output. Perhaps a str representation of the python-action and it's args and kwargs...

I guess this would be a good default, but it should be possible to overwrite it (implemented as class method).

TODO:

  • add new command line option --show-action (better name suggestions welcome)
  • implement a title() method for PythonAction & CmdAction
  • Reporter will also need a new method to be called before each action

@maximlt
Copy link

maximlt commented Jan 27, 2022

This would be very useful for debugging tasks that you've not created yourself but inherited from another package.

@schettino72
Copy link
Member

@maximlt there is PR open for that.

I cant remember which state was it... does that work for you?
Did it implement my spec above?
Maybe you can help review it...

@maximlt
Copy link

maximlt commented Jan 28, 2022

Sorry @schettino72 I've got really low bandwidth these days, hopefully some time later! Thanks for your prompt answer anyway.

@awesomescot
Copy link

I would find this useful also. 👍

@Jasha10
Copy link
Contributor Author

Jasha10 commented Dec 28, 2023

Feel free to pick up the partial PR #392. Sorry to say I won't be able to finish the PR myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants