Debian Codegraph @

share/analysis-scripts/results_display.py

raw
1 ##########################################################################
2 # #
3 # This file is part of Frama-C. #
4 # #
5 # Copyright (C) 2007-2022 #
6 # CEA (Commissariat à l'énergie atomique et aux énergies #
7 # alternatives) #
8 # #
9 # you can redistribute it and/or modify it under the terms of the GNU #
10 # Lesser General Public License as published by the Free Software #
11 # Foundation, version 2.1. #
12 # #
13 # It is distributed in the hope that it will be useful, #
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16 # GNU Lesser General Public License for more details. #
17 # #
18 # See the GNU Lesser General Public License version 2.1 #
19 # for more details (enclosed in the file licenses/LGPLv2.1). #
20 # #
21 ##########################################################################
22
23 import re
24 import sys
25 import string
26 import curses
27
28
29 sensitivity = 0.02
30
31
32 class ResultsFormatter(string.Formatter):
33 @staticmethod
34 def format_memory(kilobytes):
35 if kilobytes < 4096:
36 return str(kilobytes) + " kiB"
37 megabytes = round(kilobytes / 1024)
38 if megabytes < 4096:
39 return str(megabytes) + " MiB"
40 gigabytes = round(megabytes / 1024)
41 return str(gigabytes) + " GiB"
42
43 @staticmethod
44 def format_time(seconds):
45 if seconds < 10:
46 return str(round(seconds, 2)) + "s"
47 if seconds < 100:
48 return str(round(seconds, 1)) + "s"
49 if seconds < 600:
50 return str(round(seconds)) + "s"
51 minutes = round(seconds / 60)
52 if minutes < 600:
53 return str(minutes) + "m"
54 hours = round(minutes / 60)
55 return str(hours) + "h"
56
57 @staticmethod
58 def attribute(value, inverted):
59 if value > sensitivity:
60 return "@-" if inverted else "@+"
61 elif value < -sensitivity:
62 return "@+" if inverted else "@-"
63 else:
64 return "@="
65
66 def get_field(self, field_name, args, kwargs):
67 try:
68 return super().get_field(field_name, args, kwargs)
69 except (KeyError, AttributeError):
70 return None, field_name
71
72 def format_field(self, value, format_spec):
73 if value is None:
74 return ""
75 elif format_spec.startswith("+cmp:"):
76 remainder = format_spec.split("+cmp:",1)[1]
77 return (self.attribute(value, False) +
78 self.format_field(value, remainder) + "@=")
79 elif format_spec.startswith("-cmp:"):
80 remainder = format_spec.split("-cmp:",1)[1]
81 return (self.attribute(value, True) +
82 self.format_field(value, remainder) + "@=")
83 elif format_spec == "time":
84 return self.format_time(value)
85 elif format_spec == "memory":
86 return self.format_memory(value)
87 else:
88 return super().format_field(value, format_spec)
89
90
91 class UserExitRequest(Exception):
92 pass
93
94
95 class PlainDisplay:
96 NEGATIVE = 1
97 POSITIVE = 2
98 RUNNING = 3
99 HEADER = 4
100
101 columns = [
102 {"size":64, "caption":"Case", "format":"{target_name:s}"},
103 {"size":14, "caption":"Coverage",
104 "format":"{coverage:>8.0%} {diff_coverage:+cmp:+.0%}"},
105 {"size":14, "caption":"Alarms",
106 "format":"{alarms:>8d} {diff_alarms:-cmp:+d}"},
107 {"size":14 , "caption":"Warnings",
108 "format":"{warnings:>8d} {diff_warnings:-cmp:+d}"},
109 {"size":14, "caption":"Time",
110 "format":"{user_time:time} {diff_user_time:-cmp:+.0%}"},
111 {"size":14, "caption":"Memory",
112 "format":"{memory:memory} {diff_memory:-cmp:+.0%}"}]
113
114 def __init__(self):
115 self.NEGATIVE = 0
116 self.POSITIVE = 0
117 self.RUNNING = 0
118 self.HEADER = 0
119 self.OBSOLETE = 0
120 self.needs_update = False
121
122 def write(self, text, attributes=0):
123 sys.stdout.write(text)
124
125 def rich_write(self, text, override=None, size=0):
126 attributes = 0
127 n = 0
128 for s in re.split(r"(@.)", text):
129 if s == "@=":
130 attributes = 0
131 elif s == "@+":
132 attributes = self.POSITIVE
133 elif s == "@-":
134 attributes = self.NEGATIVE
135 else:
136 n += len(s)
137 self.write(s, attributes if override is None else override)
138 if n < size:
139 self.write(" " * (size - n), attributes if override is None else override)
140
141 fmt = ResultsFormatter()
142
143 def format(self, *args, **kwargs):
144 return self.fmt.format(*args, **kwargs)
145
146 def print_table(self, results):
147 self.write(" ", self.HEADER)
148 for column in self.columns:
149 self.write(self.format("{caption:^{size}}", **column), self.HEADER)
150 self.write(" ", self.HEADER)
151
152 self.write("\n-")
153 for column in self.columns:
154 self.write(self.format("{:-^{size}}", "", **column))
155 self.write("-")
156
157 self.write("\n")
158 for result in results:
159 self.write(" ")
160 for column in self.columns:
161 s = self.fmt.format(column["format"], **result)
162 if result["is_running"]:
163 attribute = self.RUNNING
164 elif not result["up_to_date"]:
165 attribute = self.OBSOLETE
166 else:
167 attribute = None
168 self.rich_write(s, attribute, size=column["size"])
169
170 self.write(" ")
171 self.write("\n")
172
173 self.write("\n")
174 self.needs_update = False
175
176 def process_inputs(self):
177 pass
178
179
180 class CursesDisplay(PlainDisplay):
181 def __init__(self, stdscr):
182 self.stdscr = stdscr
183 # curses.mousemask(curses.ALL_MOUSE_EVENTS)
184 stdscr.nodelay(True)
185 stdscr.refresh() # Needs to be done once or nothing will be output
186
187 self.window = curses.newpad(400, 160)
188 curses.init_color(curses.COLOR_YELLOW, 300, 300, 300)
189 curses.init_pair(1, curses.COLOR_RED, 0)
190 curses.init_pair(2, curses.COLOR_GREEN, 0)
191 curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_YELLOW)
192 curses.init_pair(4, curses.COLOR_YELLOW, 0)
193 self.NEGATIVE = curses.color_pair(1)
194 self.POSITIVE = curses.color_pair(2)
195 self.RUNNING = curses.A_BLINK | curses.color_pair(3)
196 self.HEADER = curses.A_BOLD
197 self.OBSOLETE = curses.color_pair(4)
198 self.scroll_y = 0
199
200 def write(self, text, attributes=0):
201 self.window.addstr(text, attributes)
202
203 def print_table(self, results):
204 self.window.clear()
205 PlainDisplay.print_table(self, results)
206 height, width = self.stdscr.getmaxyx()
207 try:
208 self.window.refresh(0, 0, 0, 0, 1, width - 1)
209 self.window.refresh(self.scroll_y + 2, 0, 2, 0, height - 1, width - 1)
210 except Exception:
211 # getmaxyx may be out of date, especially when resizing down the
212 # window ; just ignore errors
213 pass
214
215 def process_inputs(self):
216 previous_y = self.scroll_y
217 c = self.stdscr.getch()
218 while c != -1:
219 if c == ord("q"):
220 raise UserExitRequest
221 elif c == curses.KEY_UP:
222 self.scroll_y -= 1
223 elif c == curses.KEY_DOWN:
224 self.scroll_y += 1
225 elif c == curses.KEY_NPAGE:
226 self.scroll_y += 10
227 elif c == curses.KEY_PPAGE:
228 self.scroll_y -= 10
229 elif c == curses.KEY_MOUSE:
230 id, x, y, z, bstate = curses.getmouse()
231 if z > 0:
232 self.scroll_y += 1
233 elif z < 0:
234 self.scroll_y -= 1
235 self.scroll_y = max(0, self.scroll_y)
236 c = self.stdscr.getch()
237 if self.scroll_y != previous_y:
238 self.needs_update = True
239
240
241 def wrapper(f, *args, **kwargs):
242 if "curses" in kwargs:
243 use_curses = kwargs["curses"]
244 del kwargs["curses"]
245 else:
246 use_curses = False
247
248 if use_curses:
249
250 def g(stdscr):
251 nonlocal f, args, kwargs
252 display = CursesDisplay(stdscr)
253 return f(display, *args, **kwargs)
254
255 return curses.wrapper(g)
256 else:
257 display = PlainDisplay()
258 return f(display, *args, **kwargs)