-
Notifications
You must be signed in to change notification settings - Fork 0
/
nrm
executable file
·285 lines (242 loc) · 8.71 KB
/
nrm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env python3
__author__ = "kiroussa"
__version__ = "1.1.0"
__license__ = "Public Domain"
import sys
import os
from dataclasses import dataclass
from pathlib import Path
from norminette.lexer import Lexer, Token
from norminette.context import Context
from norminette.registry import Registry
import halo
from halo._utils import colored_frame, encode_utf_8_text
import signal
MAX_NORM_ERRORS = 10
FILE_TIMEOUT = 2
class Colors:
""" ANSI color codes """
BLACK = "\033[0;30m"
RED = "\033[0;31m"
GREEN = "\033[0;32m"
BROWN = "\033[0;33m"
BLUE = "\033[0;34m"
PURPLE = "\033[0;35m"
CYAN = "\033[0;36m"
LIGHT_GRAY = "\033[0;37m"
DARK_GRAY = "\033[1;30m"
LIGHT_RED = "\033[1;31m"
LIGHT_GREEN = "\033[1;32m"
YELLOW = "\033[1;33m"
LIGHT_BLUE = "\033[1;34m"
LIGHT_PURPLE = "\033[1;35m"
LIGHT_CYAN = "\033[1;36m"
LIGHT_WHITE = "\033[1;37m"
BOLD = "\033[1m"
FAINT = "\033[2m"
ITALIC = "\033[3m"
UNDERLINE = "\033[4m"
BLINK = "\033[5m"
NEGATIVE = "\033[7m"
CROSSED = "\033[9m"
END = "\033[0m"
# cancel SGR codes if we don't write to a terminal
if not __import__("sys").stdout.isatty():
for _ in dir():
if isinstance(_, str) and _[0] != "_":
locals()[_] = ""
else:
# set Windows console in VT mode
if __import__("platform").system() == "Windows":
kernel32 = __import__("ctypes").windll.kernel32
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
del kernel32
__shared_registry = Registry()
@dataclass
class NormOutput:
file: Path
position: tuple[int, int]
error_type: str
message: str
is_error: bool
def format(self):
return f"{self.error_type} (at {self.position[0]}:{self.position[1]}): {self.message}"
def run_norminette(file: Path, extra: list[str] = None) -> list[NormOutput]:
"""Runs the norminette on a file and returns the output.
Parameters
----------
file : Path
The file to run the norminette on.
extra : list[str], optional
Extra arguments to pass to the norminette, by default None
Returns
-------
list[NormOutput]
The norminette output.
Raises
------
TokenError
If the file could not be parsed.
CParsingError
If the file could not be parsed.
IOError
If the file could not be read or is invalid.
TimeoutError
If the norminette took too long to run.
"""
output: list[NormOutput] = []
target = str(file)
tokens: list[Token] = []
if target[-2:] not in [".c", ".h"]:
raise IOError("Target is not a valid C source/header file")
with open(target) as f:
source = f.read()
try:
lexer = Lexer(source)
tokens = lexer.get_tokens()
except KeyError as e:
raise IOError("Error while parsing file", e)
context = Context(target, tokens, added_value=extra)
def alarm_handler(signum, frame):
sys.stdout = sys.__stdout__
raise TimeoutError("Timeout while parsing file")
signal.signal(signal.SIGALRM, alarm_handler)
signal.alarm(FILE_TIMEOUT)
sys.stdout = open(os.devnull, "w")
__shared_registry.run(context, source)
sys.stdout = sys.__stdout__
signal.alarm(0)
for error in context.errors:
output.append(
NormOutput(
file,
(error.line if error.line is not None else -1, error.col if error.col is not None else -1),
error.errno,
# Capitalize for consistency
error.error_msg[0].upper() error.error_msg[1:],
True
)
)
for warning in context.warnings:
output.append(
NormOutput(
file,
(warning.line if warning.line is not None else -1, warning.col if warning.col is not None else -1),
warning.errno,
# Capitalize for consistency
warning.error_msg[0].upper() warning.error_msg[1:],
False
)
)
return output
def log(message: str, prefix: str = ">", spaces: int = 0):
print(f"\r{' ' * spaces}{prefix} {message}")
def info(message: str, spaces: int = 0):
log(message, prefix=f"{Colors.LIGHT_BLUE}i{Colors.END}", spaces=spaces)
def warn(message: str, spaces: int = 0):
log(message, prefix=f"{Colors.YELLOW}!{Colors.END}", spaces=spaces)
def error(message: str, spaces: int = 0):
log(message, prefix=f"{Colors.RED}x{Colors.END}", spaces=spaces)
def fatal(message: str, spaces: int = 0):
log(message, prefix=f"{Colors.RED}{Colors.BOLD}X{Colors.END}", spaces=spaces)
def main():
path = '.'
if len(sys.argv) > 1:
path = sys.argv[1]
if path == '-v' or path == '--version':
print(f"nrm v{__version__}")
sys.exit(0)
if path == '-V' or path == '--verbose':
from importlib.metadata import version
print(f"nrm v{__version__} by {__author__}")
print(f"Licensed under the {__license__}")
for pkg in ["halo", "norminette"]:
print(f" {pkg} {version(pkg)}")
sys.exit(0)
if len(sys.argv) > 2:
fatal("Too many arguments")
fatal(f"Usage: {sys.argv[0]} [path]")
sys.exit(1)
target_files = []
if not os.path.exists(path):
fatal(f"Error: {path} does not exist")
sys.exit(1)
if os.path.isfile(path):
target_files.append(Path(path))
else:
for root, dirs, files in os.walk(path):
for file in files:
if "MacroLibX" in root: continue
target_files.append(Path(root) / file)
# horrible code ahead --- don't look
def _frame(spinner):
frames = spinner._spinner["frames"]
frame = frames[spinner._frame_index]
if spinner._color:
frame = colored_frame(frame, spinner._color)
text_frame = spinner.text_frame()
return "{0} {1}".format(
*[
(text_frame, frame)
if spinner._placement == "right"
else (frame, text_frame)
][0]
)
def force_frame(spinner):
spinner.clear()
frame = _frame(spinner)
output = "\r{}".format(frame)
try:
spinner._write(output)
except UnicodeEncodeError:
spinner._write(encode_utf_8_text(output))
# end of horrible code
with halo.Halo(text="Running norminette...", spinner="dots") as spinner:
checked = 0
errored = 0
for file in target_files:
if file.name.endswith(".c") or file.name.endswith(".h"):
try:
spinner.text = f"Running norminette on {Colors.LIGHT_GRAY}{file}{Colors.END}..."
force_frame(spinner)
checked = 1
output = run_norminette(file)
print("\r\033[K", end="")
if len(output) != 0:
has_error = any([error.is_error for error in output])
if not has_error:
continue
errored = 1
log(f"{Colors.BOLD}{file}{Colors.END}:")
total_errors = 0
for e in output:
if e.is_error:
total_errors = 1
if total_errors > MAX_NORM_ERRORS:
info(f"{MAX_NORM_ERRORS} norm errors, skipping...", spaces=4)
break
error(e.format(), spaces=2)
for e in output:
if not e.is_error:
warn(e.format(), spaces=2)
except Exception as e:
print("\r\033[K", end="")
error(f"Error while checking {Colors.LIGHT_GRAY}{file}{Colors.END}: {Colors.RED}{str(e)}")
continue
except KeyboardInterrupt:
print("\r\033[K", end="")
fatal("Interrupted")
break
except:
print("\r\033[K", end="")
fatal("Unknown error")
break
if errored == 0:
spinner.succeed(f"{Colors.BOLD}All files follow the Norm.{Colors.END}{Colors.LIGHT_GRAY} ({checked} checked){Colors.END}")
else:
msg = "files do not"
if errored == 1:
msg = "file doesn't"
spinner.fail(f"{Colors.RED}{errored} {msg} follow the Norm.{Colors.END}{Colors.LIGHT_GRAY} ({checked} checked){Colors.END}")
if __name__ == "__main__":
main()