Skip to content

Instantly share code, notes, and snippets.

@bradneuman
Last active November 8, 2017 18:37
Show Gist options
  • Save bradneuman/3f2bcb3b3a31f00129f69fead4d94c5f to your computer and use it in GitHub Desktop.
Save bradneuman/3f2bcb3b3a31f00129f69fead4d94c5f to your computer and use it in GitHub Desktop.
Simple script to throttle ninja build output
#!/usr/bin/env python3
## simple tool to throttle the output from ninja. This is needed for compilation within a compilation buffer
## in emacs because emacs chokes with lots of long-line output. Usage of this script is:
## $ build | ninja-throttle.py
## where "build" is a build command that outputs status messages of the form:
## [93/174] Building CXX object ...
## lines matching that format will be throttles, all other lines will be printed
import sys
import re
import datetime
matcher = re.compile(r'\[([0-9]*)/([0-9*]*)\] [a-z,A-Z]*')
class ThrottlePrinter:
def __init__(self, freq_s):
self.lastPrintTime = None
self.throttle_freq = datetime.timedelta(seconds=freq_s)
def throttle(self, s):
now = datetime.datetime.now()
if not self.lastPrintTime or (now - self.lastPrintTime) >= self.throttle_freq:
self.lastPrintTime = now
sys.stdout.write(s)
# TODO: argument for frequency
printer = ThrottlePrinter(1.0)
for line in sys.stdin:
match = matcher.match(line)
if match:
printer.throttle(line)
else:
sys.stdout.write(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment