This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 144
/
lint-packages.sh
executable file
·400 lines (334 loc) · 9.39 KB
/
lint-packages.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/usr/bin/env bash
##
## Script for performing basic sanity checks of package's build.sh.
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
set -e -u
REPO_DIR=$(realpath "$(dirname "$0")/../")
PACKAGES_DIR="$REPO_DIR/packages"
lint_package() {
local package_script
local package_name
package_script=$1
package_name=$(basename "$(dirname "$package_script")")
echo "================================================================"
echo
echo "Package: $package_name"
echo
echo -n "Syntax check: "
local syntax_errors
syntax_errors=$(bash -n "$package_script" 2>&1)
if [ -n "$syntax_errors" ]; then
echo "FAILED"
echo
echo "$syntax_errors"
echo
return 1
else
echo "PASS"
fi
echo
# Fields checking is done in subshell since we will source build.sh.
(set +e +u
local pkg_lint_error
# Certain fields may be API-specific.
# Using API 24 here.
TERMUX_PKG_API_LEVEL=24
if [ -f "$REPO_DIR/scripts/properties.sh" ]; then
. "$REPO_DIR/scripts/properties.sh"
fi
. "$package_script"
pkg_lint_error=false
echo -n "TERMUX_PKG_HOMEPAGE: "
if [ -n "$TERMUX_PKG_HOMEPAGE" ]; then
if ! grep -qP '^https://.+' <<< "$TERMUX_PKG_HOMEPAGE"; then
echo "NON-HTTPS (acceptable)"
else
echo "PASS"
fi
else
echo "NOT SET"
pkg_lint_error=true
fi
echo -n "TERMUX_PKG_DESCRIPTION: "
if [ -n "$TERMUX_PKG_DESCRIPTION" ]; then
str_length=$(($(wc -c <<< "$TERMUX_PKG_DESCRIPTION") - 1))
if [ $str_length -gt 100 ]; then
echo "TOO LONG (allowed: 100 characters max)"
else
echo "PASS"
fi
unset str_length
else
echo "NOT SET"
pkg_lint_error=true
fi
echo -n "TERMUX_PKG_LICENSE: "
if [ -n "$TERMUX_PKG_LICENSE" ]; then
echo "PASS"
else
echo "NOT SET"
pkg_lint_error=true
fi
if [ -n "$TERMUX_PKG_API_LEVEL" ]; then
echo -n "TERMUX_PKG_API_LEVEL: "
if grep -qP '^[1-9][0-9]$' <<< "$TERMUX_PKG_API_LEVEL"; then
if [ $TERMUX_PKG_API_LEVEL -lt 24 ] || [ $TERMUX_PKG_API_LEVEL -gt 28 ]; then
echo "INVALID (allowed: number in range 24 - 28)"
pkg_lint_error=true
else
echo "PASS"
fi
else
echo "INVALID (allowed: number in range 24 - 28)"
pkg_lint_error=true
fi
fi
echo -n "TERMUX_PKG_VERSION: "
if [ -n "$TERMUX_PKG_VERSION" ]; then
if grep -qiP '^[0-9][0-9a-z+\-\.]*' <<< "$TERMUX_PKG_VERSION"; then
echo "PASS"
else
echo "INVALID (contains characters that are not allowed)"
fi
else
echo "NOT SET"
pkg_lint_error=true
fi
if [ -n "$TERMUX_PKG_REVISION" ]; then
echo -n "TERMUX_PKG_REVISION: "
if grep -qP '^[1-9](\d{1,8})?$' <<< "$TERMUX_PKG_REVISION"; then
echo "PASS"
else
echo "INVALID (allowed: number in range 1 - 999999999)"
pkg_lint_error=true
fi
fi
if [ -n "$TERMUX_PKG_SKIP_SRC_EXTRACT" ]; then
echo -n "TERMUX_PKG_SKIP_SRC_EXTRACT: "
if [ "$TERMUX_PKG_SKIP_SRC_EXTRACT" = "true" ] || [ "$TERMUX_PKG_SKIP_SRC_EXTRACT" = "false" ]; then
echo "PASS"
else
echo "INVALID (allowed: true / false)"
pkg_lint_error=true
fi
fi
if [ -n "$TERMUX_PKG_SRCURL" ]; then
echo -n "TERMUX_PKG_SRCURL: "
urls_ok=true
for url in "${TERMUX_PKG_SRCURL[@]}"; do
if [ -n "$url" ]; then
if ! grep -qP '^https://.+' <<< "$url"; then
echo "NON-HTTPS (acceptable)"
urls_ok=false
break
fi
else
echo "NOT SET (one of the array elements)"
urls_ok=false
pkg_lint_error=true
break
fi
done
unset url
if $urls_ok; then
echo "PASS"
fi
unset urls_ok
echo -n "TERMUX_PKG_SHA256: "
if [ -n "$TERMUX_PKG_SHA256" ]; then
if [ "${#TERMUX_PKG_SRCURL[@]}" -eq "${#TERMUX_PKG_SHA256[@]}" ]; then
sha256_ok=true
for sha256 in "${TERMUX_PKG_SHA256[@]}"; do
if ! grep -qP '^[0-9a-fA-F]{64}$' <<< "${sha256}" && [ "$sha256" != "SKIP_CHECKSUM" ]; then
echo "MALFORMED (SHA-256 should contain 64 hexadecimal numbers)"
sha256_ok=false
pkg_lint_error=true
break
fi
done
unset sha256
if $sha256_ok; then
echo "PASS"
fi
unset sha256_ok
else
echo "LENGTHS OF 'TERMUX_PKG_SRCURL' AND 'TERMUX_PKG_SHA256' ARE NOT EQUAL"
pkg_lint_error=true
fi
elif [ "${TERMUX_PKG_SRCURL: -4}" == ".git" ]; then
echo "NOT SET (acceptable since TERMUX_PKG_SRCURL is git repo)"
else
echo "NOT SET"
pkg_lint_error=true
fi
else
if [ "$TERMUX_PKG_SKIP_SRC_EXTRACT" != "true" ] && ! declare -F termux_step_extract_package > /dev/null 2>&1; then
echo "TERMUX_PKG_SRCURL: NOT SET (set TERMUX_PKG_SKIP_SRC_EXTRACT to 'true' if no sources downloaded)"
pkg_lint_error=true
fi
fi
if [ -n "$TERMUX_PKG_METAPACKAGE" ]; then
echo -n "TERMUX_PKG_METAPACKAGE: "
if [ "$TERMUX_PKG_METAPACKAGE" = "true" ] || [ "$TERMUX_PKG_METAPACKAGE" = "false" ]; then
echo "PASS"
else
echo "INVALID (allowed: true / false)"
pkg_lint_error=true
fi
fi
if [ -n "$TERMUX_PKG_ESSENTIAL" ]; then
echo "TERMUX_PKG_ESSENTIAL: DISALLOWED FOR X11 PACKAGES"
pkg_lint_error=true
#if [ "$TERMUX_PKG_ESSENTIAL" = "true" ] || [ "$TERMUX_PKG_ESSENTIAL" = "false" ]; then
# echo "PASS"
#else
# echo "INVALID (allowed: true / false)"
# pkg_lint_error=true
#fi
fi
if [ -n "$TERMUX_PKG_NO_STATICSPLIT" ]; then
echo -n "TERMUX_PKG_NO_STATICSPLIT: "
if [ "$TERMUX_PKG_NO_STATICSPLIT" = "true" ] || [ "$TERMUX_PKG_NO_STATICSPLIT" = "false" ]; then
echo "PASS"
else
echo "INVALID (allowed: true / false)"
pkg_lint_error=true
fi
fi
if [ -n "$TERMUX_PKG_BUILD_IN_SRC" ]; then
echo -n "TERMUX_PKG_BUILD_IN_SRC: "
if [ "$TERMUX_PKG_BUILD_IN_SRC" = "true" ] || [ "$TERMUX_PKG_BUILD_IN_SRC" = "false" ]; then
echo "PASS"
else
echo "INVALID (allowed: true / false)"
pkg_lint_error=true
fi
fi
if [ -n "$TERMUX_PKG_HAS_DEBUG" ]; then
echo -n "TERMUX_PKG_HAS_DEBUG: "
if [ "$TERMUX_PKG_HAS_DEBUG" = "true" ] || [ "$TERMUX_PKG_HAS_DEBUG" = "false" ]; then
echo "PASS"
else
echo "INVALID (allowed: true / false)"
pkg_lint_error=true
fi
fi
if [ -n "$TERMUX_PKG_PLATFORM_INDEPENDENT" ]; then
echo -n "TERMUX_PKG_PLATFORM_INDEPENDENT: "
if [ "$TERMUX_PKG_PLATFORM_INDEPENDENT" = "true" ] || [ "$TERMUX_PKG_PLATFORM_INDEPENDENT" = "false" ]; then
echo "PASS"
else
echo "INVALID (allowed: true / false)"
pkg_lint_error=true
fi
fi
if [ -n "$TERMUX_PKG_HOSTBUILD" ]; then
echo -n "TERMUX_PKG_HOSTBUILD: "
if [ "$TERMUX_PKG_HOSTBUILD" = "true" ] || [ "$TERMUX_PKG_HOSTBUILD" = "false" ]; then
echo "PASS"
else
echo "INVALID (allowed: true / false)"
pkg_lint_error=true
fi
fi
if [ -n "$TERMUX_PKG_FORCE_CMAKE" ]; then
echo -n "TERMUX_PKG_FORCE_CMAKE: "
if [ "$TERMUX_PKG_FORCE_CMAKE" = "true" ] || [ "$TERMUX_PKG_FORCE_CMAKE" = "false" ]; then
echo "PASS"
else
echo "INVALID (allowed: true / false)"
pkg_lint_error=true
fi
fi
if [ -n "$TERMUX_PKG_RM_AFTER_INSTALL" ]; then
echo -n "TERMUX_PKG_RM_AFTER_INSTALL: "
file_path_ok=true
while read -r file_path; do
[ -z "$file_path" ] && continue
if grep -qP '^(\.\.)?/' <<< "$file_path"; then
echo "INVALID (file path should be relative to prefix)"
file_path_ok=false
pkg_lint_error=true
break
fi
done <<< "$TERMUX_PKG_RM_AFTER_INSTALL"
unset file_path
if $file_path_ok; then
echo "PASS"
fi
unset file_path_ok
fi
if [ -n "$TERMUX_PKG_CONFFILES" ]; then
echo -n "TERMUX_PKG_CONFFILES: "
file_path_ok=true
while read -r file_path; do
[ -z "$file_path" ] && continue
if grep -qP '^(\.\.)?/' <<< "$file_path"; then
echo "INVALID (file path should be relative to prefix)"
file_path_ok=false
pkg_lint_error=true
break
fi
done <<< "$TERMUX_PKG_CONFFILES"
unset file_path
if $file_path_ok; then
echo "PASS"
fi
unset file_path_ok
fi
if $pkg_lint_error; then
exit 1
else
exit 0
fi
)
local ret=$?
echo
return "$ret"
}
linter_main() {
local package_counter=0
local problems_found=false
local package_script
for package_script in "$@"; do
if ! lint_package "$package_script"; then
problems_found=true
break
fi
package_counter=$((package_counter + 1))
done
if $problems_found; then
echo "================================================================"
echo
echo "A problem has been found in '$(realpath --relative-to="$REPO_DIR" "$package_script")'."
echo "Checked $package_counter packages before the first error was detected."
echo
echo "================================================================"
return 1
else
echo "================================================================"
echo
echo "Checked $package_counter packages."
echo "Everything seems ok."
echo
echo "================================================================"
fi
return 0
}
if [ $# -eq 0 ]; then
linter_main "$PACKAGES_DIR"/*/build.sh || exit 1
else
linter_main "$@" || exit 1
fi