Ian Mackinnon
Ian Mackinnon

Reputation: 14238

How to stop GNU make buffering Python output?

The following example Python program prints an updating progress bar.

progress.py :

import sys
import time

for i in range(100):
    sys.stdout.write("\r%3d%%" % (i + 1))
    sys.stdout.flush()
    time.sleep(.02)
sys.stdout.write("\n")

However, when running it through GNU Make (from Bash on Ubuntu) with the following Makefile, the output appears to be buffered until the newline character is encountered, so the progress updates are not visible. Unbuffered output is specified to Python, just to be sure.

all :
    python -u progress.py

Is there any way to get the partial-line output to be immediately visible when using make?

Upvotes: 3

Views: 659

Answers (2)

Flimm
Flimm

Reputation: 150853

I tracked down the problem to using grc, a general colouriser. make was aliased to grc make.

I use the Fish shell. To fix it, I edited /etc/grc.fish, and I removed make from the list grc_plugin_execs:

set -U grc_plugin_execs cat cvs df diff dig gcc g++ ls ifconfig \
       mount mtr netstat ping ps tail traceroute \
       wdiff blkid du dnf docker docker-compose docker-machine env id ip iostat journalctl kubectl \
       last lsattr lsblk lspci lsmod lsof getfacl getsebool ulimit uptime nmap \
       fdisk findmnt free semanage sar ss sysctl systemctl stat showmount \
       tcpdump tune2fs vmstat w who sockstat

If you use Zsh, modify /etc/grc.zsh instead.

Upvotes: 0

Ian Mackinnon
Ian Mackinnon

Reputation: 14238

I eventually tracked this down to make having been aliased with colormake in our /etc/bash.bashrc.

> type make
make is aliased to `colormake'

It seems colormake buffers output. Presumably it needs to parse a whole line before colouring it.

This was fixed by adding:

unalias make

to ~/.bashrc.

> type make
make is /usr/bin/make

Upvotes: 4

Related Questions