tags:

views:

178

answers:

2

I am trying to make a simple script to automate and log synchronization via Unison. I am also using subprocess.Popen rather than the usual os.system call as it's deprecated. I've spent the past 2 days looking at docs and such trying to figure out what I'm doing wrong, but for some reason if I call unison from a terminal it works no problem, yet when I make the same call from Python it tries to do user interaction and in addition I'm not capturing but about half of the output, the other is still printing to the terminal.

Here is my code I am trying to use:

sync = Popen(["unison", "sync"], shell = True, stdout = PIPE)

for line in sync.stdout
    logFile.write(line)

sync.wait()

if sync.returncode == 0 or sync.returncode == None:
    logFile.write("Sync Completed Successfully\n")
else
    logFile.write("!! Sync Failed with a returncode of: " + str(sync.returncode) + "\n")

Here is my Unison config file:

root = /home/zephyrxero/temp/
root = /home/zephyrxero/test/

auto = true
batch = true
prefer = newer
times = true
owner = true
group = true
retry = 2

What am I doing wrong? Why isn't all output from Unison getting saved to my logfile, and why is it asking me for confirmation when the script runs, but not when I run it plainly from a terminal?

UPDATE: Ok, thanks to Emil I'm now capturing all the output, but I still can't figure out why typing "unison sync" into a terminal is getting different results than when calling it from my script.

A: 

The most likely culprit is that unison is sending some output to stderr instead of just stdout. Popen takes an additional stderr argument so you can try capturing that instead of (or in addition to) stdout).

For a quick reference on standard streams see Wikipedia.

Emil
I had tried piping stderr before, but for some reason it didn't seem to work...may have been before I figured out it was waiting for input...hmm...seems to be capturing all the output now.
Derick Eisenhardt
A: 

Changed ["unison", "sync"] to simply ["unison sync"]...appears to be working without need for user interaction now, not sure why that would be any different...but works for me.

Derick Eisenhardt