blob: f171d9bdb140a617714f1e250daafadbb250c0a9 [file] [log] [blame]
Michael Schaffner8ac6c4c2020-03-03 15:00:20 -08001# Copyright lowRISC contributors.
2# Licensed under the Apache License, Version 2.0, see LICENSE for details.
3# SPDX-License-Identifier: Apache-2.0
4r"""
5Class describing lint configuration object
6"""
7
Rupert Swarbrick6cc20112020-04-24 09:44:35 +01008import hjson
Michael Schaffner8ac6c4c2020-03-03 15:00:20 -08009import logging as log
Michael Schaffner8ac6c4c2020-03-03 15:00:20 -080010from pathlib import Path
11
12from tabulate import tabulate
13
Udi Jonnalagaddadf49fb82020-03-17 11:05:17 -070014from OneShotCfg import OneShotCfg
Srikrishna Iyer559daed2020-12-04 18:22:18 -080015from utils import VERBOSE, print_msg_list, subst_wildcards
Michael Schaffner8ac6c4c2020-03-03 15:00:20 -080016
Michael Schaffner373f6d12020-07-10 14:46:34 -070017
Michael Schaffner8ac6c4c2020-03-03 15:00:20 -080018class LintCfg(OneShotCfg):
19 """Derivative class for linting purposes.
20 """
Rupert Swarbricka23dfec2020-09-07 10:01:28 +010021
22 flow = 'lint'
23
24 def __init__(self, flow_cfg_file, hjson_data, args, mk_config):
Michael Schaffner4204a8d2020-04-06 13:04:41 -070025 # This is a lint-specific attribute
26 self.is_style_lint = ""
Rupert Swarbricka23dfec2020-09-07 10:01:28 +010027 super().__init__(flow_cfg_file, hjson_data, args, mk_config)
Michael Schaffner4204a8d2020-04-06 13:04:41 -070028
29 # Convert to boolean
30 if self.is_style_lint == "True":
31 self.is_style_lint = True
32 else:
33 self.is_style_lint = False
34
Michael Schaffner8ac6c4c2020-03-03 15:00:20 -080035 # Set the title for lint results.
Michael Schaffner4204a8d2020-04-06 13:04:41 -070036 if self.is_style_lint:
37 self.results_title = self.name.upper() + " Style Lint Results"
38 else:
39 self.results_title = self.name.upper() + " Lint Results"
Michael Schaffner8ac6c4c2020-03-03 15:00:20 -080040
Michael Schaffner8ac6c4c2020-03-03 15:00:20 -080041 def gen_results_summary(self):
42 '''
43 Gathers the aggregated results from all sub configs
44 '''
45
Michael Schaffnerb60aaa52020-03-31 18:38:46 -070046 # Generate results table for runs.
Michael Schaffner8ac6c4c2020-03-03 15:00:20 -080047 log.info("Create summary of lint results")
48
49 results_str = "## " + self.results_title + " (Summary)\n\n"
Michael Schaffnercb61dc42020-07-10 16:36:39 -070050 results_str += "### " + self.timestamp_long + "\n"
Srikrishna Iyerdddf93b2020-12-04 18:24:46 -080051 if self.revision:
52 results_str += "### " + self.revision + "\n"
Srikrishna Iyer559daed2020-12-04 18:22:18 -080053 results_str += "### Branch: " + self.branch + "\n"
Michael Schaffnercb61dc42020-07-10 16:36:39 -070054 results_str += "\n"
55
Michael Schaffner8ac6c4c2020-03-03 15:00:20 -080056 header = [
Michael Schaffner62607392021-01-28 22:04:14 -080057 "Name", "Tool Warnings", "Tool Errors", "Lint Warnings",
Michael Schaffner8ac6c4c2020-03-03 15:00:20 -080058 "Lint Errors"
59 ]
60 colalign = ("center", ) * len(header)
61 table = [header]
62
63 for cfg in self.cfgs:
64
65 results_page = cfg.results_server_dir + '/results.html'
66 results_page_url = results_page.replace(
67 cfg.results_server_prefix, cfg.results_server_url_prefix)
68 name_with_link = "[" + cfg.name.upper(
69 ) + "](" + results_page_url + ")"
70 table.append([
71 name_with_link,
72 str(len(cfg.result_summary["warnings"])) + " W",
73 str(len(cfg.result_summary["errors"])) + " E",
74 str(len(cfg.result_summary["lint_warnings"])) + " W",
75 str(len(cfg.result_summary["lint_errors"])) + " E"
76 ])
77
78 if len(table) > 1:
79 self.results_summary_md = results_str + tabulate(
80 table, headers="firstrow", tablefmt="pipe",
81 colalign=colalign) + "\n"
82 else:
83 self.results_summary_md = results_str + "\nNo results to display.\n"
84
85 print(self.results_summary_md)
86
87 # Return only the tables
88 return self.results_summary_md
89
Rupert Swarbricka2892a52020-10-12 08:50:08 +010090 def _gen_results(self, results):
Michael Schaffner8ac6c4c2020-03-03 15:00:20 -080091 # '''
92 # The function is called after the regression has completed. It looks
93 # for a regr_results.hjson file with aggregated results from the lint run.
94 # The hjson needs to have the following (potentially empty) fields
95 #
96 # {
97 # tool: ""
98 # errors: []
99 # warnings: []
100 # lint_errors: []
101 # lint_warning: []
102 # lint_infos: []
103 # }
104 #
105 # where each entry is a string representing a lint message. This allows
106 # to reuse the same LintCfg class with different tools since just the
107 # parsing script that transforms the tool output into the hjson above
108 # needs to be adapted.
109 #
Scott Johnsonfe79c4b2020-07-08 10:31:08 -0700110 # note that if this is a primary config, the results will
Michael Schaffner8ac6c4c2020-03-03 15:00:20 -0800111 # be generated using the _gen_results_summary function
112 # '''
113
114 # Generate results table for runs.
Michael Schaffner8ac6c4c2020-03-03 15:00:20 -0800115 results_str = "## " + self.results_title + "\n\n"
116 results_str += "### " + self.timestamp_long + "\n"
Srikrishna Iyerdddf93b2020-12-04 18:24:46 -0800117 if self.revision:
118 results_str += "### " + self.revision + "\n"
Srikrishna Iyer559daed2020-12-04 18:22:18 -0800119 results_str += "### Branch: " + self.branch + "\n"
Michael Schaffner8ac6c4c2020-03-03 15:00:20 -0800120 results_str += "### Lint Tool: " + self.tool.upper() + "\n\n"
121
Michael Schaffner8ac6c4c2020-03-03 15:00:20 -0800122 header = [
123 "Build Mode", "Tool Warnings", "Tool Errors", "Lint Warnings",
124 "Lint Errors"
125 ]
126 colalign = ("center", ) * len(header)
127 table = [header]
128
129 # aggregated counts
130 self.result_summary["warnings"] = []
131 self.result_summary["errors"] = []
132 self.result_summary["lint_warnings"] = []
133 self.result_summary["lint_errors"] = []
134
135 fail_msgs = ""
136 for mode in self.build_modes:
137
138 result_data = Path(
139 subst_wildcards(self.build_dir, {"build_mode": mode.name}) +
140 '/results.hjson')
Srikrishna Iyer559daed2020-12-04 18:22:18 -0800141 log.info("[results:hjson]: [%s]: [%s]", self.name, result_data)
Michael Schaffner8ac6c4c2020-03-03 15:00:20 -0800142
143 try:
Michael Schaffner065297d2020-07-14 21:05:55 -0700144 with result_data.open() as results_file:
Michael Schaffner8ac6c4c2020-03-03 15:00:20 -0800145 self.result = hjson.load(results_file, use_decimal=True)
146 except IOError as err:
147 log.warning("%s", err)
148 self.result = {
149 "tool": "",
150 "errors": ["IOError: %s" % err],
151 "warnings": [],
152 "lint_errors": [],
153 "lint_warnings": [],
154 "lint_infos": []
155 }
156 if self.result:
157 table.append([
158 mode.name,
159 str(len(self.result["warnings"])) + " W ",
160 str(len(self.result["errors"])) + " E",
161 # We currently do not publish these infos at
162 # the moment len(self.result["lint_infos"]),
163 str(len(self.result["lint_warnings"])) + " W",
164 str(len(self.result["lint_errors"])) + " E"
165 ])
166 else:
167 self.result = {
168 "tool": "",
169 "errors": [],
170 "warnings": [],
171 "lint_errors": [],
172 "lint_warnings": [],
173 "lint_infos": []
174 }
175
176 self.result_summary["warnings"] += self.result["warnings"]
177 self.result_summary["errors"] += self.result["errors"]
178 self.result_summary["lint_warnings"] += self.result[
179 "lint_warnings"]
180 self.result_summary["lint_errors"] += self.result["lint_errors"]
181
182 # Append detailed messages if they exist
Michael Schaffner62607392021-01-28 22:04:14 -0800183 hdr_key_pairs = [("Tool Warnings", "warnings"),
184 ("Tool Errors", "errors"),
185 ("Lint Warnings", "lint_warnings"),
186 ("Lint Errors", "lint_errors")]
Michael Schaffner8fc927c2020-06-22 15:43:32 -0700187
Michael Schaffner0409c2c2020-07-14 22:19:40 -0700188 # Lint fails if any warning or error message has occurred
189 self.errors_seen = False
Michael Schaffner62607392021-01-28 22:04:14 -0800190 for _, key in hdr_key_pairs:
Michael Schaffner8fc927c2020-06-22 15:43:32 -0700191 if key in self.result:
Michael Schaffner0409c2c2020-07-14 22:19:40 -0700192 if self.result.get(key):
Michael Schaffner62607392021-01-28 22:04:14 -0800193 self.errors_seen = True
Michael Schaffner0409c2c2020-07-14 22:19:40 -0700194 break
Michael Schaffner8fc927c2020-06-22 15:43:32 -0700195
Michael Schaffner62607392021-01-28 22:04:14 -0800196 if self.errors_seen:
Michael Schaffner16102e52020-06-24 11:28:37 -0700197 fail_msgs += "\n### Errors and Warnings for Build Mode `'" + mode.name + "'`\n"
Michael Schaffner62607392021-01-28 22:04:14 -0800198 for hdr, key in hdr_key_pairs:
Michael Schaffner8fc927c2020-06-22 15:43:32 -0700199 msgs = self.result.get(key)
Michael Schaffner16102e52020-06-24 11:28:37 -0700200 fail_msgs += print_msg_list("#### " + hdr, msgs, self.max_msg_count)
Michael Schaffner8ac6c4c2020-03-03 15:00:20 -0800201
202 if len(table) > 1:
203 self.results_md = results_str + tabulate(
204 table, headers="firstrow", tablefmt="pipe",
Michael Schaffner373f6d12020-07-10 14:46:34 -0700205 colalign=colalign) + "\n"
206
207 # the email and published reports will default to self.results_md if they are
208 # empty. in case they need to be sanitized, override them and do not append
209 # detailed messages.
210 if self.sanitize_email_results:
211 self.email_results_md = self.results_md
212 if self.sanitize_publish_results:
213 self.publish_results_md = self.results_md
214 # locally generated result always contains all details
215 self.results_md += fail_msgs
Michael Schaffner8ac6c4c2020-03-03 15:00:20 -0800216 else:
217 self.results_md = results_str + "\nNo results to display.\n"
Michael Schaffner373f6d12020-07-10 14:46:34 -0700218 self.email_results_md = self.results_md
219 self.publish_results_md = self.results_md
Michael Schaffner8ac6c4c2020-03-03 15:00:20 -0800220
Michael Schaffner8ac6c4c2020-03-03 15:00:20 -0800221 return self.results_md