def prepare_code(files, executable_file, directory): # If the user got us one, compile the source code if files: # Try to compile the source code try: user_code = get_language_and_compile(files, directory) if type(user_code) is CCode: assert user_code.compilation_sucess, "Falha na compilação" log.uprint('Código compilado com sucesso!') except UnknownLanguageException: eprint('Não foi fornecido código de nenhuma linguagem conhecida') usage() exit(2) # If there was a compilation problem except AssertionError as e: # Notificate the user about the problem eprint("Falha na compilação!") eprint("OUTPUT >>>", user_code.gcc_file) # Show the compilation output and end the program with open(user_code.gcc_file, 'r') as gcc: log.eprint(gcc.read()) exit(1) elif executable_file: user_code = CodeLanguage() user_code.exec_file = executable_file user_code.directory = directory return user_code
def get_susy_files(susy_link): log.uprint("Detecting susy test files") html = requests.get(susy_link, verify=False) parser = SusyTestesHTMLParser() parser.feed(html.text) log.uprint("Sucessfully detected %d susy test files to download" % parser.count) return parser.get_in_files(), parser.get_res_files()
def usage(): log.uprint("""\ Barbie: O seu Susy local usage: python3 barbie.py [-h] [-e] [-u] [-l] [-t] [Arquivos de código] Opções: -e, --executable Executa a barbie utilizando um arquivo executavel, de um código já compilado. -u, --url Fornecer diretamente o link da página com os arquivos de teste do susy. -l, --local Não acessa o Susy e faz os testes com os arquivos já baixados anteriormente. -t, --timeout Define o timeout de execução de cada teste o valor deve ser dado em segundos. """)
def download_tests(susy_tests_url, in_files, res_files, dir_name): susy_path = susy_tests_url.split('/')[:-1] try: shutil.rmtree(dir_name) except FileNotFoundError: pass os.mkdir(dir_name) data_url = '/'.join(susy_path) + '/' log.uprint() try: for i in range(len(in_files)): os.mkdir(os.path.join(dir_name, str(i + 1))) # download test input log.uprint('Downloading file %d of %d' % (i * 2 + 1, len(in_files) * 2), end='\r') download_file(data_url + in_files[i], os.path.join(dir_name, str(i + 1), in_files[i])) # Download test output log.uprint('Downloading file %d of %d' % (i * 2 + 2, len(in_files) * 2), end='\r') download_file(data_url + res_files[i], os.path.join(dir_name, str(i + 1), res_files[i])) except Exception as e: log.eprint('Connection Failed!') log.eprint('Error:', e) log.eprint() log.eprint('Não foi possível obter os arquivos do sistema Susy') raise Exception
def run_tests(user_code, in_files, out_dir, timeout): size = len(in_files) log.uprint() for i in range(size): log.uprint('Executing test %d of %d' %(i+1, size), end='\r') log.uprint()
def compare(self): # Open our output and the susy answer susy = open(self.susy_file) try: test = open(self.out_file) except FileNotFoundError: folder = os.path.dirname(self.susy_file) for file in os.listdir(folder): if file.endswith(".out"): self.out_file = os.path.join(folder, file) break test = open(self.out_file) self.cmp_file = self.susy_file.split('.')[0] + '.cmp' # Let's write a .cmp comparison file with open(self.cmp_file, 'w') as barbie_out: s_lines = susy.readlines() t_lines = test.readlines() # Number of lines of each output msg_1 = "Arquivo de resposta: %d Linhas\n" % (len(s_lines)) msg_2 = "Arquivo de saida: %d Linhas\n" % (len(t_lines)) # Write to the file barbie_out.write(msg_1) barbie_out.write(msg_2) for i in range(min(len(s_lines), len(t_lines))): # If lines are differnt if s_lines[i] != t_lines[i]: # Save difference msg_s = "[%d] susy: %s" % (i, s_lines[i]) msg_o = "[%d] out: %s" % (i, t_lines[i]) if not first_error_out: self.first_error_out = msg_o self.first_error_susy = msg_s # Write to file barbie_out.write(msg_s) barbie_out.write(msg_o) difference_count+=1 # Number of different lines encountered self.difference_count += abs(len(s_lines) - len(t_lines)) msg_f = "\nDifference count: %d" % self.difference_count barbie_out.write(msg_f+'\n') if not self.difference_count: msg_t = "Teste %d: Resultado correto!" % (self.id) else: msg_t = "Teste %d: Resultado incorreto. Linhas divergentes: %d" % (self.id, self.difference_count) log.uprint(msg_t,) susy.close() test.close() self.correct = (self.difference_count==0)