-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpython_shebangs
executable file
·49 lines (44 loc) · 1.49 KB
/
python_shebangs
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
#!/bin/sh
# This is not run by default because - regardless of what
# certain other distributions may think - an unversioned
# python shebang being defined as "give me the latest
# and greatest python available" is very much allowed.
#
# It is provided because it makes sense to have versioned
# shebangs on projects not compatible with a new version.
if [ -z "$RPM_BUILD_ROOT" ]; then
printf '%s\n' "No build root defined" >&2
exit 1
fi
if [ ! -d "$RPM_BUILD_ROOT" ]; then
printf '%s\n' "Invalid build root" >&2
exit 1
fi
set -efu
# We fork a subshell, so working with a variable is not possible,
# otherwise we will have to switch to bashisms and require /dev/fd/*
tmpfile="$(mktemp)"
trap 'rm -f "$tmpfile"' EXIT
printf '%s\n' "0" > "$tmpfile"
for dir in /bin /usr/bin /sbin /usr/sbin /usr/libexec; do
if [ ! -d "${RPM_BUILD_ROOT}/${dir}" ]; then
continue
fi
find "${RPM_BUILD_ROOT}/${dir}" -type f -print | \
while read -r file; do
# Prohibit shebangs like:
# #!/usr/bin/python
# #! /usr/bin/python
# #!/usr/bin/env python
# #! /usr/bin/env python
# Note that a space may be in the end of the shebang line
if head -n 1 "$file" | grep -qE '^#!([[:blank:]])*.*(/python|[[:blank:]]python)([[:blank:]])*$' ; then
printf '%s\n' "Unversioned Python shebangs are not allowed. Specify python3 or python2 explicitly in $file"
# Do not fail on the 1st error, print all errors at one time
printf '%s\n' "1" > "$tmpfile"
fi
done
done
if [ "$(head -n 1 "$tmpfile")" != 0 ]; then
exit 1
fi