Last active
August 29, 2015 14:17
-
-
Save andrzejressel/e44795ce3dc44d1b00ab to your computer and use it in GitHub Desktop.
Sprawdzarka
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import os | |
import sys | |
import subprocess | |
import glob | |
import re | |
try: | |
from colorama import init | |
from termcolor import colored | |
init() | |
colors = True | |
except ImportError: | |
colors = False | |
def atoi(text): | |
return int(text) if text.isdigit() else text | |
def natural_keys(text): | |
''' | |
alist.sort(key=natural_keys) sorts in human order | |
http://nedbatchelder.com/blog/200712/human_sorting.html | |
(See Toothy's implementation in the comments) | |
''' | |
return [atoi(c) for c in re.split('(\d+)', text)] | |
# -p /home/andrzej/Moje_programy/Uczelnia/Algorytmy/Lista_2/build/mon1 -i /home/andrzej/in -o /home/andrzej/out | |
def main(): | |
parser = argparse.ArgumentParser(description='Process some integers.') | |
parser.add_argument('-p', '--program', help='Lokalizacja programu (exe)', required=True) | |
parser.add_argument('-i', '--input', help='Lokalizacja folderu w wejściami', required=True) | |
parser.add_argument('-o', '--output', help='Lokalizacja folderu w wynikami', required=False) | |
args = parser.parse_args() | |
program = args.program | |
input = args.input | |
output = args.output | |
success = True | |
pliki = glob.glob(input + os.path.sep + "*.in") | |
pliki.sort(key=natural_keys) | |
if output is not None: | |
# Sprawdzanie wyników | |
for file in pliki: | |
try: | |
wynik = subprocess.check_output([program, file]).decode('utf-8').rstrip() | |
except subprocess.CalledProcessError: | |
# TODO: Coś z tym zrobić | |
continue | |
nazwaPliku = file.split("/")[-1].split(".")[0] | |
spodziewanyWynik = open(output + os.path.sep + nazwaPliku + ".out").read().rstrip() | |
if wynik == spodziewanyWynik: | |
color = 'green' | |
else: | |
success = False | |
color = 'red' | |
if colors: | |
print(colored(nazwaPliku + " : " + wynik + " vs " + spodziewanyWynik, color)) | |
else: | |
print(nazwaPliku + " : " + wynik + " vs " + spodziewanyWynik) | |
else: | |
# Tryb oddawania lub pojedyńczego pliku (bez podanych wyników) | |
if os.path.isdir(input): | |
for file in pliki: | |
wynik = subprocess.check_output([program, file]).decode('utf-8').rstrip() | |
nazwaPliku = file.split("/")[-1] | |
print(nazwaPliku + " : " + wynik) | |
else: | |
wynik = subprocess.check_output([program, input]).decode('utf-8').rstrip() | |
nazwaPliku = input.split("/")[-1] | |
print(nazwaPliku + " : " + wynik) | |
if success: | |
return 0 | |
else: | |
return 1 | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment