-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
compiler.py
executable file
·305 lines (240 loc) · 8.69 KB
/
compiler.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python3
import argparse as ap
from pathlib import Path
from regex import DOTALL, MULTILINE, match, sub, findall
from typing import Union
from random import choice
def parse_args():
"""
Parse command line arguments
Returns:
argparse.Namespace: Parsed arguments
"""
parser = ap.ArgumentParser(
description='Groovy Script "Compiler" for FileBot File Format'
)
parser.add_argument("input", type=str, help="Input Groovy Script")
parser.add_argument("output", type=str, help="Output FileBot File")
return parser.parse_args()
def remove_comments(text: str) -> str:
"""
Remove comment contents from output file
Args:
text (str): Text to sanitize
Returns:
str: Sanitized text
"""
lines = text.splitlines()
# remove inline comments with regex, UNLESS IT'S PART OF URL
lines = [sub(r"(?: +)?(?<!http:|https:)//.*", "", line) for line in lines]
# Remove block comments
text = "\n".join(lines)
text = sub(r"/\*.*?\*/", "", text, flags=DOTALL)
# Remove empty curlied-comment
text = sub(r"^\{\s*\}", "", text, flags=DOTALL | MULTILINE)
return text
def append_semicolon(text: str) -> str:
"""
Append semicolons at the end of statements.
How it works:
1. Split the text into lines
2. Strip each line of leading and trailing whitespace
3. Append a semicolon to each line, do not append if:
- Line is empty
- Line already ends with a semicolon
- Line ends with a closing bracket
- Line ends with a closing parenthesis
- Line is an array or dictionary element
4. Join the lines back together
Args:
text (str): Text to append
Returns:
str: Formatted text
"""
lines = text.splitlines()
lines = [line.strip() for line in lines]
lines = [
line + ";"
if line
and not line.endswith(";")
and not line.startswith("[")
and not line.startswith("{")
and not line.endswith("(")
else line
for line in lines
]
return "\n".join(lines)
def remove_blank_lines(text: str) -> str:
"""
Remove multiple blank lines from the input text, ensuring only single blank lines remain.
Args:
text (str): Text to sanitize
Returns:
str: Sanitized text
"""
# Remove lines that contain only whitespace
lines = [line for line in text.splitlines() if line.strip() != ""]
# Join lines and replace multiple newlines with a single newline
text = "\n".join(lines)
text = sub(r"\n{2,}", "\n", text)
# Remove a leading newline if present
text = text.lstrip("\n")
return text
def resolve_imports(script_path: Union[str, Path]) -> str:
"""
Resolve imports in Groovy script, by replacing string statement for import (@**.groovy)
to script content
Args:
script_path (Union[str, Path]): Path to Groovy script
Returns:
str: Script with resolved imports
"""
script_path = Path(script_path)
with open(script_path, "r", encoding="utf8") as f:
script = f.read()
sli = script.splitlines(keepends=True)
for i, line in enumerate(sli):
if match(r"^@(.*\.groovy)", line):
import_path = Path(script_path).parent / match(
r"^@(.*\.groovy)", line
).group(1)
print(f" @ {script_path}:{i+1} <- {import_path.absolute()}")
sli[i] = resolve_imports(import_path)
return "".join(sli)
def remove_leading_whitespace(text: str) -> str:
"""
Remove leading whitespace from each line in the input text.
Args:
text (str): Text to sanitize
Returns:
str: Sanitized text
"""
return sub(r"^\s+", "", text, flags=MULTILINE)
def array_stringify(text: str) -> str:
"""
Convert multiline array into a single line of array, removing trailing commas.
FIX: This function is not perfect, it can't process nested arrays.
Args:
text (str): Text to sanitize
Returns:
str: Sanitized text
"""
# Step 1: Minify arrays by replacing newline characters inside brackets with spaces
# This captures nested arrays and dictionary-like structures in Groovy
text = sub(r"\[([^\[\]]*)\]", lambda m: m.group(0).replace("\n", ""), text)
# Step 2: Remove trailing commas before closing brackets for cleaner output
text = sub(r",\s*([\]\}])", r"\1", text)
return text
def clean_characters(text: str) -> str:
"""
Fix characters at weird places
Args:
text (str): Text to sanitize
Returns:
str: Sanitized text
"""
# fmt: off
replacements = [
(r",\s*;", ","), (r"{\s*;", "{"), (r";\s*}", "}"), (r"};\n{", "}\n{"),
(r";\s*$", ""), (r"\[\s*;", "["), (r";\s*\]", "]"), (r",\s+\)", ")"),
(r",\s*\]", "]"), (r"(\s)*", "\\1"), (r"->;", "->"), (r"->", "-> "),
(r"\r", ""), (r"\n", ""),
]
# fmt: on
for pattern, replacement in replacements:
text = sub(pattern, replacement, text)
return text
def obfuscate_variables(text: str) -> str:
"""
Obfuscate variables in the input text, replacing them with 1-3 character strings.
Args:
text (str): Text to sanitize
Returns:
str: Sanitized text
"""
variables = {}
# Find all variables in the text, excluding function definitions
# fmt: off
banned = [
"abr", "abstract", "ac", "acf", "aco", "af", "age", "all", "any", "ar",
"as", "az", "break", "case", "cf", "ci", "class", "continue", "ct",
"curl", "cy", "d", "db", "dc", "def", "di", "dt", "e", "else",
"encoding", "episode", "es", "ext", "f", "false", "final", "fn", "for",
"fps", "hd", "hdr", "hpi", "id", "if", "import", "instanceof", "it",
"khz", "metadata", "n", "native", "new", "none", "none", "null", "ny",
"package", "pc", "pi", "private", "protected", "public", "return", "s",
"sc", "scheme", "season", "source", "standalone", "static", "super",
"switch", "sxe", "sy", "t", "target", "this", "true", "value", "vbr",
"vc", "vcf", "vf", "void", "vs", "while", "ws", "xem", "XEM", "XML",
"y",
]
# fmt: on
pattens = [
r"\bdef\s([a-zA-Z_]\w*)\b", # def var
r"(?:,\s+|\[)([a-zA-Z_]\w*):", # array key, [key: value]
r"\{\s+([a-zA-Z_]\w*(?:,\s+[a-zA-Z_]\w*)*)\s*->", # map key, {key -> value}
]
for pattern in pattens:
for var in findall(pattern, text):
for key in var.split(", "):
if key not in variables:
obf = key_finder(banned, variables)
variables[key] = obf
print("Obfuscated variables:")
for var, obf in variables.items():
print(f" {var} -> {obf}{' (may be skipped)' if var in banned else ''}")
for var, obf in variables.items():
if var in banned:
continue
text = sub(rf"\b{var}\b", obf, text)
return text
def key_finder(banned_list: list[str], known_keys: dict[str, str]) -> str:
"""
Generate a unique key for a variable
Args:
banned_list (list[str]): List of banned keys
known_keys (dict[str, str]): Dictionary of known keys
Returns:
str: Unique key
"""
char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"
while True:
key = "".join(choice(char) for _ in range(1, choice([2, 3])+1))
if key not in banned_list and key not in known_keys.values():
break
return key
def dequoter(text: str) -> str:
"""Strip safe quotes from the text"""
lookup = [" ", "/", " - ", " [", "][", "]"]
for key in lookup:
fx = r'{"' + key + r'"}'
text = text.replace(fx, key)
return text
def reformat_array(text: str) -> str:
"""
Remove spaces between array elements, or between hash keys and values
DO NOT remove spaces in {".+"} outside of array or hash
"""
#
text = sub(r"(?<=\S),\s(?=\S|[^\"\}])", ",", text)
text = sub(r"(?<=\S):\s(?=\S)", ":", text)
return text
def main():
args = parse_args()
inp = Path(args.input)
out = Path(args.output)
print(f"Compiling {inp} to {out}")
script = resolve_imports(inp)
script = remove_comments(script)
script = append_semicolon(script)
script = remove_blank_lines(script)
script = array_stringify(remove_leading_whitespace(script))
script = clean_characters(script)
script = obfuscate_variables(script)
# script = reformat_array(script)
# script = dequoter(script)
out.parent.mkdir(parents=True, exist_ok=True)
out.write_text(script, encoding="utf8")
print(f'Done! Use "@{out.absolute()}" in FileBot\n')
if __name__ == "__main__":
main()