Added Help texts and optional tests path
This commit is contained in:
parent
9212a2a0f7
commit
13356348da
79
ucompile
79
ucompile
@ -1,67 +1,62 @@
|
|||||||
#!/bin/python3
|
#!/bin/python3
|
||||||
import argparse
|
import argparse
|
||||||
from os import system,popen
|
import os
|
||||||
from glob import glob
|
from glob import glob
|
||||||
|
|
||||||
def run_input_cases(program, verbose, output=False):
|
def run_input_cases(program, verbose, output=False, tests_dir="."):
|
||||||
input_files = glob("./*.in")
|
input_files = glob(os.path.join(tests_dir, "*.in"))
|
||||||
if len(input_files) < 1:
|
if len(input_files) < 1:
|
||||||
|
return os.system(program)
|
||||||
return system(program)
|
|
||||||
for input_file in input_files:
|
for input_file in input_files:
|
||||||
print(f"Running {input_file}")
|
print(f"Running {input_file}")
|
||||||
end_command = f"{program} < {input_file}"
|
end_command = f"{program} < {input_file}"
|
||||||
if verbose:
|
if verbose:
|
||||||
print("Input:")
|
print("Input:")
|
||||||
system(f"cat {input_file}")
|
os.system(f"cat {input_file}")
|
||||||
print("")
|
print("")
|
||||||
try:
|
out_file = input_file.replace(".in", ".out")
|
||||||
if (out := glob(input_file.replace(".in", ".out"))[0]):
|
if os.path.exists(out_file):
|
||||||
if verbose:
|
if verbose:
|
||||||
print("Expected output:")
|
print("Expected output:")
|
||||||
system(f"cat {out}")
|
os.system(f"cat {out_file}")
|
||||||
print("")
|
print("")
|
||||||
if output:
|
if output:
|
||||||
end_command += f" | diff {out} -"
|
end_command += f" | diff {out_file} -"
|
||||||
except IndexError:
|
print(os.popen(end_command).read())
|
||||||
pass
|
|
||||||
print(popen(end_command).read())
|
|
||||||
|
|
||||||
print("-----------")
|
print("-----------")
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
def python_compile(file_name, verbose=False):
|
def python_compile(file_name, verbose=False, tests_dir="."):
|
||||||
run_input_cases("python3 " + file_name, verbose)
|
run_input_cases("python3 " + file_name, verbose, tests_dir=tests_dir)
|
||||||
return
|
return
|
||||||
|
|
||||||
def java_compile(file_name, maven=False, verbose=False):
|
def java_compile(file_name, maven=False, verbose=False, tests_dir="."):
|
||||||
if maven:
|
if maven:
|
||||||
system("mvn clean verify")
|
os.system("mvn clean verify")
|
||||||
system("java -jar target/*.jar")
|
os.system("java -jar target/*.jar")
|
||||||
|
return
|
||||||
|
os.system("javac " + file_name)
|
||||||
|
run_input_cases("java " + file_name[:file_name.rfind(".")], verbose, tests_dir=tests_dir)
|
||||||
return
|
return
|
||||||
|
|
||||||
system("javac " + file_name)
|
def c_compile(file_name, verbose=False, output=False, tests_dir="."):
|
||||||
run_input_cases("java " + file_name[:file_name.rfind(".")], verbose)
|
|
||||||
return
|
|
||||||
|
|
||||||
def c_compile(file_name, verbose=False, output=False):
|
|
||||||
# Style the code in Meijster's way
|
# Style the code in Meijster's way
|
||||||
system("astyle -A2s2cxgk3W3xbj " + file_name)
|
os.system("astyle -A2s2cxgk3W3xbj " + file_name)
|
||||||
system("gcc -Wall -pedantic --std=c99 -g -o program -lm -Wno-unused-result " + file_name)
|
os.system("gcc -Wall -pedantic --std=c99 -g -o program -lm -Wno-unused-result " + file_name)
|
||||||
run_input_cases("./program", verbose, output)
|
run_input_cases("./program", verbose, output, tests_dir=tests_dir)
|
||||||
return
|
return
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
SUPPORTED_LANGS = [".c", ".py"]
|
SUPPORTED_LANGS = [".c", ".py", ".java"]
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
prog='UCompiler',
|
prog='UCompiler',
|
||||||
description='Compiles based on language',
|
description='Compiles based on language',
|
||||||
epilog='LOL')
|
epilog='LOL')
|
||||||
parser.add_argument("filename", metavar="N", type=str)
|
parser.add_argument("filename", metavar="<file>", type=str, help="File to compile")
|
||||||
parser.add_argument("-v", "--verbose", action="store_true", default=False)
|
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)
|
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)
|
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="<path>", type=str, help="Directory to run tests")
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
file_name = args.filename
|
file_name = args.filename
|
||||||
@ -70,16 +65,16 @@ def main():
|
|||||||
print("Unsupported language")
|
print("Unsupported language")
|
||||||
return
|
return
|
||||||
|
|
||||||
if file_ext == ".c":
|
tests_dir = args.tests if args.tests else "."
|
||||||
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
|
|
||||||
|
|
||||||
|
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__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user