limit.py 17 KB

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