-
Notifications
You must be signed in to change notification settings - Fork 25
/
opstring-extractor.sh
executable file
·299 lines (240 loc) · 6.65 KB
/
opstring-extractor.sh
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
#!/bin/bash
BRANCH="cm-13.0"
RES="https://github.com/CyanogenMod/android_packages_apps_Settings/raw/$BRANCH/res/"
TMP=$(mktemp -t aoxXXXXXX)
PATTERNS=(
's/_modify_/_write_/g'
's/_wake_up/_alarm_wakeup/g'
's/_mute_unmute/_mute/g'
's/_sms_db/_sms/g'
's/_keep_awake/_wake_lock/g'
's/_auto_start/_boot_completed/g'
's/_audio_focus/_take_audio_focus/g'
's/_notification"/_post_notification"/g'
's/_notification_toast/_post_notification/g'
's/_draw_on_top/_system_alert_window/g'
's/_media_buttons/_take_media_buttons/g'
's/_use_body_sensors/_body_sensors/g'
's/_data_change/_data_connect_change/g'
's/_cell_scan/_neighboring_cells/g'
#'s/_make_call/_call_phone/g'
's/_superuser/_su/g'
's/_start_at_boot/_boot_completed/g'
's/_mobile_data/_data_connect/g'
's/_toggle_(.*?)"/_$1_change"/g'
's/_(labels|summaries)_(.*?)_volume/_$1_audio_$2_volume/g'
)
readarray() {
builtin readarray -t "$1" &> /dev/null || read -r -a "$1"
}
grep() {
if command -v ggrep &> /dev/null; then
ggrep "$@"
else
command grep "$@"
fi
}
copy() {
[[ -f "$1" ]] && cp "$1" "$2"
}
download() {
if [[ ! -z $OPSTRING_DOWNLOADS ]]; then
if copy "$OPSTRING_DOWNLOADS/$1" "$2"; then
echo -n "*"
else
# Maybe we have a matching file in another
# directory of the same language?
dir=$(dirname "$1")
lang=${dir:7:2}
base=$(basename "$1")
if [[ ${dir:10:1} == "r" ]]; then
# We have a regional locale; try the main language
if copy "$OPSTRING_DOWNLOADS/values-$lang/$base" "$2"; then
echo -n "($lang)*"
return 0
fi
else
# We have a language without a region specified;
# try the regional locales (if any).
files=( "$OPSTRING_DOWNLOADS/values-${lang}"-r??/$base "(skip)")
valdir=$(basename "$(dirname "${files[0]}")")
count=${#files[@]}
if [[ $count -gt 0 && ${valdir:11} != "??" ]]; then
if [[ $count -gt 1 ]]; then
echo
echo "$lang: select a $base file"
select f in "${files[@]}"; do
[[ $f == "(skip)" ]] && echo -n "$lang: DL!" && return 0
locale=$(basename "$f")
locale=${locale:7}
if copy "$f" "$2"; then
echo -n "$lang: DL*"
return 0
else
echo "$lang: copy failed ($f/$base -> $2)"
fi
done
else
if copy "${files[0]}" "$2"; then
echo -n "(${valdir:7})*"
fi
fi
fi
fi
echo -n "!"
return 1
fi
return 0
fi
if wget -O "$2" -q "$RES/$1"; then
echo -n "*"
else
rm -f "$2"
echo -n "!"
return 1
fi
}
extract_lang() {
rm -f "$TMP".*
echo -n "$1: "
echo -n "DL"
download "values-$1/strings.xml" "$TMP.dl2"
if ! download "values-$1/cm_strings.xml" "$TMP.dl1"; then
use_arrays=1
else
if ! grep -qP 'app_ops_(summaries|labels)_' "$TMP.dl1" &> /dev/null; then
use_arrays=1
else
use_arrays=
fi
fi
if [[ -z $use_arrays ]]; then
echo -n " "
grep -P "app_ops_(summaries|labels)_" "$TMP.dl1" > "$TMP.ops"
echo -n "GREP* SED"
for p in ${PATTERNS[@]}; do
perl -p -i -e "$p" "$TMP.ops"
done
echo -n "* "
echo " <!-- Categories -->" >> "$TMP.ops"
grep "app_ops_categories_" "$TMP.dl1" >> "$TMP.ops"
else
if download "values-$1/arrays.xml" "$TMP.arr"; then
echo -n " SYNTH"
awk "/app_ops_summaries/,/\/string-array/" < "$TMP.arr" | grep -v string-array > "$TMP.sum"
awk '/app_ops_labels/,/\/string-array/' < "$TMP.arr" | grep -v string-array > "$TMP.lab"
awk '/app_ops_categories/,/\/string-array/' < "$TMP.arr" | grep -v string-array > "$TMP.cat"
perl -p -i -e 's/<.+?>//g' "$TMP".{sum,lab,cat}
perl -p -i -e 's/^\s+//g' "$TMP".{sum,lab,cat}
readarray summaries < "$TMP.sum"
readarray labels < "$TMP.lab"
readarray categories < "$TMP.cat"
size=${#summaries[@]}
# The old app_ops_{summaries_labels} arrays contain 47 entries; the last two are
# for OP_PROJECT_MEDIA and OP_ACTIVATE_VPN. The one before that should have been
# OP_TOAST_WINDOW, but it's missing. This means that the last two array indices
# must be shifted to be correct.
if [[ $size -ne 47 || $size -ne ${#labels[@]} ]]; then
echo "($size/${#labels[@]})!"
return 1
fi
ops=$(grep -oP 'app_ops_summaries_(\w+)' res/values/ops.xml | sed -e 's/app_ops_summaries_//')
i=0
for op in $ops; do
if [[ $op == "toast_window" ]]; then
let i=$i-1
s=
l=
elif [[ $i -lt $size ]]; then
s=${summaries[$i]}
l=${labels[$i]}
else
s=
l=
fi
if [[ ! -z "$s" ]]; then
echo " <string name=\"app_ops_summaries_$op\">$s</string>" >> "$TMP.ops"
else
echo " <!-- app_ops_summaries_$op missing -->" >> "$TMP.ops"
fi
if [[ ! -z "$l" ]]; then
echo " <string name=\"app_ops_labels_$op\">$l</string>" >> "$TMP.ops"
else
echo " <!-- app_ops_labels_$op missing -->" >> "$TMP.ops"
fi
let i=$i+1
done
echo -n "* "
else
echo -n " "
fi
echo " <!-- Categories -->" >> "$TMP.ops"
cats=$(grep -oP 'app_ops_categories_(\w+)' res/values/ops.xml)
i=0
for c in $cats; do
s=${categories[$i]}
if [[ ! -z "$s" ]]; then
echo " <string name=\"$c\">$s</string>" >> "$TMP.ops"
else
echo " <!-- $c missing -->" >> "$TMP.ops"
fi
let i=$i+1
done
fi
if [[ ! -f "$TMP.dl2" ]]; then
echo
return 1
fi
echo -n "FINISH"
echo " <!-- Misc -->" >> "$TMP.ops"
grep -P '"version_text"|"app_ops_' "$TMP.dl2" >> "$TMP.ops"
perl -p -i -e 's/msgid=".*?">/>/g' "$TMP.ops"
perl -p -i -e 's/<\/?xliff:.+?>//g' "$TMP.ops"
out="res/values-$1/extracted.xml"
echo > $out '<?xml version="1.0" encoding="utf-8"?>'
echo >> $out "<!-- Auto-generated by opstring-extractor on $(date +%Y-%m-%d) from branch $BRANCH -->"
cat >> $out <<EOF
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ExtraTranslation">
<!-- Labels and summaries -->
EOF
cat >> $out "$TMP.ops"
perl -p -i -e "s/^\s+/ /g" "$out"
echo "</resources>" >> $out
echo "*"
}
cd "$(dirname "$0")"
if [[ "$1" == "download" ]]; then
if [[ -z "$2" ]]; then
echo >&2 "usage: $0 download [target folder]"
exit 1
fi
mkdir -p "$2" || exit 1
unset OPSTRING_DOWNLOADS
fi
if [[ $# -eq 0 || "$1" == "download" ]]; then
for dir in res/values-??{,-r??}; do
lang=$(basename "$dir")
lang=${lang:7}
if [[ -z "$1" ]]; then
if [[ "${lang:3:1}" == "r" && -e "res/values-${lang:0:2}/extracted.xml" ]]; then
echo "$lang: SKIPPED"
else
extract_lang "$lang"
fi
else
base=$(basename "$dir")
d="$2/$base"
mkdir -p "$d" || exit 1
cd "$d"
echo -n "$lang: DL"
download "$base/strings.xml" "strings.xml"
download "$base/cm_strings.xml" "cm_strings.xml"
download "$base/arrays.xml" "arrays.xml"
echo
cd - &> /dev/null
fi
done
else
extract_lang "$1"
fi
rm -f "$TMP $TMP.*"