This repository has been archived by the owner on Sep 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidateDB.py
605 lines (445 loc) · 13.8 KB
/
validateDB.py
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#!/usr/bin/env python
"""
DESC: Applies static analysis in order to validate a given database file.
$Author$
$Rev$
VERSION: 1.0.2
MODIFICATION LOG:
2014/04/22 - klang - Version 1.0 released
2014/06/05 - klang - Allowed integers for menu selections,
Checks for size violations
2014/06/06 - klang - Better handles missing parentheses
2014/12/05 - klang - Help message
"""
import os
import re
import sys
import shlex
import difflib
class Token:
def __init__(self, name, matches, line):
self.name = name
self.matches = matches
self.line = line
def __str__(self):
return self.name + " (line: " + str(self.line) + ")"
def full_text(self):
return "".join([x.group() for x in self.matches])
def full_text_w_num(self):
return self.full_text() + " (line: " + str(self.line) + ")"
#Combine tokens into complex tokens
def add(self, other):
"""
Since we throw away the other token we don't
really need the temporary variables, but let's not
contaminate any data that we don't need to.
What we are doing is putting the entirety of the
other token's text and line numbers before the existing
data of those types in this token. This is because we
have to pop tokens off a stack to construct a complex
token which means they are in reverse order. So by
putting the added token at the front, we'll be in a
left-to-right order at the end.
"""
temp_matches = list(other.matches)
temp_matches.extend(self.matches)
self.matches = temp_matches
self.line = other.line
# (Regex Match, "Token Type")
db_lexicon = (
# Keywords
(re.compile(r"field"), "FIELD"),
(re.compile(r"record"), "RECORD"),
(re.compile(r"grecord"), "RECORD"),
(re.compile(r"alias"), "ALIAS"),
(re.compile(r"info"), "INFO"),
# Values
(re.compile(r"[A-Za-z][A-Za-z0-9_]*"), "NAME"),
(re.compile(r'".*"'), "STRING"),
(re.compile(r"[0-9]+"), "NUMBER"),
# Symbols
(re.compile(r":"), "COLON"),
(re.compile(r","), "COMMA"),
(re.compile(r"{"), "OPEN_BRACE"),
(re.compile(r"}"), "CLOSE_BRACE"),
(re.compile(r"\("), "OPEN_PARENS"),
(re.compile(r"\)"), "CLOSE_PARENS"),
(re.compile(r"\$"), "DOLLAR_SIGN"),
)
"""
dbd_lexicon has to parse through much larger files (iocxxxLinux.dbd is 34,000 lines),
so we are arranging the regexs by how common they are. Also, we'll tag all identifiers as
NAME, then narrow it down with a faster replacement to cut down on the number of regexs we
have to perform on every token.
"""
# (Regex Match, "Token Type")
dbd_lexicon = (
(re.compile(r"[A-Za-z][A-Za-z0-9_]*"), "NAME"),
(re.compile(r"\("), "OPEN_PARENS"),
(re.compile(r"\)"), "CLOSE_PARENS"),
(re.compile(r'".*"'), "STRING"),
(re.compile(r","), "COMMA"),
(re.compile(r"[0-9]+"), "NUMBER"),
(re.compile(r"{"), "OPEN_BRACE"),
(re.compile(r"}"), "CLOSE_BRACE"),
(re.compile(r".*"), "CATCHALL"),
)
name_replace = {
"field" : "FIELD",
"choice" : "CHOICE",
"menu" : "MENU",
"recordtype" : "RECORD_TYPE",
"prompt" : "PROMPT",
"promptgroup" :"PROMPT_GROUP",
"size": "SIZE",
"interest": "INTEREST",
"extra": "EXTRA",
"special" : "SPECIAL",
"initial" : "INITIAL",
"pp" : "PP",
"size" : "SIZE",
"base" : "BASE",
"asl" : "ASL",
"include" : "INCLUDE",
}
base_db_grammar = {
"FIELD" : {
"OPEN_PARENS" : {
"NAME" : {
"COMMA" : "FIELD_DEFINITION"}}},
"INFO" : {
"OPEN_PARENS" : {
"NAME" : {
"COMMA" : "INFO_DEFINITION"}}},
"ALIAS" : {
"OPEN_PARENS" : {
"STRING" : {
"CLOSE_PARENS" : "DEFINED_ALIAS"}}},
"RECORD" : {
"OPEN_PARENS" : {
"NAME" : {
"COMMA" : "RECORD_DEFINITION"}}},
"DOLLAR_SIGN" : {
"OPEN_PARENS" : {
"NAME" : {
"CLOSE_PARENS" : "MACRO"}}},
"MACRO" : {
"DOLLAR_SIGN" : {
"OPEN_PARENS" : {
"NAME" : {
"CLOSE_PARENS" : "MACRO"}}},
"NAME" : "MACRO",
"COLON" : "MACRO"},
"NAME" : {
"COLON" : "NAME",
"NAME" : "NAME",
"DOLLAR_SIGN" : {
"OPEN_PARENS" : {
"NAME" : {
"CLOSE_PARENS" : "MACRO"}}}},
}
base_dbd_grammar = {
"MENU" : {
"OPEN_PARENS" : {
"NAME" : {
"CLOSE_PARENS" : "MENU_DEFINITION" }}},
"MENU_DEFINITION" : {
"OPEN_BRACE" : "MENU_HEADER" },
"CHOICE" : {
"OPEN_PARENS" : {
"NAME" : {
"COMMA" : {
"STRING" : {
"CLOSE_PARENS" : "CHOICE_DEFINITION" }}}}},
"RECORD_TYPE" : {
"OPEN_PARENS" : {
"NAME" : {
"CLOSE_PARENS" : {
"OPEN_BRACE" : "RECORD_TYPE_HEADER" }}}},
"FIELD" : {
"OPEN_PARENS" : {
"NAME" : {
"COMMA" : {
"NAME" : {
"CLOSE_PARENS" : {
"OPEN_BRACE" : "FIELD_HEADER" }}}}}},
"PROMPT" : {
"OPEN_PARENS" : {
"STRING" : {
"CLOSE_PARENS" : "PROMPT_DEFINITION"}}},
"SPECIAL" : {
"OPEN_PARENS" : {
"NAME" : {
"CLOSE_PARENS" : "SPECIAL_DEFINITION"}}},
"PROMPT_GROUP" : {
"OPEN_PARENS" : {
"NAME" : {
"CLOSE_PARENS" : "PROMPT_GROUP_DEFINITION"}}},
"SIZE" : {
"OPEN_PARENS" : {
"NUMBER" : {
"CLOSE_PARENS" : "SIZE_DEFINITION"}}},
"INTEREST" : {
"OPEN_PARENS" : {
"NUMBER" : {
"CLOSE_PARENS" : "INTEREST_DEFINITION"}}},
"EXTRA" : {
"OPEN_PARENS" : {
"STRING" : {
"CLOSE_PARENS" : "EXTRA_DEFINITION"}}},
"PP" : {
"OPEN_PARENS" : {
"BOOLEAN" : {
"CLOSE_PARENS" : "PP_DEFINITION"}}},
"ASL" : {
"OPEN_PARENS" : {
"NAME" : {
"CLOSE_PARENS" : "ASL_DEFINITION"}}},
"INITIAL" : {
"OPEN_PARENS" : {
"STRING" : {
"CLOSE_PARENS" : "INITIAL_DEFINITION"}}},
"INCLUDE" : {
"STRING" : "INCLUDE_DBD"},
}
#Matches the end of field and record definitions
def search_parens(flags, starter, token_stream):
assigned = None
while token_stream:
token = token_stream.pop(0)
if token.name in ("MACRO", "NAME", "STRING", "NUMBER") and not assigned:
#Warn about un-quoted value
if token.name in ("MACRO", "NAME"):
flags["non_string_val"].append(token)
assigned = token
elif token.name == "CLOSE_PARENS" and assigned:
return starter.matches[2].group(), assigned.full_text()
else:
print "Unexpected token received: "
print "\tToken: " + token.full_text_w_num()
if assigned:
print '\tExpected: ["CLOSE_PARENS"]'
else:
print '\tExpected: ["MACRO", "NAME", "STRING", "NUMBER"]'
print
token_stream.insert(0, token)
return starter.matches[2].group(), None
print "Reached end of file without matching parens"
print "\tMatching: " + starter.full_text_w_num()
print
return starter.matches[2].group(), None
#Matches the closing brace of a definitions
def search_brace(flags, world, recordtype, recordname, starter, token_stream):
while token_stream:
token = token_stream.pop(0)
if token.name == "FIELD_DEFINITION" or token.name == "INFO_DEFINITION":
fieldname, fieldval = search_parens(flags, token, token_stream)
if fieldval:
if recordtype in world["RECORD_TYPES"].keys():
check_fields(world, recordtype, recordname, fieldname, fieldval, token)
elif recordtype not in flags["unknown_record_type"]:
flags["unknown_record_type"].append(recordtype)
elif token.name == "DEFINED_ALIAS":
continue
elif token.name == "CLOSE_BRACE":
return
else:
print "Unexpected token received: "
print "\tToken: " + token.full_text_w_num()
print
return
print "Reached end of file without matching brace"
print "\tMatching: " + starter.full_text_w_num()
print
def check_fields(world, recordtype, recordname, fieldname, fieldval, token):
if fieldname in world["RECORD_TYPES"][recordtype].keys():
fieldtype = world["RECORD_TYPES"][recordtype][fieldname]["TYPE"]
if "LENGTH" in world["RECORD_TYPES"][recordtype][fieldname].keys():
if len(fieldval.strip('"')) > world["RECORD_TYPES"][recordtype][fieldname]["LENGTH"]:
print "Field larger than allowed size: "
print "\tRecord: " + recordname
print "\tField: " + fieldname + " (line: " + str(token.line) + ")"
print "\tReceived: " + fieldval
print "\tAllowed Length: " + str(world["RECORD_TYPES"][recordtype][fieldname]["LENGTH"])
print
if fieldtype == "DBF_MENU":
menuname = world["RECORD_TYPES"][recordtype][fieldname]["CHOICES"]
if fieldval not in world["MENUS"][menuname]:
try:
selection = int(fieldval.strip('"'))
except ValueError:
selection = None
if not selection or selection >= len(world["MENUS"][menuname]):
print "Invalid menu choice: "
print "\tRecord: " + recordname
print "\tField: " + fieldname + " (line: " + str(token.line) + ")"
print "\tReceived: " + fieldval
close = difflib.get_close_matches(fieldval, world["MENUS"][menuname])
if close:
print "\tPerhaps you meant: " + str(close)
print
else:
print "Unknown field:"
print "\tRecord: " + recordname
print "\tField Received: " + fieldname + " (line: " + str(token.line) + ")"
print "\tPerhaps you meant: " + str(difflib.get_close_matches(fieldname, world["RECORD_TYPES"][recordtype].keys()))
print
def create_menu(starter, token_stream):
choices = []
while token_stream:
token = token_stream.pop(0)
if token.name == "CHOICE_DEFINITION":
choices.append(token.matches[4].group())
elif token.name == "CLOSE_BRACE":
return choices
def create_field(starter, token_stream):
data = {}
fieldname = starter.matches[2].group()
data["TYPE"] = starter.matches[4].group()
while token_stream:
token = token_stream.pop(0)
if token.name == "CLOSE_BRACE":
return fieldname, data
elif token.name == "MENU_DEFINITION":
data["CHOICES"] = token.matches[2].group()
elif token.name == "SIZE_DEFINITION":
data["LENGTH"] = int(token.matches[2].group())
def create_recordtype(starter, token_stream):
the_type = {}
while token_stream:
token = token_stream.pop(0)
if token.name == "FIELD_HEADER":
fieldname, field = create_field(token, token_stream)
the_type[fieldname] = field
elif token.name == "CLOSE_BRACE":
return the_type
def match_grammar(world, token_stream):
#To keep track of non-breaking problems
warnings = {
"non_string_val": [],
"unknown_record_type": []}
while token_stream:
token = token_stream.pop(0)
#A DB file is essentially just a list of record definitions
if token.name == "RECORD_DEFINITION":
recordtype, recordname = search_parens(warnings, token, token_stream)
#Open brace means we'll start looking for fields
if token_stream and token_stream[0].name == "OPEN_BRACE":
token_stream.pop(0)
search_brace(warnings, world, recordtype, recordname, token, token_stream)
else:
print "Unexpected token received: "
print "\tToken: " + token.full_text_w_num()
print '\tExpected: ["RECORD_DEFINITION"]'
print
return warnings
# Calls advanced grammar functions
def check_dbd(world, token_stream):
while len(token_stream):
token = token_stream.pop(0)
if token.name == "MENU_HEADER":
world["MENUS"][token.matches[2].group()] = create_menu(token, token_stream)
elif token.name == "RECORD_TYPE_HEADER":
world["RECORD_TYPES"][token.matches[2].group()] = create_recordtype(token, token_stream)
#Match a string to it's token type, returns the token type and the regex match of the string
def match(token, is_db):
if is_db:
lexicon = db_lexicon
else:
lexicon = dbd_lexicon
for regex, tok_name in lexicon:
thematch = regex.match(token)
if thematch:
if tok_name == "NAME" and is_db == False:
return name_replace.get(thematch.group(), tok_name), [thematch]
return tok_name, [thematch]
return token, []
#Matches a list of strings to tokens, returns a list of tokens
def grammate(token_stream, is_db):
if is_db:
base_grammar = base_db_grammar
else:
base_grammar = base_dbd_grammar
current_grammar = base_grammar
#Output, list of tokens
stack = []
#How many matched tokens
trail = 0
while True:
token = token_stream.get_token()
if not token:
return stack
tok_name, tok_match = match(token, is_db)
token = Token(tok_name, tok_match, token_stream.lineno)
if tok_name in current_grammar and type(current_grammar[tok_name]) == str:
token.name = current_grammar[tok_name]
for _ in range(trail):
token.add(stack.pop())
current_grammar = base_grammar
trail = 0
#Matched to complex token
if token.name in current_grammar:
trail += 1
current_grammar = current_grammar[token.name]
#Doesn't have a complex form
else:
trail = 0
current_grammar = base_grammar
stack.append(token)
def print_warnings(dbd_init, warn):
if warn["non_string_val"]:
if len(warn["non_string_val"]) > 1:
print "Warning: multiple non-quoted values found"
else:
print "Warning: non-quoted value found"
for index, token in enumerate(warn["non_string_val"]):
#Don't flood the screen with warnings
if index + 1 > 10:
print "\t..."
break
else:
print "\t" + token.full_text_w_num()
print
if warn["unknown_record_type"] and dbd_init:
if len(warn["unknown_record_type"]) > 1:
print "Warning: multiple unknown record types found"
else:
print "Warning: unknown record type found"
for index, record_type in enumerate(sorted(warn["unknown_record_type"])):
if index + 1 > 10:
print "\t..."
break
else:
print "\t" + record_type
print
def load_dbd(dbd=""):
world = {
"MENUS" : {},
"RECORD_TYPES" : {},
}
if dbd:
with open(dbd, "r") as the_file:
token_stream = grammate(shlex.shlex(the_file), False)
check_dbd(world, token_stream)
return world
if __name__ == "__main__":
if sys.argv[1] == "-h" or sys.argv[1] == "--help":
print "validateDB.py database.db [definitions.dbd]"
quit()
if not (2 <= len(sys.argv) <= 3):
print "Usage: validateDB.py database.db [definitions.dbd]"
quit()
if not os.path.isfile(sys.argv[1]):
print "No such file"
quit()
#Use dbd file if defined
if len(sys.argv) == 3:
world = load_dbd(dbd=sys.argv[2])
dbd_init = True
else:
world = load_dbd()
dbd_init = False
with open(sys.argv[1], "r") as the_file:
token_stream = grammate(shlex.shlex(the_file), True)
warn = match_grammar(world, token_stream)
print_warnings(dbd_init, warn)