-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtester.py
96 lines (83 loc) · 3.81 KB
/
tester.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
from difflib import SequenceMatcher
from os import listdir
import zipfile
import sys
import os.path
import shutil
from termcolor import colored
import subprocess
from slugify import slugify
def run(cmd, logfile):
file = open(logfile,"w")
p = subprocess.Popen(cmd, shell=True, universal_newlines=True, stdout=file)
ret_code = p.wait()
file.flush()
return ret_code
args = sys.argv
extract = './tmp'
if len(args) < 2:
print("Você deve utilizar pelo menos dois parâmetros.")
sys.exit(0)
file = args[1]
(name,ext) = os.path.splitext(file)
if len(args) > 2:
correction = args[2];
if not os.path.isfile(file):
print("O arquivo passado como parâmetro '{}' não existe.".format(file))
sys.exit(0)
print("Limpando a pasta temporária.")
shutil.rmtree(extract, ignore_errors=True)
print("Extraindo arquivo '{}' para a pasta '{}'.".format(file, extract))
zipRef = zipfile.ZipFile(file, 'r')
zipRef.extractall(extract)
zipRef.close()
location = extract + '/' + name.replace('./', '')
print("Normalizando os nomes das pastas.")
for folder in listdir(location):
if folder != '.DS_Store':
locationInside = location + '/' + folder
newFolderName = slugify(folder)
newLocationInside = location + '/' + newFolderName
os.rename(locationInside, newLocationInside)
print("Recuperando arquivos de '{}'".format(location))
for folder in listdir(location):
if folder != '.DS_Store':
locationInside = location + '/' + folder
source = locationInside + '/' + listdir(locationInside)[0]
print("Análise de similaridade para {}".format(locationInside))
for folderCheck in listdir(location):
if folderCheck != '.DS_Store' and folder != folderCheck:
locationCheck = location + '/' + folderCheck
sourceCheck = locationCheck + '/' + listdir(locationCheck)[0]
try:
fileSource = open(source, "r").read()
fileSourceCheck = open(sourceCheck, "r").read()
equals = SequenceMatcher(None, fileSource, fileSourceCheck).ratio()
if equals > 0.5:
print(colored(" {} - {:.0f}% igual.".format(sourceCheck, (equals*100)), 'red'))
else:
print(colored(" {} - OK!".format(sourceCheck), 'green'))
except Exception as e:
print(colored(" {} - ERRO DE VERIFICAÇÃO".format(sourceCheck), 'red'))
print("Executando a compilação do programa.")
locationCompiled = locationInside + "/result.out"
executeCompiled = "./" + locationCompiled
outputCompiled = locationInside + "/result.txt"
print("Compilando o programa: '{}'".format(executeCompiled))
try:
subprocess.call(["gcc", source, "-o", locationCompiled])
output = run(executeCompiled, outputCompiled)
if correction:
print("Comparando a saída de dados com a correção.")
fileSourceOutput = open(outputCompiled, "r").read()
fileSourceOutputCheck = open(correction, "r").read()
equalsOutput = SequenceMatcher(None, fileSourceOutput, fileSourceOutputCheck).ratio()
if equalsOutput < 0.9:
print(colored(" {} - {:.0f}% igual a resposta.".format(correction, (equals*100)), 'red'))
else:
print(colored(" {} - RESPOSTA CORRETA!".format(correction), 'green'))
except Exception as e:
print(colored(" {} - ERRO NA COMPILAÇÃO!".format(sourceCheck), 'red'))
print("Executando o programa e salvando a saída.")
print("Limpando a pasta temporária.")
shutil.rmtree(extract, ignore_errors=True)