limit_backend.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #
  2. # Metrix++, Copyright 2009-2019, Metrix++ Project
  3. # Link: https://github.com/metrixplusplus/metrixplusplus
  4. #
  5. # This file is a part of Metrix++ Tool.
  6. #
  7. import logging
  8. import re
  9. import mpp.api
  10. import mpp.utils
  11. import mpp.cout
  12. class Plugin(mpp.api.Plugin, mpp.api.IConfigurable):
  13. MODE_NEW = 0x01
  14. MODE_TREND = 0x03
  15. MODE_TOUCHED = 0x07
  16. MODE_ALL = 0x15
  17. def declare_configuration(self, parser):
  18. self.parser = parser
  19. parser.add_option("--hotspots", "--hs", default=None, help="If not set (none), all exceeded limits are printed."
  20. " If set, exceeded limits are sorted (the worst is the first) and only first HOTSPOTS limits are printed."
  21. " [default: %default]", type=int)
  22. parser.add_option("--disable-suppressions", "--ds", action="store_true", default=False,
  23. help = "If not set (none), all suppressions are ignored"
  24. " and associated warnings are printed. [default: %default]")
  25. parser.add_option("--warn-mode", "--wm", default='all', choices=['new', 'trend', 'touched', 'all'],
  26. help="Defines the warnings mode. "
  27. "'all' - all warnings active, "
  28. "'new' - warnings for new regions/files only, "
  29. "'trend' - warnings for new regions/files and for bad trend of modified regions/files, "
  30. "'touched' - warnings for new and modified regions/files "
  31. "[default: %default]")
  32. parser.add_option("--min-limit", "--min", action="multiopt",
  33. help="A threshold per 'namespace:field' metric in order to select regions, "
  34. "which have got metric value less than the specified limit. "
  35. "This option can be specified multiple times, if it is necessary to apply several limits. "
  36. "Should be in the format: <namespace>:<field>:<limit-value>[:region_type[,region_type]], for example: "
  37. "'std.code.lines:comments:1', or 'std.code.lines:comments:1:function,class'. "
  38. "Region types is optional specifier, and if not defined the limit is applied to regions of all types.")
  39. parser.add_option("--max-limit", "--max", action="multiopt",
  40. help="A threshold per 'namespace:field' metric in order to select regions, "
  41. "which have got metric value more than the specified limit. "
  42. "This option can be specified multiple times, if it is necessary to apply several limits. "
  43. "Should be in the format: <namespace>:<field>:<limit-value>[:region_type[,region_type]], for example: "
  44. "'std.code.complexity:cyclomatic:7', or 'std.code.complexity:maxdepth:5:function'. "
  45. "Region types is optional specifier, and if not defined the limit is applied to regions of all types.")
  46. def configure(self, options):
  47. self.hotspots = options.__dict__['hotspots']
  48. self.no_suppress = options.__dict__['disable_suppressions']
  49. if options.__dict__['warn_mode'] == 'new':
  50. self.mode = self.MODE_NEW
  51. elif options.__dict__['warn_mode'] == 'trend':
  52. self.mode = self.MODE_TREND
  53. elif options.__dict__['warn_mode'] == 'touched':
  54. self.mode = self.MODE_TOUCHED
  55. elif options.__dict__['warn_mode'] == 'all':
  56. self.mode = self.MODE_ALL
  57. if self.mode != self.MODE_ALL and options.__dict__['db_file_prev'] == None:
  58. self.parser.error("option --warn-mode: The mode '" + options.__dict__['warn_mode'] + "' requires '--db-file-prev' option set")
  59. class Limit(object):
  60. def __init__(self, limit_type, limit, namespace, field, db_filter, region_types, original):
  61. self.type = limit_type
  62. self.limit = limit
  63. self.namespace = namespace
  64. self.field = field
  65. self.filter = db_filter
  66. self.region_types = region_types
  67. self.original = original
  68. def __repr__(self):
  69. return "'{0}:{1}' {2} {3} [applied to '{4}' region type(s)]".format(
  70. self.namespace, self.field, self.filter[1], self.limit,
  71. mpp.api.Region.T().to_str(self.region_types))
  72. self.limits = []
  73. pattern = re.compile(r'''([^:]+)[:]([^:]+)[:]([-+]?[0-9]+(?:[.][0-9]+)?)(?:[:](.+))?''')
  74. if options.__dict__['max_limit'] != None:
  75. for each in options.__dict__['max_limit']:
  76. match = re.match(pattern, each)
  77. if match == None:
  78. self.parser.error("option --max-limit: Invalid format: " + each)
  79. region_types = 0x00
  80. if match.group(4) != None:
  81. for region_type in match.group(4).split(','):
  82. region_type = region_type.strip()
  83. group_id = mpp.api.Region.T().from_str(region_type)
  84. if group_id == None:
  85. self.parser.error(
  86. "option --max-limit: uknown region type (allowed: global, class, struct, namespace, function, interface, any): " + region_type)
  87. region_types |= group_id
  88. else:
  89. region_types = mpp.api.Region.T().ANY
  90. limit = Limit("max", float(match.group(3)), match.group(1), match.group(2),
  91. (match.group(2), '>', float(match.group(3))), region_types, each)
  92. self.limits.append(limit)
  93. if options.__dict__['min_limit'] != None:
  94. for each in options.__dict__['min_limit']:
  95. match = re.match(pattern, each)
  96. if match == None:
  97. self.parser.error("option --min-limit: Invalid format: " + each)
  98. region_types = 0x00
  99. if match.group(4) != None:
  100. for region_type in match.group(4).split(','):
  101. region_type = region_type.strip()
  102. group_id = mpp.api.Region.T().from_str(region_type)
  103. if group_id == None:
  104. self.parser.error(
  105. "option --max-limit: uknown region type (allowed: global, class, struct, namespace, function, interface, any): " + region_type)
  106. region_types |= group_id
  107. else:
  108. region_types = mpp.api.Region.T().ANY
  109. limit = Limit("min", float(match.group(3)), match.group(1), match.group(2),
  110. (match.group(2), '<', float(match.group(3))), region_types, each)
  111. self.limits.append(limit)
  112. def initialize(self):
  113. super(Plugin, self).initialize()
  114. loader_prev = self.get_plugin('mpp.dbf').get_loader_prev()
  115. loader = self.get_plugin('mpp.dbf').get_loader()
  116. self._verify_namespaces(loader.iterate_namespace_names())
  117. for each in loader.iterate_namespace_names():
  118. self._verify_fields(each, loader.get_namespace(each).iterate_field_names())
  119. # Try to optimise iterative change scans
  120. self.modified_file_ids = None
  121. if self.mode != self.MODE_ALL:
  122. self.modified_file_ids = self._get_list_of_modified_files(loader, loader_prev)
  123. def _verify_namespaces(self, valid_namespaces):
  124. valid = []
  125. for each in valid_namespaces:
  126. valid.append(each)
  127. for each in self.limits:
  128. if each.namespace not in valid:
  129. self.parser.error("option --{0}-limit: metric '{1}:{2}' is not available in the database file.".
  130. format(each.type, each.namespace, each.field))
  131. def _verify_fields(self, namespace, valid_fields):
  132. valid = []
  133. for each in valid_fields:
  134. valid.append(each)
  135. for each in self.limits:
  136. if each.namespace == namespace:
  137. if each.field not in valid:
  138. self.parser.error("option --{0}-limit: metric '{1}:{2}' is not available in the database file.".
  139. format(each.type, each.namespace, each.field))
  140. def _get_list_of_modified_files(self, loader, loader_prev):
  141. logging.info("Identifying changed files...")
  142. old_files_map = {}
  143. for each in loader_prev.iterate_file_data():
  144. old_files_map[each.get_path()] = each.get_checksum()
  145. if len(old_files_map) == 0:
  146. return None
  147. modified_file_ids = []
  148. for each in loader.iterate_file_data():
  149. if len(modified_file_ids) > 1000: # If more than 1000 files changed, skip optimisation
  150. return None
  151. if (each.get_path() not in list(old_files_map.keys())) or old_files_map[each.get_path()] != each.get_checksum():
  152. modified_file_ids.append(str(each.get_id()))
  153. old_files_map = None
  154. if len(modified_file_ids) != 0:
  155. modified_file_ids = " , ".join(modified_file_ids)
  156. modified_file_ids = "(" + modified_file_ids + ")"
  157. return modified_file_ids
  158. return None
  159. def _is_metric_suppressed(self, metric_namespace, metric_field, loader, select_data):
  160. data = loader.load_file_data(select_data.get_path())
  161. if select_data.get_region() != None:
  162. data = data.get_region(select_data.get_region().get_id())
  163. sup_data = data.get_data('std.suppress', 'list')
  164. else:
  165. sup_data = data.get_data('std.suppress.file', 'list')
  166. if sup_data != None and sup_data.find('[' + metric_namespace + ':' + metric_field + ']') != -1:
  167. return True
  168. return False
  169. def iterate_limits(self):
  170. for each in self.limits:
  171. yield each
  172. def is_mode_matched(self, limit, value, diff, is_modified):
  173. if is_modified == None:
  174. # means new region, True in all modes
  175. return True
  176. if self.mode == self.MODE_ALL:
  177. return True
  178. if self.mode == self.MODE_TOUCHED and is_modified == True:
  179. return True
  180. if self.mode == self.MODE_TREND and is_modified == True:
  181. if limit < value and diff > 0:
  182. return True
  183. if limit > value and diff < 0:
  184. return True
  185. return False
  186. def get_warnings(self, path, limit):
  187. class Warning (object):
  188. def __init__(self, path, cursor, namespace, field, region_name,
  189. stat_level, trend_value, stat_limit,
  190. is_modified, is_suppressed):
  191. self.path = path
  192. self.cursor = cursor
  193. self.namespace = namespace
  194. self.field = field
  195. self.region_name = region_name
  196. self.stat_level = stat_level
  197. self.trend_value = trend_value
  198. self.stat_limit = stat_limit
  199. self.is_modified = is_modified
  200. self.is_suppressed = is_suppressed
  201. loader_prev = self.get_plugin('mpp.dbf').get_loader_prev()
  202. loader = self.get_plugin('mpp.dbf').get_loader()
  203. warnings = []
  204. filters = [limit.filter]
  205. if self.modified_file_ids != None:
  206. filters.append(('file_id', 'IN', self.modified_file_ids))
  207. sort_by = None
  208. limit_by = None
  209. limit_warnings = None
  210. if self.hotspots != None:
  211. sort_by = limit.field
  212. if limit.type == "max":
  213. sort_by = "-" + sort_by
  214. if self.mode == self.MODE_ALL:
  215. # if it is not ALL mode, the tool counts number of printed warnings below
  216. limit_by = self.hotspots
  217. limit_warnings = self.hotspots
  218. selected_data = loader.load_selected_data(limit.namespace,
  219. fields = [limit.field],
  220. path=path,
  221. filters = filters,
  222. sort_by=sort_by,
  223. limit_by=limit_by)
  224. if selected_data == None:
  225. mpp.utils.report_bad_path(path)
  226. return None
  227. for select_data in selected_data:
  228. if limit_warnings != None and limit_warnings <= 0:
  229. break
  230. is_modified = None
  231. diff = None
  232. file_data = loader.load_file_data(select_data.get_path())
  233. file_data_prev = loader_prev.load_file_data(select_data.get_path())
  234. if file_data_prev != None:
  235. if file_data.get_checksum() == file_data_prev.get_checksum():
  236. diff = 0
  237. is_modified = False
  238. else:
  239. matcher = mpp.utils.FileRegionsMatcher(file_data, file_data_prev)
  240. prev_id = matcher.get_prev_id(select_data.get_region().get_id())
  241. if matcher.is_matched(select_data.get_region().get_id()):
  242. if matcher.is_modified(select_data.get_region().get_id()):
  243. is_modified = True
  244. else:
  245. is_modified = False
  246. diff = mpp.api.DiffData(select_data,
  247. file_data_prev.get_region(prev_id)).get_data(limit.namespace, limit.field)
  248. if (self.is_mode_matched(limit.limit,
  249. select_data.get_data(limit.namespace, limit.field),
  250. diff,
  251. is_modified) == False):
  252. continue
  253. is_sup = self._is_metric_suppressed(limit.namespace, limit.field, loader, select_data)
  254. if is_sup == True and self.no_suppress == False:
  255. continue
  256. region_cursor = 0
  257. region_name = None
  258. if select_data.get_region() != None:
  259. if select_data.get_region().get_type() & limit.region_types == 0:
  260. continue
  261. region_cursor = select_data.get_region().cursor
  262. region_name = select_data.get_region().name
  263. warnings.append(Warning(select_data.get_path(),
  264. region_cursor,
  265. limit.namespace,
  266. limit.field,
  267. region_name,
  268. select_data.get_data(limit.namespace, limit.field),
  269. diff,
  270. limit.limit,
  271. is_modified,
  272. is_sup))
  273. if limit_warnings != None:
  274. limit_warnings -= 1
  275. return warnings
  276. def get_all_warnings(self, path):
  277. """ returns all warnings from a specified path """
  278. warnings = []
  279. for limit in self.iterate_limits():
  280. warnings = warnings + self.get_warnings(path, limit)
  281. return warnings