-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_test_case
executable file
·91 lines (71 loc) · 2.95 KB
/
add_test_case
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
#!/usr/bin/python -B
# -*- coding: utf-8 -*-
##############################################################################
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
##############################################################################
# This script takes a file on the command line, runs relic on it to get the
# "answers" (which are presumably correct when you add it to the suite!) and
# then adds the file to the test suite, along with the determined answers.
#
# So when you update the code to cope with a particular file, call this
# script on it and hopefully it'll never break again without you noticing.
import os
import sys
import argparse
import subprocess
import json
import shutil
def get_script_path():
return os.path.dirname(os.path.realpath(sys.argv[0]))
# Find absolute path to passed-in file
parser = argparse.ArgumentParser(description='Add one or more files to the relic testcase set.')
parser.add_argument('files', metavar='<file>', nargs='+',
help='a file for the testcase set')
args = parser.parse_args()
id_dir = os.path.join(get_script_path(), "test", "data", "identification")
for filename in args.files:
sourcename = os.path.realpath(filename)
basename = os.path.basename(sourcename)
destname = os.path.join(id_dir, basename)
print "Adding file %s..." % sourcename
# Deal with possibly clashing names using subdirectories
i = 1
while os.path.isfile(destname):
i = i + 1
destname = os.path.join(id_dir, str(i), basename)
if i > 1:
print " (As %s)" % destname
basename = os.path.join(str(i), basename)
if not os.path.isdir(os.path.dirname(destname)):
os.mkdir(os.path.dirname(destname))
# Run slic on it and capture result
slic = os.path.join(get_script_path(), "slic")
output = subprocess.check_output([slic, "-j", "-D", sourcename])
result = json.loads(output)
if not result:
print "No data from relic"
continue
resultcount = len(result)
# The rest of the fields pertain to the first result
result = result[0]
tag = result['tag']
textlength = "0"
if 'text' in result:
textlength = str(len(result['text']))
copyrightlength = "0"
if 'copyrights' in result:
copyrightlength = str(len(result['copyrights']))
# Copy file to test directory
shutil.copy(sourcename, destname)
# Add entry to index.csv
indexname = os.path.join(id_dir, "index.csv")
with open(indexname, "a") as indexfh:
indexfh.write(",".join([basename,
str(resultcount),
tag,
textlength,
copyrightlength,
""]))
indexfh.write("\n")