/
apertium-regtest.py
executable file
·1056 lines (1011 loc) · 38.6 KB
/
apertium-regtest.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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import base64
import cmd
from collections import defaultdict
from functools import partial
import hashlib
from http import HTTPStatus
import http.server
import json
import math
import os
import re
import shlex
import socketserver
import subprocess
import sys
import threading
import time
import urllib.parse
import xml.etree.ElementTree
import zlib
def hash_line(s):
return base64.b64encode(hashlib.sha256(s.encode('utf-8')).digest(), b'-_')[:12].decode('utf-8')
class InputFileDoesNotExist(FileNotFoundError):
pass
class InputFileIsEmpty(Exception):
pass
class ErrorInPipeline(Exception):
pass
def load_input(fname):
try:
with open(fname, 'r') as fin:
lines = fin.read().splitlines()
ret = {}
for i, l_ in enumerate(lines):
ls = l_.split('#')
l = ls.pop(0)
while l.endswith('\\') and ls:
l = l[:-1] + ls.pop(0)
l = l.replace('\\n', '\n').strip()
if not l:
continue
ret[hash_line(l)] = [i, l]
if len(ret) == 0:
print('ERROR: Input file %s was empty!' % fname)
raise InputFileIsEmpty(fname)
return ret
except FileNotFoundError:
print('ERROR: Input file %s does not exist!' % fname)
raise InputFileDoesNotExist(fname)
def load_input_string(fname):
txt = ''
for hsh, (line, content) in load_input(fname).items():
txt += '[%s#%s] %s\n[/%s]\n\0' % (hsh, line, content, hsh)
return txt
# [hash(#line)?] content [/hash]
hash_format = re.compile(r'\[([A-Za-z0-9_-]+)(#\d+|)\](.*)\[/\1\]', re.DOTALL)
# the line number is completely useless, but it now appears
# in the expected files in 365 repositories, so we need to still
# parse it - 2021-07-23
def load_output(fname):
try:
with open(fname, 'r') as fin:
ret = {}
txt = fin.read().replace('\0', '')
for hsh, line, content_ in hash_format.findall(txt):
content = content_.strip()
if not content:
print('ERROR: Entry %s in %s was empty!' % (hsh, fname))
l = 0
if line:
l = int(l[1:])
ret[hsh] = [l, content]
# line numbers are nice for debugging,
# but nothing breaks if we don't have them
return ret
except FileNotFoundError:
return {}
def save_output(fname, data):
with open(fname, 'w') as fout:
for inhash in sorted(data.keys()):
fout.write('[%s#0] %s\n[/%s]\n' % (inhash, data[inhash][1], inhash))
def load_gold(fname):
try:
with open(fname, 'r') as fin:
ret = {}
for hsh, line, content in hash_format.findall(fin.read()):
opts = []
for o in content.split('[/option]'):
o2 = o.strip()
if o2:
opts.append(o2)
if not opts:
print('ERROR: Empty entry %s in %s' % (ident, fname))
continue
ret[hsh] = opts
return ret
except FileNotFoundError:
return {}
def save_gold(fname, data):
with open(fname, 'w') as fout:
for inhash in sorted(data.keys()):
fout.write('[%s]\n' % inhash)
for ln in sorted(set(data[inhash])):
fout.write('%s [/option]\n' % ln)
fout.write('[/%s]\n' % inhash)
def run_command(cmd, intxt, outfile, shell=False):
proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=shell)
stdout, stderr = proc.communicate(intxt.encode('utf-8'))
if proc.returncode != 0:
c = cmd if isinstance(cmd, str) else ' '.join(cmd)
print('Failed command: %s' % c)
print('Writing stderr to test/error.log')
with open('test/error.log', 'ab') as fout:
fout.write(('Command: %s\n' % c).encode('utf-8'))
fout.write(('Output file: %s\n' % outfile).encode('utf-8'))
fout.write(('Time: %s\n' % time.asctime()).encode('utf-8'))
fout.write(b'Stdin:\n\n')
fout.write(intxt.encode('utf-8'))
fout.write(b'Stdout:\n\n')
fout.write(stdout)
fout.write(b'Stderr:\n\n')
fout.write(stderr)
fout.write(b'\n\n')
print('Exiting')
raise ErrorInPipeline(c)
else:
with open(outfile, 'wb') as fout:
if not intxt:
h = hash_line(intxt)
stdout = ('[%s#0]\n' % h).encode('utf-8') + stdout
stdout += ('\n[/%s]\n' % h).encode('utf-8')
fout.write(stdout)
class Step:
prognames = {
'cg-proc': 'disam',
'apertium-tagger': 'tagger',
'apertium-pretransfer': 'pretransfer',
'lrx-proc': 'lex',
'apertium-transfer': 'chunker',
'apertium-interchunk': 'interchunk',
'apertium-postchunk': 'postchunk',
'lsx-proc': 'autoseq',
'rtx-proc': 'transfer',
'apertium-anaphora': 'anaph',
'cg-conv': 'convert',
'vislcg3': 'disam'
}
morphmodes = {
'-b': 'biltrans',
'-p': 'postgen',
'-g': 'generator'
}
def __init__(self, xml):
pr = shlex.split(xml.attrib['name'])
self.prog = pr[0]
self.args = pr[1:]
for ar in xml:
if ar.tag == 'arg':
self.args += shlex.split(ar.attrib['name'])
else:
self.args.append(ar.attrib['name'])
remove = []
for i in range(len(self.args)):
if self.args[i] == '$1':
self.args[i] = '-g'
elif self.args[i] == '$2':
remove.append(i)
remove.reverse()
for i in remove:
self.args.pop(i)
self.name = xml.attrib.get('debug-suff', 'unknown')
if self.name == 'unknown':
if self.prog in Step.prognames:
self.name = Step.prognames[self.prog]
elif self.prog in ['lt-proc', 'hfst-proc']:
self.name = 'morph'
for op in Step.morphmodes:
if op in self.args:
self.name = Step.morphmodes[op]
def run(self, in_name, out_name, first=False):
cmd = [self.prog] + self.args
if self.prog in Step.prognames or self.prog in ['lt-proc', 'hfst-proc']:
if self.prog not in ['cg-conv', 'vislcg3']:
cmd.append('-z')
txt = ''
if first:
txt = load_input_string(in_name)
else:
with open(in_name, 'r') as fin:
txt = fin.read()
if self.prog == 'vislcg3':
txt = txt.replace('\0', '\n<STREAMCMD:FLUSH>\n')
run_command(cmd, txt, out_name)
class Mode:
all_modes = {}
def __init__(self, xml):
self.name = xml.attrib['name']
self.steps = [Step(s) for s in xml[0]]
self.commands = {}
nm = defaultdict(lambda: 0)
for i, s in enumerate(self.steps):
nm[s.name] += 1
if nm[s.name] > 1:
s.name += str(nm[s.name])
self.commands[s.name] = i
Mode.all_modes[self.name] = self
def run(self, corpusname, filename, start=None):
fin = filename
idx = self.commands.get(start, 0)
for i, step in enumerate(self.steps[idx:]):
fout = 'test/%s-%s-output.txt' % (corpusname, step.name)
step.run(fin, fout, first=(i == 0))
fin = fout
def get_commands(self):
return [s.name for s in self.steps]
def load_modes():
try:
root = xml.etree.ElementTree.parse('modes.xml').getroot()
except FileNotFoundError:
print('modes.xml not found.')
print('Please ensure that apertium-regtest is being run in an Apertium directory.')
sys.exit(1)
except xml.etree.ElementTree.ParseError as e:
print('Unable to parse modes.xml.')
print('Parser message: %s' % e.msg)
sys.exit(1)
for m in root:
try:
Mode(m)
except:
print('Unable to parse modes.xml.')
print('Run `apertium-validate-modes` for more information.')
sys.exit(1)
def get_url(remote):
proc = subprocess.run(['git', 'remote', 'get-url', remote],
stdout=subprocess.PIPE)
if proc.returncode != 0:
return ''
return proc.stdout.decode('utf-8').strip()
def yes_no(msg):
ans = input(msg + ' (yes/no) ')
while True:
if 'yes'.startswith(ans.strip().lower()):
return True
elif 'no'.startswith(ans.strip().lower()):
return False
else:
ans = input('unable to interpret reply - please type yes or no: ')
def check_git():
# look for an external git repo
# return True if we end up cloning it
proc = subprocess.run(['git', 'remote'], stdout=subprocess.PIPE)
if proc.returncode != 0:
return False
all_remotes = proc.stdout.decode('utf-8').strip().split()
if len(all_remotes) == 0:
return False
url = ''
if 'origin' in all_remotes:
url = get_url('origin')
if not url:
for remote in all_remotes:
url = get_url('origin')
if url:
break
if not url:
return False
url = url.replace('/apertium-', '/test-')
ans = yes_no('Test corpora not found. Clone external test corpus?')
if not ans:
return False
inurl = input('remote url (default %s): ' % url).strip()
if not inurl:
inurl = url
proc = subprocess.run(['git', 'clone', inurl, 'test'])
if proc.returncode == 0:
return True
else:
print('Cloning failed. Please check the remote url and try again.')
sys.exit(1)
class Corpus:
all_corpora = {}
def __init__(self, name, blob):
self.name = name
self.mode = blob.get('mode', None)
self.shell = blob.get('command', None)
if not self.mode and not self.shell:
print('Corpus %s must specify either "mode": or "command":' % self.name)
sys.exit(1)
if self.mode and self.mode not in Mode.all_modes:
print('Unknown mode %s in corpus %s' % (self.mode, self.name))
sys.exit(1)
if 'input' not in blob:
print('Corpus %s must specify an input file' % self.name)
sys.exit(1)
self.infile = None
if blob['input'] != None:
self.infile = 'test/' + blob['input']
elif self.shell:
pass
else:
print('Corpus %s has empty input with standard mode' % self.name)
sys.exit(1)
self.start_step = blob.get('start-step', None)
self.data = {}
self.loaded = False
self.unsaved = set()
self.command_list = ['all']
if self.mode:
self.command_list = Mode.all_modes[self.mode].get_commands()
self.commands = {c:i for i, c in enumerate(self.command_list)}
self.relevant_commands = blob.get('relevant', [self.command_list[-1]])
if not isinstance(self.relevant_commands, list):
print('Corpus %s specified a non-list for "relevant"' % self.name)
sys.exit(1)
self.hashes = []
Corpus.all_corpora[name] = self
def __len__(self):
return len(self.hashes)
def run(self):
if self.mode:
Mode.all_modes[self.mode].run(self.name, self.infile,
start=self.start_step)
else:
txt = ''
if self.infile:
txt = load_input_string(self.infile)
run_command(self.shell, txt, self.out_name('all'), shell=True)
self.loaded = False
def exp_name(self, cmd):
return 'test/%s-%s-expected.txt' % (self.name, cmd)
def out_name(self, cmd):
return 'test/%s-%s-output.txt' % (self.name, cmd)
def gold_name(self, cmd):
return 'test/%s-%s-gold.txt' % (self.name, cmd)
def save(self):
for blob in self.data['cmds']:
if blob['cmd'] in self.unsaved:
save_output(self.exp_name(blob['cmd']), blob['expect'])
self.unsaved = set()
def load(self):
if self.loaded:
return
if self.infile:
ins = load_input(self.infile)
else:
ins = {hash_line(''): [0, '']}
self.hashes = list(ins.keys())
self.hashes.sort(key = lambda x: ins[x][0])
outs = []
self.data = {
'inputs': ins,
'cmds': [],
'count': len(ins)
}
for c in self.command_list:
expfile = self.exp_name(c)
outdata = load_output(self.out_name(c))
expdata = {}
if os.path.isfile(expfile):
expdata = load_output(expfile)
else:
save_output(expfile, outdata)
expdata = outdata
golddata = {}
goldfile = self.gold_name(c)
if os.path.isfile(goldfile):
golddata = load_gold(goldfile)
if not outs:
outs = expdata.keys()
self.data['cmds'].append({
'cmd': c,
'opt': c,
'relevant': (c in self.relevant_commands),
'output': outdata,
'expect': expdata,
'gold': golddata,
'trace': {} # TODO?
})
add = [k for k in ins if k not in outs]
delete = [k for k in outs if k not in ins]
add.sort(key = lambda x: ins[x][0])
delete.sort()
self.data['add'] = add
self.data['del'] = delete
def page(self, start, page_len):
hs = self.hashes[start:start+page_len]
def hf(dct):
nonlocal hs
return {k:v for k, v in dct.items() if k in hs}
return {
'inputs': hf(self.data['inputs']),
'cmds': [
{
'cmd': blob['cmd'],
'opt': blob['opt'],
'relevant': blob['relevant'],
'output': hf(blob['output']),
'expect': hf(blob['expect']),
'gold': hf(blob['gold']),
'trace': hf(blob['trace'])
}
for blob in self.data['cmds']
],
'count': page_len,
'add': self.data['add'],
'del': self.data['del']
}
def step(self, s):
return self.data['cmds'][self.commands.get(s, -1)]
def get_changed_hashes(self):
norm = set()
imp = set()
for cmd in self.relevant_commands:
blob = self.step(cmd)
for hsh in self.data['inputs']:
if hsh not in blob['expect']:
continue
if blob['output'][hsh][1] == blob['expect'][hsh][1]:
continue
if hsh in blob['gold']:
if blob['output'][hsh][1] in blob['gold'][hsh]:
continue
norm.add(hsh)
if blob['relevant']:
imp.add(hsh)
imp_ret = sorted(imp, key = lambda x: self.data['inputs'][x][0])
norm -= imp
norm_ret = sorted(norm, key = lambda x: self.data['inputs'][x][0])
return imp_ret + norm_ret
def display_line(self, hsh, step=None):
# TODO: colors, diffs
def indent(s):
print(' ' + s.replace('\n', '\n '))
blob = self.step(step)
if hsh in self.data['inputs']:
print('%s %s of %s' % (self.name, self.hashes.index(hsh)+1, len(self.hashes)))
print('INPUT:')
indent(self.data['inputs'][hsh][1])
else:
print(self.name)
print('INPUT: [sentence deleted from input corpus]')
if hsh in blob['expect']:
print('EXPECTED OUTPUT:')
indent(blob['expect'][hsh][1])
else:
print('EXPECTED OUTPUT: [sentence added since last run]')
if hsh in blob['output']:
print('ACTUAL OUTPUT:')
indent(blob['output'][hsh][1])
if hsh in blob['gold']:
print('IDEAL OUTPUTS:')
for g in blob['gold'][hsh]:
indent(g)
def accept_add_del(self, should_save=True):
if not ('add' in self.data or 'del' in self.data):
return []
changes = []
for blob in self.data['cmds']:
for a in self.data['add']:
if a not in blob['expect']:
blob['expect'][a] = [0, blob['output'][a][1]]
changes.append(a)
self.unsaved.add(blob['cmd'])
for d in self.data['del']:
if d in blob['expect']:
del blob['expect'][d]
changes.append(d)
self.unsaved.add(blob['cmd'])
if d in blob['gold']:
del blob['gold'][d]
if should_save:
self.save()
self.data['add'] = []
self.data['del'] = []
return list(set(changes))
def accept(self, hashes=None, last_step=None):
if 'cmds' not in self.data:
return []
changes = self.accept_add_del(False)
for blob in self.data['cmds']:
for h in (hashes or blob['expect'].keys()):
if h not in blob['expect']:
continue
if blob['expect'][h][1] != blob['output'][h][1]:
blob['expect'][h][1] = blob['output'][h][1]
changes.append(h)
self.unsaved.add(blob['cmd'])
if blob['cmd'] == last_step:
break
self.save()
return list(set(changes))
def set_gold(self, hsh, vals, step=None):
blob = self.step(step)
blob['gold'][hsh] = vals
save_gold(self.gold_name(blob['cmd']), blob['gold'])
def load_corpora(names, static=False):
if not os.path.isdir('test') or not os.path.isfile('test/tests.json'):
if os.path.isdir('.git'):
if not static and check_git():
load_corpora(names)
return
print('Test corpora not found. Please create test/tests.json')
print('as described at https://wiki.apertium.org/wiki/User:Popcorndude/Regression-Testing')
sys.exit(1)
with open('test/tests.json') as ts:
pats = []
if names:
for n in names:
pats.append(re.compile(n))
else:
pats.append(re.compile('.*'))
try:
blob = json.load(ts)
for k in blob:
for p in pats:
if p.search(k):
Corpus(k, blob[k])
break
except json.JSONDecodeError as e:
print('test/tests.json is not a valid JSON document. First error on line %s' % e.lineno)
sys.exit(1)
def test_run(corpora):
ls = corpora
if '*' in corpora:
ls = list(Corpus.all_corpora.keys())
for name in ls:
Corpus.all_corpora[name].run()
return True, ''
def cb_load(page, step=25):
changes = {
'changed_final': [],
'changed_any': [],
'unchanged': []
}
state = {
'_step': step,
'_ordered': [],
'_page': page
}
ct_min = page * step
ct_max = (page + 1) * step
ct = 0
for name in sorted(Corpus.all_corpora.keys()):
corpus = Corpus.all_corpora[name]
corpus.load()
ct_next = ct + len(corpus)
if ct_next < ct_min or ct >= ct_max:
state[name] = corpus.page(0, 0)
elif ct < ct_min:
start = ct_min - ct
ln = min(ct_max, ct_next) - ct_min
state[name] = corpus.page(start, ln)
else: # ct >= ct_min
ln = min(ct_max, ct_next) - ct
state[name] = corpus.page(0, ln)
ct = ct_next
state['_count'] = ct
state['_pages'] = math.ceil(ct/step)
return {'state': state}
def compress(s):
step = 2 << 17
producer = zlib.compressobj(level=9, wbits=15)
idx = 0
while idx < len(s):
yield producer.compress(s[idx:idx+step])
idx += step
yield producer.flush()
THE_CALLBACK_LOCK = threading.Lock()
class CallbackRequestHandler(http.server.SimpleHTTPRequestHandler):
protocol_version = 'HTTP/1.1'
def __init__(self, request, client_address, server, directory=None,
page_size=25):
self.page_size = page_size
super().__init__(request, client_address, server, directory=directory)
def do_GET(self):
parts = urllib.parse.urlsplit(self.path)
if parts.path.strip('/') != 'callback':
return super().do_GET()
else:
params = urllib.parse.parse_qs(parts.query)
self.do_callback(params)
def do_POST(self):
ln = int(self.headers['Content-Length'])
data = self.rfile.read(ln)
self.do_callback(urllib.parse.parse_qs(data.decode('utf-8')))
def send_json(self, status, blob):
# based on https://github.com/PierreQuentel/httpcompressionserver/blob/master/httpcompressionserver.py (BSD license)
self.send_response(status)
self.send_header('Content-type', 'application/json')
rstr = json.dumps(blob).encode('utf-8')
self.send_header('Content-Encoding', 'deflate')
if len(rstr) < (2 << 18):
# don't bother chunking shorter messages
dt = b''.join(compress(rstr))
self.send_header('Content-Length', len(dt))
self.end_headers()
self.wfile.write(dt)
else:
self.send_header('Transfer-Encoding', 'chunked')
self.end_headers()
for data in compress(rstr):
if data:
ln = hex(len(data))[2:].upper().encode('utf-8')
self.wfile.write(ln + b'\r\n' + data + b'\r\n')
self.wfile.write(b'0\r\n\r\n')
def do_callback(self, params):
if 'a' not in params:
resp = 'Parameter a must be passed!'
self.send_response(HTTPStatus.OK)
self.send_header("Content-type", 'text/plain')
self.send_header("Content-Length", len(resp))
self.end_headers()
self.wfile.write(resp.encode('utf-8'))
return
status = HTTPStatus.OK
resp = {}
shutdown = False
THE_CALLBACK_LOCK.acquire()
# TODO: error checking
if params['a'][0] == 'init':
resp['folder'] = os.path.basename(os.getcwd())
resp['corpora'] = list(sorted(Corpus.all_corpora.keys()))
elif params['a'][0] == 'load':
try:
resp = cb_load(int(params['p'][0]), self.page_size)
except InputFileDoesNotExist as e:
resp = {'error': 'Input file %s expected but not found! Server exiting.' % e.args[0]}
shutdown = True
except InputFileIsEmpty as e:
resp = {'error': 'Input file %s contained no data! Server exiting.' % e.args[0]}
shutdown = True
elif params['a'][0] == 'run':
try:
good, output = test_run(params.get('c', ['*']))
resp['good'] = good
resp['output'] = output
except ErrorInPipeline as e:
resp = {'error': 'Command `%s` crashed. Server exiting.' % e.args[0]}
shutdown = True
elif params['a'][0] == 'accept-nd':
resp['c'] = params['c'][0]
resp['hs'] = Corpus.all_corpora[resp['c']].accept_add_del()
elif params['a'][0] == 'accept':
resp['c'] = params['c'][0]
s = params.get('s', [None])[0]
hs = []
if 'hs' in params:
hs = params['hs'][0].split(';')
resp['hs'] = Corpus.all_corpora[resp['c']].accept(hs, s)
elif params['a'][0] == 'gold':
corp = params['c'][0]
hsh = params['h'][0]
golds = json.loads(params['gs'][0])
stp = None
if 's' in params:
stp = params['s'][0]
Corpus.all_corpora[corp].set_gold(hsh, golds, stp)
resp = {'c': corp, 'hs': [hsh]}
else:
resp['error'] = 'unknown value for parameter a'
self.send_json(status, resp)
THE_CALLBACK_LOCK.release()
if shutdown:
if 'error' in resp:
sys.exit(1)
else:
sys.exit(0)
class BigQueueServer(socketserver.ThreadingTCPServer):
request_queue_size = 100
def start_server(port, page_size=25):
d = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'static/')
handle = partial(CallbackRequestHandler, directory=d, page_size=page_size)
print('Starting server')
print('Open http://localhost:%d in your browser' % port)
with BigQueueServer(('', port), handle) as httpd:
try:
httpd.serve_forever()
except KeyboardInterrupt:
print('')
# the exception raised by sys.exit() gets caught by the
# server, so we need to be a bit more drastic
os._exit(0)
class RegtestShell(cmd.Cmd):
prompt = '> '
lines_todo = defaultdict(list) # { corpus_name : [ hash, hash, ... ] }
lines_accepted = defaultdict(list)
corpus_filter = None
current_corpus = None
current_hash = None
end_step = None
show_step = None
def __init__(self, autosave=True):
print('\nRunning regression tests for %s' % os.path.basename(os.getcwd()))
print('Type `help` for a list of available commands.\n')
print('Loading corpora...')
for k in sorted(Corpus.all_corpora.keys()):
self.load_corpus(k)
self.autosave = autosave
self.next_hash()
super().__init__()
def load_corpus(self, name):
corp = Corpus.all_corpora[name]
corp.load()
self.lines_todo[name] = []
if corp.data['add'] or corp.data['del']:
print('Corpus %s has %s added lines and %s deleted lines since last run' % (name, len(corp.data['add']), len(corp.data['del'])))
if yes_no('Run test and save changes?'):
corp.run()
corp.load()
corp.accept_add_del()
else:
self.lines_todo[name] = corp.data['add'] + corp.data['del']
self.lines_todo[name] += corp.get_changed_hashes()
# TODO: important hashes in all corpora before unimporant ones
print('Corpus %s has %s lines to be examined.' % (name, len(self.lines_todo[name])))
def next_hash(self, drop_prev=False):
if drop_prev and self.current_corpus in self.lines_todo:
if self.current_hash in self.lines_todo[self.current_corpus]:
self.lines_todo[self.current_corpus].remove(self.current_hash)
if not self.lines_todo[self.current_corpus]:
del self.lines_todo[self.current_corpus]
drop = []
for k, l in self.lines_todo.items():
if len(l) == 0:
drop.append(k)
for k in drop:
del self.lines_todo[k]
if self.corpus_filter:
self.current_corpus = self.corpus_filter
elif not self.current_corpus and len(self.lines_todo) > 0:
self.current_corpus = list(sorted(self.lines_todo.keys()))[0]
if self.current_corpus not in self.lines_todo:
self.current_hash = None
else:
self.current_hash = self.lines_todo[self.current_corpus][0]
self.do_show('')
def do_s(self, arg):
'Synonym for `show`'
self.do_show(arg)
def do_show(self, arg):
'''Display the selected step of the current line.
`show [step]` - Display step `step` of the current line.
`show` - Display the default step of the current line.
If the default display step has not been set, the final step
will be used. Invoking this command with an argument also sets
the default display step.'''
if self.current_corpus and self.current_hash:
corp = Corpus.all_corpora[self.current_corpus]
if arg:
self.show_step = arg
corp.display_line(self.current_hash, self.show_step)
elif self.corpus_filter and len(self.lines_todo) > 0:
print('No changed lines match current filter')
print("Use 'filter' to change filter or 'quit' to exit")
else:
print('No more changed lines')
print("Use 'run' to update tests or 'quit' to exit")
def complete_show(self, text, line, begidx, endidx):
ret = []
if self.current_corpus in Corpus.all_corpora:
corp = Corpus.all_corpora[self.current_corpus]
for cmd in corp.commands:
if cmd.startswith(text):
ret.append(cmd)
return ret
def do_a(self, arg):
'Synonym for `accept`'
self.do_accept('')
def do_accept(self, arg):
'''Accept the outputs of the current line and replace the expected outputs.
If `upto` has been called, this will not affect steps after the limit.
Abbreviated form: `a`'''
if self.current_corpus and self.current_hash:
self.lines_accepted[self.current_corpus].append(self.current_hash)
self.next_hash(True)
def do_ag(self, arg):
'Synonym for `addgold`'
self.do_addgold('')
def do_addgold(self, arg):
'''Add the output for the currently displayed step of the current line
to the list of ideal outputs for that line.
This command also runs `accept`.
Abbreviated form: `ag`'''
if self.current_corpus and self.current_hash:
corp = Corpus.all_corpora[self.current_corpus]
blob = corp.step(self.show_step)
out = blob['output'][self.current_hash][1]
gold = blob['gold'].get(self.current_hash, [])
corp.set_gold(self.current_hash, gold + [out], self.show_step)
self.do_accept('')
else:
print('No input selected')
def do_rg(self, arg):
'Synonym for `replacegold`'
self.do_replacegold('')
def do_replacegold(self, arg):
'''Remove the list of ideal outputs for the current step of the current
line and replace them with the current output.
This command also runs `accept`.
Abbreviated form: `rg`'''
if self.current_corpus and self.current_hash:
corp = Corpus.all_corpora[self.current_corpus]
out = corp.step(self.show_step)['output'][self.current_hash][1]
corp.set_gold(self.current_hash, [out], self.show_step)
self.do_accept('')
else:
print('No input selected')
def do_r(self, arg):
'Synonym for `run`'
self.do_run(arg)
def do_run(self, corpus):
'''Run tests and display results
`run` - Run tests for all corpora.
`run [name]` - Run tests only for corpus `name`.
Abbreviated form: `r`'''
if corpus == '*' or corpus == '':
for name, corp in Corpus.all_corpora.items():
print('Running %s' % name)
corp.run()
self.load_corpus(name)
else:
for name in corpus.split():
if name in Corpus.all_corpora:
print('Running %s' % name)
Corpus.all_corpora[name].run()
self.load_corpus(name)
else:
print("Corpus '%s' does not exist" % name)
self.next_hash()
def complete_run(self, text, line, begidx, endidx):
ls = []
for c in Corpus.all_corpora:
if c.startswith(text):
ls.append(c)
return ls
def do_v(self, arg):
'Synonym for `save`'
self.do_save('')
def do_save(self, arg):
'''Save all pending changes to expected output.
This is automatically run by `quit` unless apertium-regtest was
invoked with `--no-autosave`.
Abbreviated form: `v`'''
for name in sorted(Corpus.all_corpora.keys()):
corp = Corpus.all_corpora[name]
if name in self.lines_accepted:
s = ''
if self.end_step:
s = ' through step %s' % self.end_step
print('Saving changes to %s%s' % (name, s))
corp.accept(self.lines_accepted[name], self.end_step)
del self.lines_accepted[name]
elif len(corp.unsaved) > 0:
print('Saving changes to %s' % name)
corp.save()
def do_upto(self, arg):
'''Disregard changes after a particular step.
When `save` is run, no steps after the last value passed to `upto`
will be updated. Calling without arguments will select the final step.
If the default display step is after the new limit step, the default
display step will be set to the new limit step.'''
if not arg:
self.end_step = None
else:
self.end_step = arg
if self.current_corpus:
cmd = Corpus.all_corpora[self.current_corpus].commands
if cmd.get(self.end_step, 0) > cmd.get(self.show_step, len(cmd)):
self.show_step = self.end_step
def complete_upto(self, *args):
return self.complete_show(*args)
def do_q(self, arg):
'Synonym for `quit`'
return self.do_quit('')
def do_quit(self, arg):
'''Exit the program.
This will run `save` unless apertium-regtest was invoked with `--no-autosave`.
Abbreviated form: `q`'''
if self.autosave:
self.do_save('')
return True
def do_EOF(self, arg):
'Synonym for `quit` provided so that CTRL-D will work as expected.'
print('')
return self.do_quit('')
def check_hash(corpus, hsh):
expect = True # matches expectation or gold in all cases
gold = True # matches gold in all cases
# note: if gold not present, returns False
for c in corpus.relevant_commands:
data = corpus.step(c)
out = data['output'].get(hsh, [0, ''])[1]
exp = data['expect'].get(hsh, [0, ''])[1]
gld = data['gold'].get(hsh, [])
if out in gld:
continue
gold = False
if out != exp:
expect = False
return expect, gold
def static_test(ignore_add=False):
n = len(Corpus.all_corpora.items())
changed = set()
for i, (name, corp) in enumerate(Corpus.all_corpora.items(), 1):
print('Corpus %s of %s: %s' % (i, n, name))
if not corp.loaded:
corp.run()
corp.load()
if corp.data['add']:
print(' %s tests added since last run' % len(corp.data['add']))
if not ignore_add:
changed.add(name)
if corp.data['del']:
print(' %s tests removed since last run' % len(corp.data['del']))
if not ignore_add:
changed.add(name)
total = 0
same = 0
gold = 0
for hsh in corp.data['inputs']:
if hsh in corp.data['add']:
continue
e, g = check_hash(corp, hsh)
total += 1
if e:
same += 1
if g:
gold += 1
if total > 0:
print(' %s/%s (%s%%) tests pass' % (same, total, round(100.0*same/total, 2)), end='')
if same != total:
changed.add(name)
if same > 0:
print(' (%s/%s (%s%%) match gold)' % (gold, same, round(100.0*gold/same, 2)))
else:
print('')
print('')
if changed:
print('There were changes! Rerun in interactive mode to update tests.')
print('Changed corpora: ' + ', '.join(sorted(changed)))
return False
else:
print('All tests pass.')
return True
if __name__ == '__main__':
load_modes()
import argparse
parser = argparse.ArgumentParser(
prog='apertium-regtest',
description='Run and update regression tests for Apertium data repositories',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog='''
apertium-regtest has 3 modes available:
- 'test' runs all tests, printing a report and setting the exit code
if any failed. This is primarily intended for 'make test' recipes.
- 'web' starts a local webserver so that tests can be interactively
updated from the browser.