0

I have a post-receive hook on my git bare repo that excute this fabfile.py:

def deploy():
    print(cyan('Pulling changes to local server'))

    with cd('/var/www')
        local('git pull')

It executes fine, but it shows the output for the local command before the print, like this:

remote: From /media/projetos/repositorios/test
remote:    b057a4b..02d85b3  master     -> origin/master
remote: Updating b057a4b..02d85b3
remote: Fast-forward
remote:  .../test/template/catalog/product/list.phtml     | 98 +++++++++++-----------
remote:  1 file changed, 47 insertions(+), 51 deletions(-)
remote: Pulling changes to local server

If i just ssh on the server and run fab deploy it works normal.

Diego Castro
  • 2,046
  • 2
  • 21
  • 28

1 Answers1

1

Add a sys.stdout.flush() call after printing, so that even if output is fully buffered (as it is on a pipe, which is the case when running under a git hook), it is sent to the output stream at that point, before running another command.

torek
  • 448,244
  • 59
  • 642
  • 775
  • Thank you, that did solved the problem but if I add new print statement I have to flush the output every time. Is there a way to fix this without calling sys.stdout.flush() for every print? – Diego Castro Jan 31 '14 at 18:07
  • 1
    You only need to `flush` before doing something that will write directly to the underlying stdout (such as running another command as above). Or, see related question: [http://stackoverflow.com/questions/107705/python-output-buffering](http://stackoverflow.com/q/107705/1256452) – torek Jan 31 '14 at 18:28
  • Thank you again, I was able to solve it by putting `sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)` on the beginning of the file. – Diego Castro Jan 31 '14 at 18:39
  • 1
    Note that (per related question) this `os.fdopen` will raise a `ValueError` in python 3. You might want to use the wrapper method, which will work in both python 2 and python 3. – torek Jan 31 '14 at 18:44