-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestrunner.py
More file actions
91 lines (80 loc) · 3.14 KB
/
testrunner.py
File metadata and controls
91 lines (80 loc) · 3.14 KB
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
from pathlib import Path
from textparser import parse2json
from pyparsing import ParseException
from datetime import datetime
import json
import argparse
from shutil import copyfile
ap = argparse.ArgumentParser()
ap.add_argument('src', help='EM TXT input directory')
ap.add_argument('dst', help='JSON output parent directory')
ap.add_argument('-b', '--base',
help='set specific baseline directory')
ap.add_argument('-q', '--quick',
help='ignore footnotes to save a bit of time',
action='store_true')
args = ap.parse_args()
srcdir = Path(args.src)
dstdir = Path(args.dst)
dtfmt = '%Y-%m-%d_%H-%M-%S'
dt = datetime.now().strftime(dtfmt)
resultsdir = dstdir / dt
investigatedir = resultsdir / 'investigate'
# choose most recent dir for baseline
datedirs = [(datetime.strptime(d.name, dtfmt), d)
for d in dstdir.glob('[0-9][0-9][0-9][0-9]-*')
if d.is_dir()]
datedirs.sort(reverse=True, key=lambda p: p[0])
_, basedir = datedirs[0]
# ... unless otherwise specified
if args.base is not None:
basedir = Path(args.base)
diffFile = investigatedir / 'diff-log.txt'
investigatedir.mkdir(parents=True)
def makecopy(file: Path) -> None:
copypath = investigatedir / file.name
copyfile(str(file), str(copypath))
with open(str(diffFile), 'w') as df:
for emTxt in srcdir.glob('uksiem_*.txt'):
emJson = emTxt.with_suffix('.json').name
result = resultsdir / emJson
base = basedir / emJson
baseExists = base.exists()
extraMsg = ''
processFtnotes = not args.quick
print('Processing ' + emTxt.name)
try:
extraMsg = parse2json(str(emTxt), str(result), processFtnotes)
except ParseException as ex:
errMsg = '{0}:\n{1}\n{2}^\n{3}\n'.format(emTxt.name,
ex.line,
' '*(ex.column-1),
ex)
# did this parse previously? if so mark "dead"
if baseExists:
print('[DEAD] ' + emTxt.name)
df.write('[DEAD] ' + errMsg)
# otherwise "fail" = still dead
else:
df.write('[FAIL] ' + errMsg)
makecopy(emTxt)
continue
# any extra messages from parse? if so, copy for later investigation
if len(extraMsg) > 0:
makecopy(emTxt)
makecopy(result)
# if base didn't exist then it's become "live"
if not baseExists:
print('[LIVE] {0} {1}'.format(emJson, extraMsg))
df.write('[LIVE] {0} {1}\n'.format(emJson, extraMsg))
# otherwise check whether we have a "diff"
else:
with open(str(base)) as bf, \
open(str(result)) as rf:
bdict = json.load(bf)
rdict = json.load(rf)
if rdict != bdict:
# print('[DIFF] {0} {1}'.format(emJson, extraMsg))
df.write('[DIFF] {0} {1}\n'.format(emJson, extraMsg))
else:
df.write('[PASS] {0} {1}\n'.format(emJson, extraMsg))