Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.08 KB | None | 0 0
  1. import sublime, sublime_plugin
  2.  
  3. import os
  4. import sys
  5. import subprocess
  6. import xml.etree.ElementTree as ET
  7.  
  8. from collections import defaultdict
  9.  
  10. class Error:
  11.     def __init__(self, id, severity, msg, file, line):
  12.         self.id = id
  13.         self.msg = msg
  14.         self.severity = severity
  15.         self.file = file
  16.         self.line = line
  17.  
  18.     def __eq__(self, other):
  19.         return (isinstance(other, self.__class__)
  20.             and self.__dict__ == other.__dict__)
  21.  
  22.     def __ne__(self, other):
  23.         return not __eq__(other)
  24.  
  25.     def __hash__(self):
  26.         return hash((self.id, self.msg, self.severity, self.file, self.line))
  27.  
  28.     def __str__(self):
  29.         return "{0}, {1}\n {2}, {3}, {4}\n".format(self.file, self.line, self.severity, self.id, self.msg)
  30.  
  31. # make args to cppcheck
  32. def process_file(path):
  33.     if not path.endswith(('.cpp', '.c', '.h')):
  34.         return None
  35.    
  36.     command = ' '.join(["cppcheck",
  37.                         "--enable=warning,style,information,performance",
  38.                         "--xml-version=2",
  39.                         "--template=gcc",
  40.                         path])
  41.  
  42.     res = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
  43.     return res.communicate()[0]
  44.  
  45. def parse_output(output):
  46.     errors = []
  47.     root = ET.fromstring(output)
  48.     for err in root.iter('error'):
  49.         id = err.get('id')
  50.         msg = err.get('msg')
  51.         severity = err.get('severity')
  52.         try:
  53.             file = err.find('location').get('file')
  54.             line = err.find('location').get('line')
  55.             errors.append(Error(id, severity, msg, file, line))
  56.         except AttributeError:
  57.             pass
  58.        
  59.     return errors
  60.  
  61. def find_errors(msg, lst):
  62.     if msg != None:
  63.         out = parse_output(msg)
  64.         lst.extend(out)
  65.     return lst
  66.  
  67. path = ""
  68.  
  69. class CheckCommand(sublime_plugin.TextCommand):
  70.     def run(self, edit, **args):
  71.         file_path = self.view.file_name()
  72.         v = self.view.window().new_file()
  73.  
  74.         errors_list = []
  75.        
  76.         if args['use_dir']:
  77.             path = os.path.dirname(file_path)
  78.             for root, dirs, files in os.walk(path):
  79.                 for file in files:
  80.                     output = process_file(os.path.join(root, file))
  81.                     errors_list = find_errors(output, errors_list)
  82.         else:
  83.             path = file_path
  84.             output = process_file(path)
  85.             errors_list = find_errors(output, errors_list)
  86.        
  87.         pt1 = pt2 = 0
  88.         errors_d = defaultdict(int)
  89.         errors_set = set(errors_list)
  90.         for err in errors_set:
  91.             errors_d[err.severity] += 1
  92.             pt2 = pt1 + v.insert(edit, pt1, str(err) + '\n')
  93.             v.add_regions('err' + str(pt2), [sublime.Region(pt1, pt2)])
  94.             pt1 = pt2
  95.         pt2 = pt1 + v.insert(edit, pt1, "Found " + str(len(errors_list)) + " errors\n")
  96.         pt1 = pt2
  97.         for key, val in errors_d.items():
  98.             pt2 = pt1 + v.insert(edit, pt1, str(key) + ": " + str(val) + "\n")
  99.             pt1 = pt2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement