daily pastebin goal
4%
SHARE
TWEET

Untitled

a guest May 16th, 2018 108 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. # makefilter.py: version 1.3
  4.  
  5. # Copyright 2009 Andrew J. Bennieston. All rights reserved.
  6.  
  7. # Redistribution and use in source and binary forms, with or without
  8. # modification, are permitted provided that the following conditions
  9. # are met:
  10.  
  11. #   1. Redistributions of source code must retain the above copyright
  12. #      notice, this list of conditions and the following disclaimer.
  13. #   2. Redistributions in binary form must reproduce the above copyright
  14. #      notice, this list of conditions and the following disclaimer in
  15. #      the documentation and/or other materials provided with the
  16. #      distribution.
  17.  
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY
  19. # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  21. # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR
  22. # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  26. # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  
  30. import os
  31. import sys
  32. from popen2 import Popen4
  33.  
  34. #MAKEPROGRAM = 'make'
  35. MAKEPROGRAM = '/usr/bin/make' # Use this version if makefilter.py is called 'make'
  36.  
  37. class TerminalOutput(object):
  38.     def __init__(self):
  39.         self.fgcolormap = {
  40.             'black'     :   '30',
  41.             'red'       :   '31',
  42.             'green'     :   '32',
  43.             'yellow'    :   '33',
  44.             'blue'      :   '34',
  45.             'magenta'   :   '35',
  46.             'cyan'      :   '36',
  47.             'white'     :   '37'
  48.             }
  49.         self.bgcolormap = {
  50.             'black'     :   '40',
  51.             'red'       :   '41',
  52.             'green'     :   '42',
  53.             'yellow'    :   '43',
  54.             'blue'      :   '44',
  55.             'magenta'   :   '45',
  56.             'cyan'      :   '46',
  57.             'white'     :   '47'
  58.             }
  59.         self.attrmap = {
  60.             'reset'     :   '0',
  61.             'bright'    :   '1',
  62.             'dim'       :   '2',
  63.             'underline' :   '3',
  64.             'blink'     :   '5',
  65.             'reverse'   :   '7',
  66.             'hidden'    :   '8'
  67.             }
  68.    
  69.     def writeColored(self, str, attr, fg, bg=None):
  70.         colorStr = '\033[' + self.attrmap[attr] + ';' + self.fgcolormap[fg]
  71.         if bg is not None:
  72.             colorStr += ';' + self.bgcolormap[bg]
  73.         colorStr += 'm'
  74.         sys.stdout.write(colorStr + str)
  75.    
  76.     def write(self, str):
  77.         colorStr = '\033[0m'
  78.         sys.stdout.write(colorStr + str)
  79.  
  80.     def space(self):
  81.         sys.stdout.write(' ')
  82.    
  83.     def newline(self):
  84.         sys.stdout.write('\n')
  85.        
  86. class MakeFilter(object):
  87.     def __init__(self):
  88.         self.compilers = [
  89.             'gcc', 'g++', 'cc', 'cxx', 'icc', 'icpc', '/bin/sh', 'distcc'
  90.             ]
  91.         self.tools = [
  92.             'make', 'ld', 'ln', 'rm'
  93.             ]
  94.         self.flags = [
  95.             '-g', '-ggdb', '-pedantic', '-pedantic-errors', '-c',
  96.             '-shared', '-ansi'
  97.             ]
  98.         self.source = None
  99.         self.process = None
  100.         self.printer = TerminalOutput()
  101.  
  102.     def run(self, args=[]):
  103.         make_command = MAKEPROGRAM
  104.         if len(args):
  105.             make_command += " '" + "' '".join(args) + "'"
  106.         self.process = Popen4(make_command)
  107.         self.source = self.process.fromchild
  108.         try:
  109.             line = self.source.readline()
  110.             while len(line):
  111.                 line = line.strip()
  112.                 parts = line.split(' ')
  113.                 if parts[0] in self.compilers:
  114.                     self.processCompilerLine(line)
  115.                 elif parts[0].split(':')[0] in self.tools:
  116.                     self.processToolLine(line)
  117.                 else:
  118.                     self.printer.write(line)
  119.                 self.printer.newline()
  120.                 line = self.source.readline()
  121.         except KeyboardInterrupt:
  122.             os.kill(self.process.pid, 15)
  123.         self.source.close()
  124.    
  125.     def processCompilerLine(self, line):
  126.         parts = line.split(' ')
  127.         self.printer.writeColored(parts[0], 'reset', 'green')
  128.         self.printer.space()
  129.         parts = parts[1:]
  130.         isOutputFile = False
  131.         for part in parts:
  132.             if len(part) is 0:
  133.                 continue
  134.             if isOutputFile:
  135.                 self.printer.writeColored(part, 'bright', 'cyan')
  136.                 isOutputFile = False
  137.             elif part[:2] == '-O':
  138.                 self.printer.writeColored(part, 'reset', 'red')
  139.             elif part[:2] == '-W' or part[:2] == '-f':
  140.                 self.printer.writeColored(part, 'reset', 'yellow')
  141.             elif part[:2] == '-D':
  142.                 self.printer.writeColored(part, 'reset', 'green')
  143.             elif part[:2] == '-L' or part[:2] == '-l' or part[:2] == '-I':
  144.                 self.printer.writeColored(part, 'bright', 'blue')
  145.             elif part == '-o':
  146.                 self.printer.writeColored(part, 'bright', 'cyan')
  147.                 isOutputFile = True
  148.             elif part in self.flags:
  149.                 self.printer.writeColored(part, 'reset', 'magenta')
  150.             elif part[:5] == '-std=':
  151.                 # Extra highlighting for language standard specifications
  152.                 self.printer.writeColored(part[:5], 'reset', 'magenta')
  153.                 self.printer.writeColored(part[5:], 'bright', 'magenta')
  154.             else:
  155.                 self.printer.writeColored(part, 'dim','white')
  156.             self.printer.space()
  157.    
  158.     def processToolLine(self, line):
  159.         parts = line.split(' ')
  160.         self.printer.writeColored(parts[0], 'reset', 'green')
  161.         self.printer.space()
  162.         parts = parts[1:]
  163.         for part in parts:
  164.             if len(part) is 0:
  165.                 continue
  166.             if part[0] == '-': # Colorize options to tools
  167.                 self.printer.writeColored(part, 'reset', 'yellow')
  168.             else:
  169.                 self.printer.writeColored(part, 'dim', 'white')
  170.             self.printer.space()
  171.  
  172. if __name__ == '__main__':
  173.     filter = MakeFilter()
  174.     filter.run(sys.argv[1:])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top