|
7 | 7 |
|
8 | 8 | """ to run this script you need to install libxml2-dev, libboost-dev and clang-format (sudo apt-get install %s) [Python 3.6.9] """ |
9 | 9 |
|
| 10 | +import argparse |
10 | 11 | import os |
11 | 12 | from pathlib import Path |
12 | 13 | import sys |
@@ -47,15 +48,26 @@ def find_all_constraint_definitions(path): |
47 | 48 | return path.glob("**/*.xml") |
48 | 49 |
|
49 | 50 |
|
50 | | -""" |
51 | | - The script needs 2 parameters 1) path to benchmarks folder (parent directory) 2) path to outputfolder |
52 | | -""" |
53 | | -if len(sys.argv) == 3: |
54 | | - # create input and output path |
55 | | - path = Path(sys.argv[1]) |
56 | | - output = Path(sys.argv[2]) |
57 | | - os.makedirs(output, exist_ok=True) |
| 51 | +def parse_args(argv): |
| 52 | + parser = argparse.ArgumentParser() |
| 53 | + parser.add_argument( |
| 54 | + "--output", default=".", help="output directory for benchmark tasks" |
| 55 | + ) |
| 56 | + parser.add_argument( |
| 57 | + "benchmark_directory", |
| 58 | + help="directory that contains original XCSP benchmark tasks", |
| 59 | + ) |
| 60 | + args = parser.parse_args(argv) |
| 61 | + |
| 62 | + args.output = Path(args.output) |
| 63 | + args.benchmark_directory = Path(args.benchmark_directory) |
| 64 | + if not args.benchmark_directory.exists(): |
| 65 | + raise ValueError(f"Directory does not exist: {str(args.benchmark_directory)}") |
| 66 | + |
| 67 | + return args |
| 68 | + |
58 | 69 |
|
| 70 | +def create_tasks(path, output): |
59 | 71 | # loop through every constraints file in the input folder |
60 | 72 | all_constraint_defs = list(find_all_constraint_definitions(path)) |
61 | 73 | task_amount = len(all_constraint_defs) |
@@ -104,11 +116,20 @@ def find_all_constraint_definitions(path): |
104 | 116 | taskdef_file = output / (program_file.name[:-1] + "yml") |
105 | 117 | with open(taskdef_file, "w+") as outp: |
106 | 118 | outp.write(get_taskdef(program_file.name, verdict)) |
107 | | - print("") |
108 | 119 |
|
109 | | -else: |
110 | | - print( |
111 | | - 'You have to pass two ABSOLUTE paths.\nThe file has to be placed in the directory with the executable and compiled file of XCSP.\nType "python ' |
112 | | - + sys.argv[0] |
113 | | - + ' absolute/path/to/benchmarks absolute/path/for/translated/files"' |
114 | | - ) |
| 120 | + |
| 121 | +def main(argv=None): |
| 122 | + if argv is None: |
| 123 | + argv = sys.argv[1:] |
| 124 | + args = parse_args(argv) |
| 125 | + os.makedirs(args.output, exist_ok=True) |
| 126 | + |
| 127 | + try: |
| 128 | + create_tasks(args.benchmark_directory, args.output) |
| 129 | + finally: |
| 130 | + # required to reset progressbar |
| 131 | + print("", file=sys.stderr) |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + sys.exit(main()) |
0 commit comments