Skip to content

Commit

Permalink
update testcase-help
Browse files Browse the repository at this point in the history
  • Loading branch information
X3nom committed Dec 9, 2024
1 parent cb51efa commit 0abb1e6
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,5 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

.vscode/
140 changes: 128 additions & 12 deletions src/testcase-help.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
import os
import glob
import chardet
import subprocess
import zipfile

VERBOSE = False # global variable holding whether do verbose prints or not
verb_print = lambda message: print(message) if VERBOSE else None



#============================================================
# FIX ENCODING
#============================================================

def fix_encoding_file(file_path):
'''
Expand Down Expand Up @@ -67,21 +71,115 @@ def fix_encoding(args :argparse.Namespace):



#============================================================
# GENERATE / CHECK
#============================================================

def gen_check_res(args :argparse.Namespace, gen=True):

test_i = 1
for in_file in glob.glob(f"**/*in*", root_dir=args.path, recursive=True):

in_file_path = f"{args.path}/{in_file}"

dir_path = f"{args.path}/{os.path.dirname(in_file)}"
dir_name = os.path.basename(dir_path)

with open(in_file_path, "r") as f:
in_txt = f.read()


if(gen): cmd = args.generate_answers
else: cmd = args.verify_answers

process = subprocess.run(
cmd,
input=in_txt,
text=True, # Treat input and output as strings
capture_output=True, # Capture stdout and stderr
shell=True
)
if process.returncode != 0:
print(f"Error: '{cmd}' failed on '{in_file_path}' with exit code {process.returncode}")

ans_txt = process.stdout


if gen:
with open(f"{dir_path}/ans.txt", "w") as f:
f.write(ans_txt)

else:
out_files = glob.glob(f"*ans*", root_dir=dir_path) + glob.glob(f"*out*", root_dir=dir_path)

if len(out_files) != 1:
print(f"Error: missing .out/.ans file in {dir_path}")
continue

with open(f"{dir_path}/{out_files[0]}", "r") as f:
orig_ans_txt = f.read()

if ans_txt != orig_ans_txt:
print(f"Warning: answer in \"{dir_name}\" does NOT Match answer generated with \"{cmd}\"")
else:
verb_print(f" answer in {dir_name} matching")



#============================================================
# ZIP
#============================================================



def make_import_zip(args :argparse.Namespace):
zipf = zipfile.PyZipFile(f"{args.name}.zip", "w")
zipf.mkdir("data")
zipf.mkdir("data/secret")

zipf.writestr("problem.yaml", f"# autogenerated by testcase-help.py\nname: '{args.name}'")

test_i = 1
for in_file in glob.glob(f"**/*in*", root_dir=args.path, recursive=True):

dir_path = f"{args.path}/{os.path.dirname(in_file)}"
dir_name = os.path.basename(dir_path)

zipf.write(f"{args.path}/{in_file}", f"data/secret/{test_i}.in")
zipf.writestr(f"data/secret/{test_i}.desc", dir_name)

out_files = glob.glob(f"*ans*", root_dir=dir_path) + glob.glob(f"*out*", root_dir=dir_path)

if len(out_files) != 1:
print(f"Error: missing .out/.ans file in {dir_path}")

else:
zipf.write(f"{dir_path}/{out_files[0]}", f"data/secret/{test_i}.ans")


zipf.close()





def main():
global VERBOSE
arg_parser = argparse.ArgumentParser()

arg_parser.add_argument("path", metavar="PATH", help="Path to a file or directory")

arg_parser.add_argument(
"path",
metavar="PATH",
help="Path to a file or directory"
)
arg_parser.add_argument(
"-V", "--verbose",
action="store_true",
help="enable verbose output"
)
arg_parser.add_argument(
"-r", "--recursive",
action="store_true",
"-r", "--recursive",
action="store_true",
help="process directories recursively"
)
arg_parser.add_argument(
Expand All @@ -90,27 +188,45 @@ def main():
help="fix encoding of file(s) to UTF-8 LF"
)
arg_parser.add_argument(
"-g", "--generate-results",
"-g", "--generate-answers",
metavar="CMD",
help="run CMD on every in.txt and generate out.txt"
)
arg_parser.add_argument(
"-c", "--check-results",
"-v", "--verify-answers",
metavar="CMD",
help="run CMD for in.txt and verify correctness of out.txt"
)


arg_parser.add_argument(
"-z", "--make-zip",
action="store_true",
help="create mass upload zip from directory"
)
arg_parser.add_argument(
"-n", "--name",
default="unnamed",
help="specify name for zip"
)

args = arg_parser.parse_args()
VERBOSE = args.verbose # set global VERBOSE (used by verbose print)

VERBOSE = args.verbose


if args.fix_encoding:
if args.fix_encoding: # fix encodingss
if fix_encoding(args) == 1: return 1


if args.generate_answers is not None:
gen_check_res(args)

if args.verify_answers is not None:
gen_check_res(args)


if args.make_zip:
make_import_zip(args)




if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions test/test2/ans.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
1 change: 1 addition & 0 deletions test/test2/in.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
3 changes: 0 additions & 3 deletions test/test2/testfile2.txt

This file was deleted.

3 changes: 0 additions & 3 deletions test/testfile.txt

This file was deleted.

1 change: 1 addition & 0 deletions test/tst.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print(input())

0 comments on commit 0abb1e6

Please sign in to comment.