From 13356348daeb15e6375840e1e94609d76e2188d4 Mon Sep 17 00:00:00 2001 From: Boyan Date: Wed, 2 Oct 2024 15:59:24 +0200 Subject: [PATCH] Added Help texts and optional tests path --- ucompile | 91 ++++++++++++++++++++++++++------------------------------ 1 file changed, 43 insertions(+), 48 deletions(-) diff --git a/ucompile b/ucompile index cc7fa1f..04d78c0 100755 --- a/ucompile +++ b/ucompile @@ -1,67 +1,62 @@ #!/bin/python3 import argparse -from os import system,popen +import os from glob import glob -def run_input_cases(program, verbose, output=False): - input_files = glob("./*.in") +def run_input_cases(program, verbose, output=False, tests_dir="."): + input_files = glob(os.path.join(tests_dir, "*.in")) if len(input_files) < 1: - - return system(program) + return os.system(program) for input_file in input_files: print(f"Running {input_file}") end_command = f"{program} < {input_file}" if verbose: print("Input:") - system(f"cat {input_file}") + os.system(f"cat {input_file}") print("") - try: - if (out := glob(input_file.replace(".in", ".out"))[0]): - if verbose: - print("Expected output:") - system(f"cat {out}") - print("") - if output: - end_command += f" | diff {out} -" - except IndexError: - pass - print(popen(end_command).read()) - + out_file = input_file.replace(".in", ".out") + if os.path.exists(out_file): + if verbose: + print("Expected output:") + os.system(f"cat {out_file}") + print("") + if output: + end_command += f" | diff {out_file} -" + print(os.popen(end_command).read()) print("-----------") - return -def python_compile(file_name, verbose=False): - run_input_cases("python3 " + file_name, verbose) +def python_compile(file_name, verbose=False, tests_dir="."): + run_input_cases("python3 " + file_name, verbose, tests_dir=tests_dir) return -def java_compile(file_name, maven=False, verbose=False): +def java_compile(file_name, maven=False, verbose=False, tests_dir="."): if maven: - system("mvn clean verify") - system("java -jar target/*.jar") + os.system("mvn clean verify") + os.system("java -jar target/*.jar") return - - system("javac " + file_name) - run_input_cases("java " + file_name[:file_name.rfind(".")], verbose) + os.system("javac " + file_name) + run_input_cases("java " + file_name[:file_name.rfind(".")], verbose, tests_dir=tests_dir) return -def c_compile(file_name, verbose=False, output=False): +def c_compile(file_name, verbose=False, output=False, tests_dir="."): # Style the code in Meijster's way - system("astyle -A2s2cxgk3W3xbj " + file_name) - system("gcc -Wall -pedantic --std=c99 -g -o program -lm -Wno-unused-result " + file_name) - run_input_cases("./program", verbose, output) + os.system("astyle -A2s2cxgk3W3xbj " + file_name) + os.system("gcc -Wall -pedantic --std=c99 -g -o program -lm -Wno-unused-result " + file_name) + run_input_cases("./program", verbose, output, tests_dir=tests_dir) return def main(): - SUPPORTED_LANGS = [".c", ".py"] + SUPPORTED_LANGS = [".c", ".py", ".java"] parser = argparse.ArgumentParser( - prog='UCompiler', - description='Compiles based on language', - epilog='LOL') - parser.add_argument("filename", metavar="N", type=str) - parser.add_argument("-v", "--verbose", action="store_true", default=False) - parser.add_argument("-a", "--alternative", action="store_true", default=False) - parser.add_argument("-o", "--output", action="store_true", default=False) + prog='UCompiler', + description='Compiles based on language', + epilog='LOL') + parser.add_argument("filename", metavar="", type=str, help="File to compile") + parser.add_argument("-v", "--verbose", action="store_true", default=False, help="Prints input and output") + parser.add_argument("-a", "--alternative", action="store_true", default=False, help="Uses alternative compiler to compile") + parser.add_argument("-o", "--output", action="store_true", default=False, help="Checks diff for each .in file with .out file") + parser.add_argument("-t", "--tests", metavar="", type=str, help="Directory to run tests") args = parser.parse_args() file_name = args.filename @@ -70,16 +65,16 @@ def main(): print("Unsupported language") return - if file_ext == ".c": - c_compile(file_name, args.verbose, args.output) - elif file_ext == ".py": - python_compile(file_name, args.verbose) - elif file_ext == ".java": - java_compile(file_name, args.verbose, maven=args.alternative) - return + tests_dir = args.tests if args.tests else "." + if file_ext == ".c": + c_compile(file_name, args.verbose, args.output, tests_dir=tests_dir) + elif file_ext == ".py": + python_compile(file_name, args.verbose, tests_dir=tests_dir) + elif file_ext == ".java": + java_compile(file_name, args.alternative, args.verbose, tests_dir=tests_dir) + + return if __name__ == "__main__": main() - -