Skip to content

Commit

Permalink
update readme, parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
X3nom committed Dec 6, 2024
1 parent 69b996c commit cb51efa
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 21 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
# domjudge-testcase-tools
Tooling for checking correct file encodings, and correctness of testcases

## usage
```
usage: testcase-help.py [-h] [-V] [-r] [-f] [-g CMD] [-c CMD] PATH
positional arguments:
PATH Path to a file or directory
options:
-h, --help show this help message and exit
-V, --verbose enable verbose output
-r, --recursive process directories recursively
-f, --fix-encoding fix encoding of file(s) to UTF-8 LF
-g CMD, --generate-results CMD
run CMD on every in.txt and generate out.txt
-c CMD, --check-results CMD
run CMD for in.txt and verify correctness of out.txt
```
64 changes: 44 additions & 20 deletions src/testcase-help.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,29 @@
import glob
import chardet

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




def fix_encoding_file(file_path):
'''
Change file's encoding to UTF-8 LE (the UNIX standard)
'''
verb_print(f" fixing encoding of {file_path}")

with open(file_path, "rb") as file:
raw_data = file.read()
detected_encoding = chardet.detect(raw_data)["encoding"]

# Read the file with the detected encoding
with open(file_path, "r", encoding=detected_encoding) as file:
content = file.read()
try:
with open(file_path, "r", encoding=detected_encoding) as file:
content = file.read()
except:
verb_print(f"ERROR: skipping \"{file_path}\", the file could not be processed!")
return 1

# Normalize line endings to LF and prepare UTF-8 content
normalized_content = content.replace("\r\n", "\n").replace("\r", "\n")
Expand All @@ -37,51 +47,65 @@ def fix_encoding_dir(path, recursive=False):


def fix_encoding(args :argparse.Namespace):
print(f"Fixing encoding of {args.path}")

if args.dir:
if not os.path.isdir(args.path):
print(f"Error: {args.path} is not a valid directory!")
return 1
verb_print(f"TASK: fix-encoding of {args.path}")


if os.path.isdir(args.path):
verb_print(f" directory detected, processing")
fix_encoding_dir(args.path, args.recursive)

else:
if not os.path.isfile(args.path):
print(f"Error: {args.path} is not a valid file!")
return 1

elif os.path.isfile(args.path):
fix_encoding_file(args.path)

print("Success!")
else:
print(f"Error: {args.path} is not a valid file or directory!")
return 1


verb_print("TASK DONE!")




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(
"-d", "--dir",
"-V", "--verbose",
action="store_true",
help="Treat the path as a directory (default: treat as a file)"
help="enable verbose output"
)

arg_parser.add_argument(
"-rec", "--recursive",
"-r", "--recursive",
action="store_true",
help="Process directories recursively"
help="process directories recursively"
)

arg_parser.add_argument(
"-fe", "--fix-encoding",
"-f", "--fix-encoding",
action="store_true",
help="fix encoding of file(s) to UTF-8 LF"
)
arg_parser.add_argument(
"-g", "--generate-results",
metavar="CMD",
help="run CMD on every in.txt and generate out.txt"
)
arg_parser.add_argument(
"-c", "--check-results",
metavar="CMD",
help="run CMD for in.txt and verify correctness of out.txt"
)



args = arg_parser.parse_args()

VERBOSE = args.verbose


if args.fix_encoding:
if fix_encoding(args) == 1: return 1
Expand Down
3 changes: 2 additions & 1 deletion test/testfile.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
asdfbn;l
adfbn;l
as;dfk
sd

0 comments on commit cb51efa

Please sign in to comment.