-
Notifications
You must be signed in to change notification settings - Fork 1
/
minify.py
executable file
·47 lines (39 loc) · 1.15 KB
/
minify.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
#!/usr/bin/env python3.10
deps = ("python-minifier==2.7.0", "cowsay==5.0")
import runfromvenv
import python_minifier
import base64
import zlib
import textwrap
def minify_all(x):
return python_minifier.minify(
x,
None,
remove_annotations=True,
remove_pass=True,
remove_literal_statements=True,
combine_imports=True,
hoist_literals=True,
rename_locals=True,
rename_globals=True,
remove_object_base=True,
convert_posargs_to_args=True,
)
with open(f"runfromvenv.py", "rb") as f:
code = f.read()
print(f"init: {len(code)}")
mini = minify_all(code)
print(f"mini: {len(f'exec({repr(mini)})')}")
print(textwrap.indent(mini, " "))
SUBST_CHAR = "\\n"
assert not (SUBST_CHAR.encode("ascii") in code)
code = mini.replace("\n", "\\n").replace('"', '\\"')
code = f'exec("{code}")' # .replace("{SUBST_CHAR}","\\n"))'
print(code)
# code = zlib.compress(code, 9)
# code = f"__import__('zlib').decompress({code})".encode("ascii")
# code = f"exec(bytes.fromhex('{code.hex()}'))"
print(f"after: {len(code)}")
exec(code)
with open("minified.py", "w") as f:
f.write(code)