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

Log command being executed #474

Open
yajo opened this issue Oct 16, 2019 · 4 comments
Open

Log command being executed #474

yajo opened this issue Oct 16, 2019 · 4 comments

Comments

@yajo
Copy link

yajo commented Oct 16, 2019

In my bash scripts I can use set -x at the beginning, so bash keeps printing the command being executed.

I found nothing similar in plumbum. I even tried with:

logging.getLogger("plumbum.local").setLevel(logging.DEBUG)

... no luck.

There should be a standard and documented way to make plumbum print the command it's going to execute. I guess a logger should do the trick. Example:

logging.getLogger("plumbum.command").setLevel(logging.INFO)  # This prints just the command to be executed
logging.getLogger("plumbum.command").setLevel(logging.DEBUG)  # This prints additional details, and also prints when the command has finished

Thanks!

@AndydeCleyre
Copy link
Contributor

Have you seen the LocalCommand's formulate method? You might do something like this:

In [1]: from plumbum.cmd import uptime

In [2]: up = uptime['--pretty']

In [3]: up()
Out[3]: 'up 1 day, 23 minutes\n'

In [4]: up.run_fg()
up 1 day, 23 minutes

In [5]: import sys

In [6]: def logrun(cmd, fg=False):
   ...:     print(cmd.formulate(), file=sys.stderr)
   ...:     if not fg:
   ...:         return cmd()
   ...:     cmd.run_fg()
   ...:

In [7]: logrun(up)
['/bin/uptime', '--pretty']
Out[7]: 'up 1 day, 25 minutes\n'

In [8]: logrun(up, fg=True)           
['/bin/uptime', '--pretty']
up 1 day, 28 minutes

@yajo
Copy link
Author

yajo commented Nov 17, 2019

Oh yes, it seems pretty close to what I want.

However I'm thinking on a replacement of Bash's set -x, which always outputs commands before their execution.

For things like CI environments, where you usually want to know at which step a given command has failed, it's very useful. I miss an equivalent in plumbum, which I think could be supported upstream with a logger that always logs the command if instructed. It could be also attached to the machine log level. Something like:

import logging
from plumbum import local, SshMachine

local.log_level = logging.DEBUG
remote = SshMachine("example.com")
remote.log_level = logging.INFO

@imperialguy
Copy link

This is a very important feature. And it's missing. Must be made available as part of the package. Something similar to below could help build the feature into the package:
https://gist.github.com/bgreenlee/1402841

@8cylinder
Copy link

I'm not sure if this is what you are looking for, but I've used shlex to generate "copy & pastable" commands. Shlex takes an array (such as the output of formulate) and creates a command with proper quoting, essentially the same as set -x.

import shlex

cmd = local['ls']['-l', 'some file with spaces']
print(shlex.join(cmd.formulate()))

that outputs: /usr/bin/ls -l 'some file with spaces'

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

No branches or pull requests

4 participants