-
Notifications
You must be signed in to change notification settings - Fork 10
/
find_parameter_set.py
153 lines (102 loc) · 3.65 KB
/
find_parameter_set.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python3
import argparse
import os
import subprocess
import sys
from pathlib import Path
from typing import List, Tuple
# pip install colorama
from colorama import Fore, Style
class UnknownParameter:
def __init__(self):
self.bool0: bool = False
self.bool1: bool = False
self.bool2: bool = False
self.int0: int = 0
self.numUserLayers: int = 0 # @todo not required anymore
self.additionalStr2: int = 0
def permute_unknown_parameters():
param_set = UnknownParameter()
for bool1 in [True, False]:
param_set.bool1 = bool1
for bool2 in [True, False]:
param_set.bool2 = bool2
for int0 in range(17):
param_set.int0 = int0
for additionalStr2 in range(45):
param_set.additionalStr2 = additionalStr2
yield param_set
def print_found(result: Tuple[str, str, bool]) -> None:
print(Fore.GREEN)
print("---------- Found valid Parameter Set -----------")
print(result[1])
print(Style.RESET_ALL)
def print_not_found(result: Tuple[str, str, bool]) -> None:
print(Fore.RED)
print("-------- No valid parameter set found! ---------")
print(result[1])
print(Style.RESET_ALL)
def print_result(result: Tuple[str, str, bool]) -> None:
command = result[1]
found_param_set = result[2]
if found_param_set:
print(Fore.GREEN, end="")
else:
print(Fore.RED, end="")
print(command)
print(Style.RESET_ALL, end="", flush=True)
parser = argparse.ArgumentParser()
parser.add_argument(
"path", help="Path to `*.pad` file or folder containing them", type=str
)
args = parser.parse_args()
if __name__ == "__main__":
if not os.path.exists(args.path):
print(f"{args.path} does not exist!")
sys.exit(-1)
if not os.path.isdir(args.path) and not os.path.isfile(args.path):
print(f"{args.path} needs to be a file or directory!")
sys.exit(-1)
path_pads = []
if os.path.isfile(args.path):
path_pads = [args.path]
if os.path.isdir(args.path):
path_pads = Path(args.path).rglob("*.pad")
# List of tuples with pad path, command and whether
# a working command was found
results: List[Tuple[str, str, bool]] = []
for path_pad in path_pads:
path_pad = str(path_pad)
print(f"Evaluate {path_pad}")
cmd = ["./build/cli/OpenAllegroParser-cli", "-i", path_pad]
found_param_set = False
for param_set in permute_unknown_parameters():
tmp_cmd = cmd.copy()
if param_set.bool0:
tmp_cmd += ["--bool0"]
if param_set.bool1:
tmp_cmd += ["--bool1"]
if param_set.bool2:
tmp_cmd += ["--bool2"]
if param_set.int0 > 0:
tmp_cmd += ["--int0", str(param_set.int0)]
if param_set.numUserLayers > 0:
tmp_cmd += ["--numUserLayers", str(param_set.numUserLayers)]
if param_set.additionalStr2 > 0:
tmp_cmd += ["--additionalStr2", str(param_set.additionalStr2)]
# Shows progress
print(".", end="", flush=True)
proc = subprocess.run(tmp_cmd, capture_output=True)
if proc.returncode == 0:
found_param_set = True
cmd = tmp_cmd.copy()
break
results += [(path_pad, " ".join(cmd), found_param_set)]
if found_param_set:
print_found(results[-1])
else:
print_not_found(results[-1])
print()
print("Summary:")
for result in results:
print_result(result)