-
Notifications
You must be signed in to change notification settings - Fork 0
/
cats_tools.py
379 lines (303 loc) · 12 KB
/
cats_tools.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
import os
import sys
from option_lib import getOption
from enum import Enum
GENERIC_TEST_FOLDER_NAMES = ['tests']
class COLORS:
VIOLET = '\033[95m'
BLUE = '\033[94m'
GREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
DEF = ''
SUPPORTED_SOLUTION_EXT = ['.cpp', '.cc']
def cprint(text, color, end='\n', bold=False):
if bold:
color += COLORS.BOLD
print(color + text + COLORS.ENDC, end=end)
def tabulate(text, tabs=1):
if type(text) is str:
text = text.splitlines()
for i in range(len(text)):
text[i] = "\t" * tabs + " " + text[i]
return os.linesep.join(text)
def isPath(path):
return "\\" in path or "/" in path
def isRealPath(path):
return os.path.exists(path)
def buildFile(filePath, filePathCpp):
print("Building " + os.path.basename(filePathCpp) + "...")
exitCode = os.system("g++ -o " + filePath + ".exe" + " " + filePathCpp)
if exitCode != 0:
cprint("Build failed.", COLORS.FAIL)
return exitCode
print("Done.")
return exitCode
def createTests(testFolderPath, numTests):
maxNumberedTest = 0
if os.path.exists(testFolderPath):
testFiles = os.listdir(testFolderPath)
completeTests = []
maxNumberedTest = 0
for file in testFiles:
fileNoExtension = os.path.splitext(os.path.basename(file))[0]
if fileNoExtension.isnumeric():
if int(fileNoExtension) > maxNumberedTest:
maxNumberedTest = int(file.split(".")[0])
else:
os.mkdir(testFolderPath)
for i in range(maxNumberedTest + 1, maxNumberedTest + numTests + 1):
print("Please enter the input for test " + str(i) + ":")
contents = []
while True:
try:
line = input()
except EOFError:
break
contents.append(line)
open(os.path.join(testFolderPath, str(i) + ".in"), "w").write("\n".join(contents))
print("Please enter the output for test " + str(i) + ":")
contents = []
while True:
try:
line = input()
except EOFError:
break
contents.append(line)
open(os.path.join(testFolderPath, str(i)) + ".out", "w").write("\n".join(contents))
"""def printTestResults(testResult: TestResult):
tr = testResult
if testResult.result is None:
cprint(tr.emoji + " Test " + str(tr.testName) + " finished. Runtime: " + str(round(tr.runTime, 5)) + " seconds."
, COLORS.BLUE, bold=True)
cprint("\tInput:", COLORS.DEF, bold=True)
print(tabulate(tr.input))
cprint("\tOutput:", COLORS.DEF, bold=True)
print(tabulate(tr.actual))
elif testResult.result:
cprint(tr.emoji + " Test " + str(tr.testName) + " passed. Runtime: " + str(round(tr.runTime, 5)) + " seconds.",
COLORS.GREEN, bold=True)
elif not testResult.result:
cprint(tr.emoji + " Test " + str(tr.testName) + " failed. Runtime: " + str(round(tr.runTime, 5)) + " seconds.",
COLORS.FAIL, bold=True)
cprint("\tInput:", COLORS.DEF, bold=True)
print(tabulate(tr.input))
cprint("\tExpected:", COLORS.DEF, bold=True)
print(tabulate(tr.expected))
cprint("\tActual:", COLORS.DEF, bold=True)
print(tabulate(tr.actual))
"""
def runTests(filePath, fileName, fileNameNoExtension):
if not os.path.exists(fileName):
cprint("Can't test - file does not exist.", COLORS.WARNING)
return
t = TestSet(filePath, fileName, fileNameNoExtension)
if not t.setTests():
cprint("Can't test - no valid tests found.", COLORS.WARNING)
return
allPassed = True
for test in t.tests:
tr = t.tests[test].run()
printTestResults(tr)
if not tr.result:
allPassed = False
return allPassed
def runSpecificTest(filePath, fileName, fileNameNoExtension, testCode):
if not os.path.exists(fileName):
cprint("Can't run test - file does not exist.", COLORS.WARNING)
return
t = TestSet(filePath, fileName, fileNameNoExtension)
if not t.setTests():
cprint("Can't run test - no valid tests found.", COLORS.WARNING)
return
if testCode not in t.tests:
cprint("Can't run test - test not found.", COLORS.WARNING)
return
tr = t.tests[testCode].run()
printTestResults(tr)
def runTestsWithoutResults(filePathExe):
fileNameExe = os.path.basename(filePathExe)
fileNameNoExtension = os.path.splitext(fileNameExe)[0]
if not os.path.exists(fileNameExe):
cprint("Can't run with input - file does not exist.", COLORS.WARNING)
return
t = TestSet(filePathExe, fileNameExe, fileNameNoExtension)
try:
t.setTests()
except Exception as e:
print(e)
return
for test in t.tests:
tr = t.tests[test].runWithoutResult()
printTestResults(tr)
def isArg(arg):
return arg.startswith("-")
def isValidOption(option):
return option is not None
def getOptionsAndFileName(args, options):
validOptions = []
nonOptionArguments = []
for arg in args:
if isArg(arg):
option = getOption(arg, options) # get the option that corresponds to the tag
if not isValidOption(option):
cprint("Invalid option: " + arg, COLORS.FAIL)
else:
optionObject = options[option]
if optionObject.getType() is not bool:
if len(arg.split("=")) > 1:
optionObject.setValue(arg.split("=")[1])
else:
validOptions.append(option)
else:
validOptions.append(option)
else:
nonOptionArguments.append(arg)
file = None if len(nonOptionArguments) == 0 else nonOptionArguments[0]
return validOptions, file
def setOptionsToDefault(options, settings):
for option in options:
optionName = options[option].getName() + "DefaultValue"
options[option].setValue(settings[optionName])
def setOptions(optionArguments, options, settings):
if len(optionArguments) == 0:
setOptionsToDefault(options, settings)
return
for option in options:
if options[option].getType() is bool:
options[option].setValue(False)
for optionArgument in optionArguments:
options[optionArgument].setValue(True)
""" def runTestsWithoutResults(filePath, fileName, fileNameNoExtension):
if not os.path.exists(fileName):
cprint("Can't run with input - file does not exist.", COLORS.WARNING)
return
t = TestSet(filePath, fileName, fileNameNoExtension)
try:
t.setTests()
except Exception as e:
print(e)
return
for test in t.tests:
tr = t.tests[test].runWithoutResult()
printTestResults(tr)"""
def runExecutable(executablePath):
fileName = os.path.basename(executablePath)
if not os.path.exists(executablePath):
cprint("Can't run executable - file does not exist.", COLORS.WARNING)
return
cprint("Running " + fileName + "... ", COLORS.VIOLET, bold=True)
os.system(executablePath)
print("\nDone.")
def print_error(error):
cprint(error, COLORS.FAIL, bold=True)
def is_valid_solution_file(file_path, accepted_extensions=SUPPORTED_SOLUTION_EXT):
extension = os.path.splitext(file_path)[1]
return extension in accepted_extensions
def name_from_path(path):
return os.path.splitext(os.path.basename(path))[0]
def eval_file_candidate(candidate_path: str, goal_name: str):
candidate = None
if '.' in goal_name:
candidate = os.path.basename(candidate_path)
else:
candidate = name_from_path(candidate_path)
return candidate == goal_name
class FILE_ARG_TYPES(Enum):
NAME = 1
FULL_PATH = 2
LOCAL_PATH = 3
def get_file_arg_type(given_input):
if isPath(given_input):
if os.path.exists(given_input):
return FILE_ARG_TYPES.FULL_PATH
else:
return FILE_ARG_TYPES.LOCAL_PATH
return FILE_ARG_TYPES.NAME
class SolutionFile:
def __init__(self, given_path, MAX_SEARCH_DEPTH=4, allowed_extensions=SUPPORTED_SOLUTION_EXT):
self.allowed_extensions = allowed_extensions
self.MAX_SEARCH_DEPTH = MAX_SEARCH_DEPTH
self.name = None
self.path = None
given_path_type = get_file_arg_type(given_path)
self.given_path_type = given_path_type
if given_path_type is FILE_ARG_TYPES.FULL_PATH:
self.path = given_path
self.set_name()
elif given_path_type is FILE_ARG_TYPES.LOCAL_PATH:
self.path = os.path.abspath(given_path)
self.set_name()
elif given_path_type is FILE_ARG_TYPES.NAME:
self.path = self.search_for_file(given_path, os.getcwd())
if self.path is None:
print_error("Couldn't find provided file name in current working directory tree")
sys.exit(1)
self.set_name()
self.validate_path()
def search_for_file(self, searched_name, searched_dir, depth=0):
if depth > self.MAX_SEARCH_DEPTH:
return None
for file in os.listdir(searched_dir):
path = os.path.join(searched_dir, file)
if is_valid_solution_file(path, self.allowed_extensions) and eval_file_candidate(path, searched_name):
return os.path.join(searched_dir, file)
elif os.path.isdir(file):
subtree = self.search_for_file(searched_name, os.path.join(searched_dir, file), depth + 1)
if subtree is not None:
return subtree
def set_name(self):
self.name = os.path.basename(self.path)
self.name = os.path.splitext(self.name)[0]
def validate_path(self):
if not os.path.exists(self.path):
print_error("Provided path doesn't exist")
sys.exit(1)
elif not os.path.isfile(self.path):
print_error("Provided path is not a file")
def get_path_without_ext(self):
return os.path.splitext(self.path)[0]
# Searching for tests
def is_generic_tests_folder(folder_name):
return folder_name in GENERIC_TEST_FOLDER_NAMES
def is_test_folder(tested_file_name, folder_name):
if tested_file_name in folder_name and 'test' in folder_name:
return True
return False
def search_generic_tests_folder(tested_file_name, directory, level=0, MAX_SEARCH_DEPTH=4):
test_folders = []
if level > MAX_SEARCH_DEPTH:
return test_folders
for file in os.listdir(directory):
file_path = os.path.join(directory, file)
if os.path.isdir(file_path):
if file == tested_file_name:
test_folders.append(file_path)
else:
test_folders += (search_generic_tests_folder(tested_file_name, file_path, level + 1, MAX_SEARCH_DEPTH))
return test_folders
def find_test_folders(tested_file_name, directory, level=0, MAX_SEARCH_DEPTH=4):
"""
Recursively searches for all test folders named correctly that could potentially contain tests.
Returns list of folder paths.
:rtype: list
:arg directory: directory to search in
:arg level: current search depth
:return: list of test folders
"""
test_folders = []
if level > MAX_SEARCH_DEPTH:
return test_folders
for file in os.listdir(directory):
file_path = os.path.join(directory, file)
if os.path.isdir(file_path):
if is_test_folder(tested_file_name, file):
test_folders.append(file_path)
elif is_generic_tests_folder(file):
test_folders += search_generic_tests_folder(tested_file_name, file_path, level + 1, MAX_SEARCH_DEPTH)
else:
test_folders += find_test_folders(tested_file_name, file_path, level + 1)
return test_folders