Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # makefilter.py: version 1.3
- # Copyright 2009 Andrew J. Bennieston. All rights reserved.
- # Redistribution and use in source and binary forms, with or without
- # modification, are permitted provided that the following conditions
- # are met:
- # 1. Redistributions of source code must retain the above copyright
- # notice, this list of conditions and the following disclaimer.
- # 2. Redistributions in binary form must reproduce the above copyright
- # notice, this list of conditions and the following disclaimer in
- # the documentation and/or other materials provided with the
- # distribution.
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY
- # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR
- # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
- # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- import os
- import sys
- from popen2 import Popen4
- #MAKEPROGRAM = 'make'
- MAKEPROGRAM = '/usr/bin/make' # Use this version if makefilter.py is called 'make'
- class TerminalOutput(object):
- def __init__(self):
- self.fgcolormap = {
- 'black' : '30',
- 'red' : '31',
- 'green' : '32',
- 'yellow' : '33',
- 'blue' : '34',
- 'magenta' : '35',
- 'cyan' : '36',
- 'white' : '37'
- }
- self.bgcolormap = {
- 'black' : '40',
- 'red' : '41',
- 'green' : '42',
- 'yellow' : '43',
- 'blue' : '44',
- 'magenta' : '45',
- 'cyan' : '46',
- 'white' : '47'
- }
- self.attrmap = {
- 'reset' : '0',
- 'bright' : '1',
- 'dim' : '2',
- 'underline' : '3',
- 'blink' : '5',
- 'reverse' : '7',
- 'hidden' : '8'
- }
- def writeColored(self, str, attr, fg, bg=None):
- colorStr = '\033[' + self.attrmap[attr] + ';' + self.fgcolormap[fg]
- if bg is not None:
- colorStr += ';' + self.bgcolormap[bg]
- colorStr += 'm'
- sys.stdout.write(colorStr + str)
- def write(self, str):
- colorStr = '\033[0m'
- sys.stdout.write(colorStr + str)
- def space(self):
- sys.stdout.write(' ')
- def newline(self):
- sys.stdout.write('\n')
- class MakeFilter(object):
- def __init__(self):
- self.compilers = [
- 'gcc', 'g++', 'cc', 'cxx', 'icc', 'icpc', '/bin/sh', 'distcc'
- ]
- self.tools = [
- 'make', 'ld', 'ln', 'rm'
- ]
- self.flags = [
- '-g', '-ggdb', '-pedantic', '-pedantic-errors', '-c',
- '-shared', '-ansi'
- ]
- self.source = None
- self.process = None
- self.printer = TerminalOutput()
- def run(self, args=[]):
- make_command = MAKEPROGRAM
- if len(args):
- make_command += " '" + "' '".join(args) + "'"
- self.process = Popen4(make_command)
- self.source = self.process.fromchild
- try:
- line = self.source.readline()
- while len(line):
- line = line.strip()
- parts = line.split(' ')
- if parts[0] in self.compilers:
- self.processCompilerLine(line)
- elif parts[0].split(':')[0] in self.tools:
- self.processToolLine(line)
- else:
- self.printer.write(line)
- self.printer.newline()
- line = self.source.readline()
- except KeyboardInterrupt:
- os.kill(self.process.pid, 15)
- self.source.close()
- def processCompilerLine(self, line):
- parts = line.split(' ')
- self.printer.writeColored(parts[0], 'reset', 'green')
- self.printer.space()
- parts = parts[1:]
- isOutputFile = False
- for part in parts:
- if len(part) is 0:
- continue
- if isOutputFile:
- self.printer.writeColored(part, 'bright', 'cyan')
- isOutputFile = False
- elif part[:2] == '-O':
- self.printer.writeColored(part, 'reset', 'red')
- elif part[:2] == '-W' or part[:2] == '-f':
- self.printer.writeColored(part, 'reset', 'yellow')
- elif part[:2] == '-D':
- self.printer.writeColored(part, 'reset', 'green')
- elif part[:2] == '-L' or part[:2] == '-l' or part[:2] == '-I':
- self.printer.writeColored(part, 'bright', 'blue')
- elif part == '-o':
- self.printer.writeColored(part, 'bright', 'cyan')
- isOutputFile = True
- elif part in self.flags:
- self.printer.writeColored(part, 'reset', 'magenta')
- elif part[:5] == '-std=':
- # Extra highlighting for language standard specifications
- self.printer.writeColored(part[:5], 'reset', 'magenta')
- self.printer.writeColored(part[5:], 'bright', 'magenta')
- else:
- self.printer.writeColored(part, 'dim','white')
- self.printer.space()
- def processToolLine(self, line):
- parts = line.split(' ')
- self.printer.writeColored(parts[0], 'reset', 'green')
- self.printer.space()
- parts = parts[1:]
- for part in parts:
- if len(part) is 0:
- continue
- if part[0] == '-': # Colorize options to tools
- self.printer.writeColored(part, 'reset', 'yellow')
- else:
- self.printer.writeColored(part, 'dim', 'white')
- self.printer.space()
- if __name__ == '__main__':
- filter = MakeFilter()
- filter.run(sys.argv[1:])
Add Comment
Please, Sign In to add comment