Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fabric - Is there any way to capture run stdout?

Tags:

python

fabric

I'm trying to do the following:

output = run("ls -l backups") for line in output.split("/n"):     do_stuff(line) 

Any way of having the stdout of ls sent to output?


To be more specific I'm using a CLI app called s3cmd which does something similar to ls, but with remote Amazon S3 buckets.

So a replacement for ls won't help unfortunately.


like image 612
RadiantHex Avatar asked Jan 29 '14 22:01

RadiantHex


2 Answers

Exactly what you are asking for should be happening. From the docs:

run will return the result of the remote program’s stdout as a single (likely multiline) string.

run(), and related commands like local() and sudo(), return an _AttributeString object that is just a wrapper around stdout with attribute access to additional information like failure/success booleans, stderr, the command run, etc. The result object also has a stdout attribute, which is just more explicit.

To troubleshoot, print type(output), output to be sure the response is what you expect. Examine output.failed and output.stderr. It could be the command isn't doing what you expect, there is no "backups" directory, etc.

like image 114
JCotton Avatar answered Oct 12 '22 21:10

JCotton


Try as below using String IO

from fabric.api import * from StringIO import StringIO  fh = StringIO() run("ls -l backups", stdout=fh)  fh.seek(0) for line in fh.readlines():     do_stuff(line) 
like image 36
user1406490 Avatar answered Oct 12 '22 21:10

user1406490