Formatted + passing all args to each function
This commit is contained in:
parent
53f66d440c
commit
d14c0a725c
121
ucompile
121
ucompile
@ -17,13 +17,24 @@ def check_dependencies() -> None:
|
|||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
def run_input_cases(program, verbose, output=False, tests_dir=".", time_flag=False):
|
def run_input_cases(program, args):
|
||||||
|
verbose = args.verbose
|
||||||
|
output = args.output
|
||||||
|
tests_dir = args.tests_dir if args.tests_dir else "."
|
||||||
|
time_flag = args.time
|
||||||
|
|
||||||
input_files = glob(os.path.join(tests_dir, "*.in"))
|
input_files = glob(os.path.join(tests_dir, "*.in"))
|
||||||
|
|
||||||
if len(input_files) < 1:
|
if len(input_files) < 1:
|
||||||
if time_flag:
|
if time_flag:
|
||||||
start_time = time.perf_counter()
|
start_time = time.perf_counter()
|
||||||
result = subprocess.run(program, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
|
result = subprocess.run(
|
||||||
|
program,
|
||||||
|
shell=True,
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.PIPE,
|
||||||
|
universal_newlines=True,
|
||||||
|
)
|
||||||
end_time = time.perf_counter()
|
end_time = time.perf_counter()
|
||||||
elapsed_time = end_time - start_time
|
elapsed_time = end_time - start_time
|
||||||
print(result.stdout.strip())
|
print(result.stdout.strip())
|
||||||
@ -36,23 +47,34 @@ def run_input_cases(program, verbose, output=False, tests_dir=".", time_flag=Fal
|
|||||||
print(f"Running {input_file}")
|
print(f"Running {input_file}")
|
||||||
end_command = f"{program} < {input_file}"
|
end_command = f"{program} < {input_file}"
|
||||||
|
|
||||||
with open(input_file, 'r') as f:
|
with open(input_file, "r") as f:
|
||||||
input_data = f.read()
|
input_data = f.read()
|
||||||
|
|
||||||
out_file = input_file.replace(".in", ".out")
|
out_file = input_file.replace(".in", ".out")
|
||||||
expected_output = None
|
expected_output = None
|
||||||
if os.path.exists(out_file):
|
if os.path.exists(out_file):
|
||||||
with open(out_file, 'r') as f:
|
with open(out_file, "r") as f:
|
||||||
expected_output = f.read()
|
expected_output = f.read()
|
||||||
|
|
||||||
# Measure time if time_flag is True
|
|
||||||
if time_flag:
|
if time_flag:
|
||||||
start_time = time.perf_counter()
|
start_time = time.perf_counter()
|
||||||
result = subprocess.run(end_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
|
result = subprocess.run(
|
||||||
|
end_command,
|
||||||
|
shell=True,
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.PIPE,
|
||||||
|
universal_newlines=True,
|
||||||
|
)
|
||||||
end_time = time.perf_counter()
|
end_time = time.perf_counter()
|
||||||
elapsed_time = end_time - start_time
|
elapsed_time = end_time - start_time
|
||||||
else:
|
else:
|
||||||
result = subprocess.run(end_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
|
result = subprocess.run(
|
||||||
|
end_command,
|
||||||
|
shell=True,
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.PIPE,
|
||||||
|
universal_newlines=True,
|
||||||
|
)
|
||||||
|
|
||||||
program_output = result.stdout
|
program_output = result.stdout
|
||||||
|
|
||||||
@ -81,7 +103,9 @@ def run_input_cases(program, verbose, output=False, tests_dir=".", time_flag=Fal
|
|||||||
print(f"{Fore.RED}Program output:{Style.RESET_ALL}")
|
print(f"{Fore.RED}Program output:{Style.RESET_ALL}")
|
||||||
print(program_output.strip())
|
print(program_output.strip())
|
||||||
else:
|
else:
|
||||||
print(f"{Fore.YELLOW}Expected output file not found; cannot compare outputs.{Style.RESET_ALL}")
|
print(
|
||||||
|
f"{Fore.YELLOW}Expected output file not found; cannot compare outputs.{Style.RESET_ALL}"
|
||||||
|
)
|
||||||
if not verbose:
|
if not verbose:
|
||||||
print("Program output:")
|
print("Program output:")
|
||||||
print(program_output.strip())
|
print(program_output.strip())
|
||||||
@ -97,66 +121,95 @@ def run_input_cases(program, verbose, output=False, tests_dir=".", time_flag=Fal
|
|||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
def python_compile(file_name, verbose=False, tests_dir=".", time=False):
|
def python_compile(file_name, args):
|
||||||
run_input_cases("python3 " + file_name, verbose, tests_dir=tests_dir, time_flag=time)
|
run_input_cases("python3 " + file_name, args)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
def java_compile(file_name, maven=False, verbose=False, tests_dir=".", time=False):
|
def java_compile(file_name, args):
|
||||||
if maven:
|
if args.alternative:
|
||||||
os.system("mvn clean verify")
|
os.system("mvn clean verify")
|
||||||
run_input_cases("java -jar target/*.jar", verbose, tests_dir=tests_dir, time_flag=time)
|
run_input_cases("java -jar target/*.jar", args)
|
||||||
return
|
return
|
||||||
os.system("javac " + file_name)
|
os.system("javac " + file_name)
|
||||||
run_input_cases("java " + file_name[:file_name.rfind(".")], verbose, tests_dir=tests_dir, time_flag=time)
|
class_name = os.path.splitext(file_name)[0]
|
||||||
|
run_input_cases("java " + class_name, args)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
def haskell_compile(file_name, verbose=False, tests_dir=".", time=False):
|
def haskell_compile(file_name, args):
|
||||||
os.system("ghc -O2 " + file_name + " -o program")
|
os.system("ghc -O2 " + file_name + " -o program")
|
||||||
run_input_cases("./program", verbose, tests_dir=tests_dir, time_flag=time)
|
run_input_cases("./program", args)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
def c_compile(file_name, verbose=False, output=False, tests_dir=".", time=False):
|
def c_compile(file_name, args):
|
||||||
# Style the code in Meijster's way
|
# Style the code in Meijster's way
|
||||||
os.system("astyle -A2s2cxgk3W3xbj " + file_name)
|
os.system("astyle -A2s2cxgk3W3xbj " + file_name)
|
||||||
os.system("gcc -Wall -pedantic --std=c99 -g -o program -lm -Wno-unused-result " + file_name)
|
os.system(
|
||||||
run_input_cases("./program", verbose, output, tests_dir=tests_dir, time_flag=time)
|
"gcc -Wall -pedantic --std=c99 -g -o program -lm -Wno-unused-result " + file_name
|
||||||
|
)
|
||||||
|
run_input_cases("./program", args)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
SUPPORTED_LANGS = [".c", ".py", ".java", ".hs"]
|
SUPPORTED_LANGS = [".c", ".py", ".java", ".hs"]
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
prog='UCompiler',
|
prog="UCompiler", description="Compiles based on language", epilog="LOL"
|
||||||
description='Compiles based on language',
|
)
|
||||||
epilog='LOL')
|
|
||||||
parser.add_argument("filename", metavar="<file>", type=str, help="File to compile")
|
parser.add_argument("filename", metavar="<file>", type=str, help="File to compile")
|
||||||
parser.add_argument("-v", "--verbose", action="store_true", default=False, help="Prints input and output")
|
parser.add_argument(
|
||||||
parser.add_argument("-a", "--alternative", action="store_true", default=False, help="Uses alternative compiler to compile")
|
"-v",
|
||||||
parser.add_argument("-o", "--output", action="store_true", default=False, help="Checks diff for each .in file with .out file")
|
"--verbose",
|
||||||
parser.add_argument("-t", "--tests", metavar="<path>", type=str, help="Directory to run tests")
|
action="store_true",
|
||||||
parser.add_argument("-T", "--time", action="store_true", default=False, help="Prints time taken to run each test case")
|
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="<path>", type=str, help="Directory to run tests"
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-T",
|
||||||
|
"--time",
|
||||||
|
action="store_true",
|
||||||
|
default=False,
|
||||||
|
help="Prints time taken to run each test case",
|
||||||
|
)
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
file_name = args.filename
|
file_name = args.filename
|
||||||
file_ext = file_name[file_name.rfind("."):]
|
file_ext = os.path.splitext(file_name)[1]
|
||||||
if file_ext not in SUPPORTED_LANGS:
|
if file_ext not in SUPPORTED_LANGS:
|
||||||
print("Unsupported language")
|
print("Unsupported language")
|
||||||
return
|
return
|
||||||
|
|
||||||
# Use the specified tests directory or default to current directory
|
args.tests_dir = args.tests if args.tests else "."
|
||||||
tests_dir = args.tests if args.tests else "."
|
|
||||||
|
|
||||||
if file_ext == ".c":
|
if file_ext == ".c":
|
||||||
c_compile(file_name, args.verbose, args.output, tests_dir=tests_dir, time=args.time)
|
c_compile(file_name, args)
|
||||||
elif file_ext == ".py":
|
elif file_ext == ".py":
|
||||||
python_compile(file_name, args.verbose, tests_dir=tests_dir, time=args.time)
|
python_compile(file_name, args)
|
||||||
elif file_ext == ".java":
|
elif file_ext == ".java":
|
||||||
java_compile(file_name, args.alternative, args.verbose, tests_dir=tests_dir, time=args.time)
|
java_compile(file_name, args)
|
||||||
elif file_ext == ".hs":
|
elif file_ext == ".hs":
|
||||||
haskell_compile(file_name, args.verbose, tests_dir=tests_dir, time=args.time)
|
haskell_compile(file_name, args)
|
||||||
|
|
||||||
|
print(args)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user