Skip to content
This repository was archived by the owner on Oct 3, 2021. It is now read-only.

Commit 8b7254e

Browse files
committed
Improve script for XCSP task creation: argparse, main method
1 parent 28fbe77 commit 8b7254e

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

c/xcsp/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ IGNORE_DIRS := ./original/
1111
include $(LEVEL)/Makefile.config
1212

1313
tasks:
14-
python3 create_from_xmls.py original/ .
14+
python3 create_from_xmls.py original/ --output .
1515
clang-format -i *.c
1616
reuse addheader --template header.jinja2 --year 2020 --copyright "The SV-Benchmarks Community" --license MIT *c
1717
reuse addheader --template header.jinja2 --year 2016 --copyright "Gilles Audemard" --license MIT *c

c/xcsp/create_from_xmls.py

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
""" to run this script you need to install libxml2-dev, libboost-dev and clang-format (sudo apt-get install %s) [Python 3.6.9] """
99

10+
import argparse
1011
import os
1112
from pathlib import Path
1213
import sys
@@ -47,15 +48,26 @@ def find_all_constraint_definitions(path):
4748
return path.glob("**/*.xml")
4849

4950

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+
5869

70+
def create_tasks(path, output):
5971
# loop through every constraints file in the input folder
6072
all_constraint_defs = list(find_all_constraint_definitions(path))
6173
task_amount = len(all_constraint_defs)
@@ -104,11 +116,20 @@ def find_all_constraint_definitions(path):
104116
taskdef_file = output / (program_file.name[:-1] + "yml")
105117
with open(taskdef_file, "w+") as outp:
106118
outp.write(get_taskdef(program_file.name, verdict))
107-
print("")
108119

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

Comments
 (0)