fab-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Fab-user] Collecting output when using Fabric directly in python


From: Yann Malet
Subject: Re: [Fab-user] Collecting output when using Fabric directly in python
Date: Tue, 18 Aug 2009 12:00:52 -0400

Jeff,

This is nice improvement but I would like to see if possible is to capture both stdout and stderr into 2 python variables once a fabric Task is executed. Could you please help me to understand how you would do this and ideally without touching the fabfile.py

I am going to take an example using 2 files :

fabfile.py
---------8<-------------------------
from fabric.api import *
def hello_world():
    """
    Say hello to the world
    """
    print "hello world"
---------8<-------------------------

fabfile_runner.py
---------8<-------------------------
from fabfile import hello_world
output = None
error = None
hello_world()
print "output : %s" %output
print "error : %s" %error
---------8<-------------------------

It would be wonderful if you could explain me how I could  collect output and error by only modifying the file called fabfile_runner.py. Idealy the fabric task itself could be unmodified, this would allow to upload the the factory the same file that you have tested manually.

Thank you very much for your kind explanation.
Regards,
Yann

On Tue, Aug 18, 2009 at 11:04 AM, Jeff Forcier <address@hidden> wrote:
On Sun, Aug 16, 2009 at 1:57 PM, Steve
Steiner<address@hidden> wrote:

> Can this be done more nicely as a decorator since we've already got a Python
> 2.5+ requirement anyway?
>
> @capture_stderr
> def func_that_wants_stderr(...)
>        pass

The main problem with this is that the stdout/stderr capturing is
per-run/sudo invocation and not per-task, so that doesn't mesh with
how I currently organize or think about things.

That said, it did make me realize that there *might* be a use case for
making the settings() context manager (and/or others) into a decorator
in some fashion. In other words, if one currently needed to write
this:

   def myfunc():
       with settings(warn_only=True):
           # stuff

one could write the below modification, which would save a level of
indentation if you always need the entirety of the task "wrapped" in a
given settings change:

  address@hidden(warn_only=True)
   def myfunc():
       # stuff

So if we were to create a global setting for what run/sudo/local
capture, one could then do basically what you describe:

  address@hidden(capture_stderr=True)
   def myfunc():
       this_would_be_stderr = run('foo')

This is all just off the top of my head, still settling in after a
morning of traveling =)

Best,
Jeff


reply via email to

[Prev in Thread] Current Thread [Next in Thread]