MCPcopy Create free account
hub / github.com/Tencent/CodeAnalysis / RulesToVisitors

Class RulesToVisitors

client/tool/findbugs.py:27–79  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

25
26
27class RulesToVisitors(object):
28 def __init__(self, tool_name: str):
29 self.tool_name = tool_name
30 self.tool_home = f"{tool_name.upper()}_HOME"
31
32 # 获取检测类到检测模式的映射
33 def class_to_pattern(self, tool_xml_data, visitors_to_pattern=None):
34 raw_material = ET.fromstring(tool_xml_data)
35 if not visitors_to_pattern:
36 visitors_to_pattern = {}
37 for detector in raw_material.iter("Detector"):
38 detector_name = detector.attrib.get("class")
39 report_patterns = detector.attrib.get("reports")
40 acutal_visitor = detector_name[detector_name.rfind(".") + 1 :]
41 if report_patterns:
42 visitors_to_pattern[acutal_visitor] = report_patterns.split(",")
43 return visitors_to_pattern
44
45 def search_File(self, path, _format):
46 file_list = []
47 for root, _, files in os.walk(path):
48 for fp in files:
49 if fp.endswith(_format):
50 file_list.append(os.path.join(root, fp))
51 return file_list
52
53 def get_needed_visitors(self, pattern=None):
54 """
55 通过缺陷模式匹配对应的规则列表
56 :param pattern:缺陷模式名
57 :return:匹配的规则列表
58 """
59 if not pattern:
60 # 全量规则扫描
61 return None
62
63 tool_jar = os.path.join(os.environ[self.tool_home], "lib", f"{self.tool_name}.jar")
64 zfile = zipfile.ZipFile(tool_jar, "r")
65 tool_xml_data = zfile.read("findbugs.xml")
66 whole_patterns = self.class_to_pattern(tool_xml_data)
67
68 # 获取自定义规则的配置信息
69 plugin_list = self.search_File(os.path.join(os.environ[self.tool_home], "plugin"), ".jar")
70 for plugin in plugin_list:
71 plugin_file = zipfile.ZipFile(plugin, "r")
72 plugin_tool_xml_data = plugin_file.read("findbugs.xml")
73 whole_patterns = self.class_to_pattern(plugin_tool_xml_data, whole_patterns)
74
75 visitors = []
76 for key, value in whole_patterns.items():
77 if list(set(value) & set(pattern)):
78 visitors.append(key)
79 return visitors
80
81
82class Findbugs(CodeLintModel):

Callers 2

analyzeMethod · 0.90
analyzeMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected