2

I'm working on a wrapper script for invocations to the Ninja c/c++ buildsystem, the script is in python and one thing it should do is to log the output from Ninja and the underlying compiler but without supressing standard output.

The part that gives me trouble is that Ninja seems to detect that it is writing to a terminal or not, so simply catching the output and sending it to standard output ends up changing it (most notably, Ninja does not fill the screen with lists of warning and errorless buildfiles but removes the line of the last successfully built translation unit as a new one comes in). Is there any way to let Ninja write to the terminal, while still capturing its output? The writing to the terminal should happen as the Ninja subprocess runs, but the capturing of said output may wait until the subprocess has completed.

Pierre Andersson
  • 320
  • 2
  • 12

1 Answers1

3

pty.spawn() allows you to log output to a file while hoodwinking Ninja subprocess into thinking that it works with a terminal (tty):

import os
import pty

logfile = open('logfile', 'wb')

def read(fd):
    data = os.read(fd, 1024)
    logfile.write(data)
    return data

pty.spawn("ninja", read)
jfs
  • 399,953
  • 195
  • 994
  • 1,670