forked from shhossain/computer_science
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_code.py
More file actions
501 lines (416 loc) · 16 KB
/
test_code.py
File metadata and controls
501 lines (416 loc) · 16 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
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
# test code in readme files
import subprocess
import termcolor
import uuid
import os
import sys
import threading
import shutil
import re
import hashlib
# this file is in .github\test_code.py
# chdir to root of repo
os.chdir(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
SUPPORTED_LANGUAGE = {
"python": {
"extension": ".py",
"alias": ["py", "python"],
"win_cmd": "python {file_name}",
"linux_cmd": "python3 {file_name}",
},
"c++": {
"extension": ".cpp",
"alias": ["cpp", "cxx", "cc", "c++"],
"win_cmd": "g++ {file_name} -o {file_name_without_extension} -std=c++17 && {file_name_without_extension}",
"linux_cmd": "g++ {file_name} -o {file_name_without_extension} -std=c++17 && ./{file_name_without_extension}",
},
"c": {
"extension": ".c",
"alias": ["c"],
"win_cmd": "gcc {file_name} -o {file_name_without_extension} && {file_name_without_extension}",
"linux_cmd": "gcc {file_name} -o {file_name_without_extension} && ./{file_name_without_extension}",
},
"java": {
"extension": ".java",
"alias": ["java"],
"win_cmd": "javac {file_name} && java {file_name_without_extension}",
"linux_cmd": "javac {file_name} && java {file_name_without_extension}",
},
"javascript": {
"extension": ".js",
"alias": ["js", "javascript"],
"win_cmd": "node {file_name}",
"linux_cmd": "node {file_name}",
},
"typescript": {
"extension": ".ts",
"alias": ["ts", "typescript"],
"win_cmd": "tsc {file_name} && node {file_name_without_extension}",
"linux_cmd": "tsc {file_name} && node {file_name_without_extension}",
},
"go": {
"extension": ".go",
"alias": ["go"],
"win_cmd": "go run {file_name}",
"linux_cmd": "go run {file_name}",
},
"php": {
"extension": ".php",
"alias": ["php"],
"win_cmd": "php {file_name}",
"linux_cmd": "php {file_name}",
},
"ruby": {
"extension": ".rb",
"alias": ["rb", "ruby"],
"win_cmd": "ruby {file_name}",
"linux_cmd": "ruby {file_name}",
},
"bash": {
"extension": ".sh",
"alias": ["sh", "bash"],
"win_cmd": "echo 'bash not supported on windows' {file_name}",
"linux_cmd": "echo {file_name}",
}
}
def get_random_file_name(extension):
rs = str(uuid.uuid4()).split("-")[0]
return rs + extension
# class TugOfWar
# {
def java_file_name(code):
return re.search(r'class\s+(\w+)', code).group(1) + ".java"
errors = []
error_level = [0]
class Log:
def __init__(self, color):
self.color = color
def __call__(self, msg):
print(termcolor.colored(msg, self.color))
@staticmethod
def info(*msg, **kwargs):
s = " ".join([str(x) for x in msg])
print(termcolor.colored(s, 'blue'), **kwargs)
@staticmethod
def debug(*msg, **kwargs):
s = " ".join([str(x) for x in msg])
print(termcolor.colored(s, 'green'), **kwargs)
@staticmethod
def error(*msg, **kwargs):
s = " ".join([str(x) for x in msg])
if not "threaded" in kwargs:
level = kwargs["level"] if "level" in kwargs else 0
errors.append((level, s))
error_level[0] = max(error_level[0], level)
else:
kwargs.pop("threaded", None)
print(termcolor.colored(s, 'red'), **kwargs)
@staticmethod
def warn(*msg, **kwargs):
s = " ".join([str(x) for x in msg])
print(termcolor.colored(s, 'yellow'), **kwargs)
@staticmethod
def error_occured():
return error_level[0] > 0
class LANGUAGE_NOT_SUPPORTED(Exception):
error_level: int = 1
class CODE_EXECUTION_ERROR(Exception):
error_level: int = 5
class Code:
def __init__(self, code, language, file_path=None) -> None:
self.code = code
Log.debug("Language: " + language)
self.file_path = file_path
self.language = self.get_language(language)
self.extension = SUPPORTED_LANGUAGE[self.language]['extension']
self.analyze_code()
self.command = self.get_command()
def analyze_code(self) -> None:
if self.language == "javascript":
ch_table = {"<script>": "", "</script>": "",
"document.write": "console.log", "document.writeln": "console.log"}
for k, v in ch_table.items():
self.code = self.code.replace(k, v)
elif self.language == "c++":
# change all header to <bits/stdc++.h>
self.code = re.sub(r'#include\s+<\w+>',
'#include <bits/stdc++.h>', self.code)
# change all header to <bits/stdc++.h>
def get_command(self):
if sys.platform == 'win32':
return SUPPORTED_LANGUAGE[self.language]['win_cmd']
else:
return SUPPORTED_LANGUAGE[self.language]['linux_cmd']
def get_language(self, language):
language = language.lower().strip()
if language in SUPPORTED_LANGUAGE:
return language
for lang in SUPPORTED_LANGUAGE:
if language in SUPPORTED_LANGUAGE[lang]['alias']:
return lang
raise LANGUAGE_NOT_SUPPORTED(
f"{self.file_path} | Language {language} is not supported")
def run(self):
if self.language == "java":
file_name = java_file_name(self.code)
else:
file_name = get_random_file_name(self.extension)
if self.language == "c#":
return self.run_dotnet(file_name)
with open(file_name, 'w') as f:
f.write(self.code)
Log.info("Running code")
Log.debug(f"File name: {file_name}")
cmd = self.command.format(
file_name=file_name, file_name_without_extension=file_name.split('.')[0])
Log.debug(f"Command: {cmd}")
process = subprocess.Popen(
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
self.clean_up(file_name)
if stderr:
raise CODE_EXECUTION_ERROR(stderr.decode())
return stdout.decode()
def run_dotnet(self, file_name):
Log.info("Running code")
Log.debug(f"File name: {file_name}")
# create dotnet project
cmd = "dotnet new console -o {file_name_without_extension}".format(
file_name_without_extension=file_name.split('.')[0])
Log.debug(f"Command: {cmd}")
process = subprocess.Popen(
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
if stderr:
raise CODE_EXECUTION_ERROR(stderr.decode())
project_path = os.path.join(
os.getcwd(), file_name.split('.')[0], file_name)
Log.debug(f"Project path: {project_path}")
with open(project_path, 'w') as f:
f.write(self.code)
# run dotnet project
cmd = "dotnet run --project {file_name_without_extension}".format(
file_name_without_extension=file_name.split('.')[0])
Log.debug(f"Command: {cmd}")
process = subprocess.Popen(
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
self.clean_up(file_name.split('.')[0])
if stderr:
raise CODE_EXECUTION_ERROR(stderr.decode())
return stdout.decode()
def clean_up(self, file_name):
# remove file that starts with the same name
for file in os.listdir():
if file.startswith(file_name.split('.')[0]):
shutil.rmtree(file) if os.path.isdir(file) else os.remove(file)
def fix_path(path):
# if linux and starts with / remove it
if sys.platform != 'win32' and path.startswith('/'):
path = path[1:]
return path
class Test:
def __init__(self, readme_path) -> None:
self.readme_path = fix_path(readme_path)
self.codes = self.get_codes()
Log.debug("Testing code in ", readme_path)
def get_codes(self):
codes = {} # key: language, value: [(code,line_number)]
with open(self.readme_path) as f:
for n, line in enumerate(f):
if line.startswith('```'):
language = line.split('```')[1].strip()
if not language:
continue
code = ''
for line in f:
if line.startswith('```'):
break
code += line
if language not in codes:
codes[language] = []
codes[language].append((code, n+1))
Log.debug(f"{self.readme_path} | Codes: {len(codes)}")
return codes
# def test(self):
# for language in self.codes:
# for code, line_number in self.codes[language]:
# Log.info(f"Testing code in line {line_number}")
# try:
# Code(code, language).run()
# except LANGUAGE_NOT_SUPPORTED as e:
# raise e
# except CODE_EXECUTION_ERROR as e:
# Log.error("Code execution error in file " +
# self.readme_path + " line " + str(line_number))
# raise e
# Log.info("Code executed successfully")
def threaded_test(self):
threads = []
for language in self.codes:
for code, line_number in self.codes[language]:
Log.info(
f"{self.readme_path} | Testing {language} code in line {line_number}")
t = threading.Thread(target=self.test_code, args=(
code, language, line_number))
threads.append(t)
t.start()
for t in threads:
t.join()
def normal_test(self):
for language in self.codes:
for code, line_number in self.codes[language]:
Log.info(
f"{self.readme_path} | Testing {language} code in line {line_number}")
self.test_code(code, language, line_number)
def test_code(self, code, language, line_number):
error = False
try:
output = Code(code, language).run()
Log.debug(
f"{self.readme_path} => \n==<START>==OUTPUT==={language}=\n{output}\n={line_number}===OUTPUT==<END>==")
except LANGUAGE_NOT_SUPPORTED as e:
Log.error(e,level=LANGUAGE_NOT_SUPPORTED.error_level)
error = True
except CODE_EXECUTION_ERROR as e:
Log.error(
f"{self.readme_path} | {language} code execution error in ", line_number, level=CODE_EXECUTION_ERROR.error_level)
Log.error(e,level=CODE_EXECUTION_ERROR.error_level)
error = True
if not error:
Log.info(
f"{self.readme_path} | {language} code in line {line_number} executed successfully")
def test(self):
self.threaded_test()
# self.normal_test()
# if __name__ == "__main__":
# if len(sys.argv) < 2:
# Log.error("No file path given")
# sys.exit(1)
# readme_path = sys.argv[1]
# if not os.path.exists(readme_path):
# Log.error("File path does not exist")
# sys.exit(1)
# try:
# Test(readme_path).test()
# except LANGUAGE_NOT_SUPPORTED as e:
# Log.error(e)
# sys.exit(1)
def test_all(files: list):
# files = []
# for root, dirs, file in os.walk('.'):
# for f in file:
# if f.endswith('.md'):
# files.append(os.path.join(root, f))
Log.info(f"Testing {len(files)} files")
threads = []
for file in files:
t = threading.Thread(target=Test(file).test)
threads.append(t)
t.start()
for t in threads:
t.join()
max_error_level = error_level[0]
if max_error_level >= 5:
Log.error("There are errors in the code",threaded=False)
for error in errors:
Log.error(error,threaded=False)
sys.exit(1)
elif max_error_level > 0:
Log.error("There are warnings in the code", threaded=False)
for error in errors:
Log.warn(error)
else:
Log.info("No errors found")
# compare files with hash
class CompareGitRepo:
def __init__(self, local_repo_path, remote_repo_url) -> None:
self.local_repo_path = local_repo_path
self.remote_repo_url = remote_repo_url
self.repo_name = remote_repo_url.split('/')[-1]
self.local_files_hash = {}
self.remote_files_hash = {}
self.ignore_files = [".gitignore", ".git", ".github", "temp"]
self.clone_repo()
def clone_repo(self):
if not os.path.exists("temp"):
Log.info("Cloning remote repo")
os.mkdir("temp")
os.chdir("temp")
os.system(f"git clone {self.remote_repo_url}")
os.chdir("..")
def get_local_files(self) -> list:
files = []
# all files in local repo except .git, .github, .gitignore, temp
for root, dirs, file in os.walk(self.local_repo_path):
dirs[:] = [d for d in dirs if d not in self.ignore_files]
for f in file:
if not f.endswith('.md'):
continue
files.append(os.path.join(root, f))
return files
def get_remote_files(self) -> list:
files = []
# all files in local repo except .git, .github, .gitignore, temp
for root, dirs, file in os.walk("temp"):
dirs[:] = [d for d in dirs if d not in self.ignore_files]
for f in file:
if not f.endswith('.md'):
continue
files.append(os.path.join(root, f))
return files
def compare(self):
local_files = self.get_local_files()
remote_files = self.get_remote_files()
# print("Local files", local_files)
# print("Remote files", remote_files)
threads = []
for file in local_files:
t = threading.Thread(target=self.get_hash, args=(file, "local"))
t.start()
threads.append(t)
for file in remote_files:
t = threading.Thread(target=self.get_hash, args=(file, "remote"))
t.start()
threads.append(t)
for t in threads:
t.join()
# print("local", self.local_files_hash.keys())
# print("remote", self.remote_files_hash.keys())
modified_files = []
new_files = []
# get common files using set intersection
common_files = set(self.local_files_hash.keys()).intersection(
set(self.remote_files_hash.keys()))
# print("common files", common_files)
# get modified files
for file in common_files:
if self.local_files_hash[file] != self.remote_files_hash[file]:
modified_files.append(file)
# get new files that are in local but not in remote
for file in self.local_files_hash.keys():
if not file in common_files:
new_files.append(file)
Log.info("Files modified")
for mf in modified_files:
Log.warn("Modified file ", mf)
for nf in new_files:
Log.warn("New file ", nf)
return modified_files + new_files
def get_file_name(self, name):
return name.split(self.repo_name)[-1]
def get_hash(self, file, repo):
with open(file, "rb") as f:
file_hash = hashlib.sha256(f.read()).hexdigest()
if repo == "local":
f = self.get_file_name(file)
self.local_files_hash[f] = file_hash
else:
f = self.get_file_name(file)
self.remote_files_hash[f] = file_hash
if __name__ == "__main__":
curent_dir = os.getcwd()
cm = CompareGitRepo(
curent_dir, "https://github.com/shhossain/computer_science")
modified_files = cm.compare()
test_all(modified_files)