-
Notifications
You must be signed in to change notification settings - Fork 4
/
inplace-fix.py
executable file
·201 lines (174 loc) · 7.3 KB
/
inplace-fix.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
#!/usr/bin/python3
# encoding: utf-8
# SPDX-FileCopyrightText: 2024 FC (Fay) Stegerman <[email protected]>
# SPDX-License-Identifier: AGPL-3.0-or-later
import argparse
import os
import shutil
import subprocess
import sys
import tempfile
from typing import Optional, Tuple
COMMANDS = (
"fix-compresslevel",
"fix-files",
"fix-newlines",
"fix-pg-map-id",
"rm-files",
"sort-apk",
"sort-baseline",
)
BUILD_TOOLS_WITH_BROKEN_ZIPALIGN = ("31.0.0", "32.0.0")
BUILD_TOOLS_WITH_PAGE_SIZE_FROM = "35.0.0-rc1"
SDK_ENV = ("ANDROID_HOME", "ANDROID_SDK", "ANDROID_SDK_ROOT")
def _zipalign_cmd(page_align: bool, page_size: Optional[int]) -> Tuple[str, ...]:
if page_align:
if page_size is not None:
return ("zipalign", "-P", str(page_size), "4")
return ("zipalign", "-p", "4")
return ("zipalign", "4")
ZIPALIGN = _zipalign_cmd(page_align=False, page_size=None)
ZIPALIGN_P = _zipalign_cmd(page_align=True, page_size=None)
class Error(RuntimeError):
pass
def inplace_fix(command: str, input_file: str, *args: str,
zipalign: bool = False, page_align: bool = False,
page_size: Optional[int] = None, internal: bool = False) -> None:
if command not in COMMANDS:
raise Error(f"Unknown command {command}")
exe, script = _script_cmd(command)
ext = os.path.splitext(input_file)[1]
with tempfile.TemporaryDirectory() as tdir:
fixed = os.path.join(tdir, "fixed" + ext)
run_command(exe, script, input_file, fixed, *args, trim=2)
if zipalign:
aligned = os.path.join(tdir, "aligned" + ext)
zac = zipalign_cmd(page_align=page_align, page_size=page_size, internal=internal)
run_command(*zac, fixed, aligned, trim=2)
print(f"[MOVE] {aligned} to {input_file}")
shutil.move(aligned, input_file)
else:
print(f"[MOVE] {fixed} to {input_file}")
shutil.move(fixed, input_file)
def zipalign_cmd(page_align: bool = False, page_size: Optional[int] = None,
internal: bool = False) -> Tuple[str, ...]:
"""
Find zipalign command using $PATH or $ANDROID_HOME etc.
>>> zipalign_cmd()
('zipalign', '4')
>>> zipalign_cmd(page_align=True)
('zipalign', '-p', '4')
>>> zipalign_cmd(page_align=True, page_size=16)
('zipalign', '-P', '16', '4')
>>> cmd = zipalign_cmd(page_align=True, page_size=16, internal=True)
>>> [x.split("/")[-1] for x in cmd]
['python3', 'zipalign.py', '-P', '16', '4']
>>> os.environ["PATH"] = ""
>>> for k in SDK_ENV:
... os.environ[k] = ""
>>> cmd = zipalign_cmd()
>>> [x.split("/")[-1] for x in cmd]
['python3', 'zipalign.py', '4']
>>> os.environ["ANDROID_HOME"] = "test/fake-sdk"
>>> zipalign_cmd()
[SKIP BROKEN] 31.0.0
[FOUND] test/fake-sdk/build-tools/30.0.3/zipalign
('test/fake-sdk/build-tools/30.0.3/zipalign', '4')
>>> cmd = zipalign_cmd(page_align=True, page_size=16)
[SKIP TOO OLD] 31.0.0
[SKIP TOO OLD] 30.0.3
[SKIP TOO OLD] 26.0.2
>>> [x.split("/")[-1] for x in cmd]
['python3', 'zipalign.py', '-P', '16', '4']
>>> os.environ["ANDROID_HOME"] = "test/fake-sdk-2"
>>> zipalign_cmd(page_align=True, page_size=16)
[FOUND] test/fake-sdk-2/build-tools/35.0.0-rc1/zipalign
('test/fake-sdk-2/build-tools/35.0.0-rc1/zipalign', '-P', '16', '4')
"""
cmd, *args = _zipalign_cmd(page_align, page_size)
if not internal:
if shutil.which(cmd):
return (cmd, *args)
for k in SDK_ENV:
if home := os.environ.get(k):
tools = os.path.join(home, "build-tools")
if os.path.exists(tools):
for vsn in sorted(os.listdir(tools), key=_vsn, reverse=True):
if page_size and _vsn(vsn) < _vsn(BUILD_TOOLS_WITH_PAGE_SIZE_FROM):
print(f"[SKIP TOO OLD] {vsn}")
continue
for s in BUILD_TOOLS_WITH_BROKEN_ZIPALIGN:
if vsn.startswith(s):
print(f"[SKIP BROKEN] {vsn}")
break
else:
c = os.path.join(tools, vsn, cmd)
if shutil.which(c):
print(f"[FOUND] {c}")
return (c, *args)
return (*_script_cmd(cmd), *args)
def _vsn(v: str) -> Tuple[int, ...]:
"""
>>> vs = "31.0.0 32.1.0-rc1 34.0.0-rc3 34.0.0 35.0.0-rc1".split()
>>> for v in sorted(vs, key=_vsn, reverse=True):
... (_vsn(v), v)
((35, 0, 0, 0, 1), '35.0.0-rc1')
((34, 0, 0, 1, 0), '34.0.0')
((34, 0, 0, 0, 3), '34.0.0-rc3')
((32, 1, 0, 0, 1), '32.1.0-rc1')
((31, 0, 0, 1, 0), '31.0.0')
"""
if "-rc" in v:
v = v.replace("-rc", ".0.", 1)
else:
v = v + ".1.0"
return tuple(int(x) if x.isdigit() else -1 for x in v.split("."))
def _script_cmd(command: str) -> Tuple[str, str]:
script_dir = os.path.dirname(__file__)
for cmd in (command, command.replace("-", "_")):
script = os.path.join(script_dir, cmd + ".py")
if os.path.exists(script):
break
else:
raise Error(f"Script for {command} not found")
exe = sys.executable or "python3"
return exe, script
def run_command(*args: str, trim: int = 1) -> None:
targs = tuple(os.path.basename(a) for a in args[:trim]) + args[trim:]
print(f"[RUN] {' '.join(targs)}")
try:
subprocess.run(args, check=True)
except subprocess.CalledProcessError as e:
raise Error(f"{args[0]} command failed") from e
except FileNotFoundError as e:
raise Error(f"{args[0]} command not found") from e
def main() -> None:
prog = os.path.basename(sys.argv[0])
usage = (f"{prog} [-h] [--zipalign] [--page-align] [--page-size N] [--internal]\n"
f"{len('usage: ' + prog) * ' '} COMMAND INPUT_FILE [...]")
epilog = f"Commands: {', '.join(COMMANDS)}."
parser = argparse.ArgumentParser(usage=usage, epilog=epilog)
parser.add_argument("--zipalign", action="store_true",
help="run zipalign after COMMAND")
parser.add_argument("--page-align", action="store_true",
help="run zipalign w/ -p option (implies --zipalign)")
parser.add_argument("--page-size", metavar="N", type=int,
help="run zipalign w/ -P N option (implies --page-align)")
parser.add_argument("--internal", action="store_true",
help="use zipalign.py instead of searching $PATH/$ANDROID_HOME/etc.")
parser.add_argument("command", metavar="COMMAND")
parser.add_argument("input_file", metavar="INPUT_FILE")
args, rest = parser.parse_known_args()
palign = bool(args.page_align or args.page_size)
zalign = bool(args.zipalign or palign)
try:
if args.internal and not zalign:
raise Error("Conflicting options: --internal without --zipalign/--page-align/--page-size")
inplace_fix(args.command, args.input_file, *rest, zipalign=zalign, page_align=palign,
page_size=args.page_size, internal=args.internal)
except Error as e:
print(f"Error: {e}.", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()
# vim: set tw=80 sw=4 sts=4 et fdm=marker :