fix msm-plugin.c svace issue: make sure dupPath is not NULL before strchr()
[platform/upstream/rpm.git] / scripts / brp-python-bytecompile
1 #!/bin/bash
2 errors_terminate=$2
3
4 # If using normal root, avoid changing anything.
5 if [ -z "$RPM_BUILD_ROOT" -o "$RPM_BUILD_ROOT" = "/" ]; then
6         exit 0
7 fi
8
9 # Figure out how deep we need to descend.  We could pick an insanely high
10 # number and hope it's enough, but somewhere, somebody's sure to run into it.
11 depth=`(find "$RPM_BUILD_ROOT" -type f -name "*.py" -print0 ; echo /) | \
12        xargs -0 -n 1 dirname | sed 's,[^/],,g' | sort -u | tail -n 1 | wc -c`
13 if [ -z "$depth" -o "$depth" -le "1" ]; then
14         exit 0
15 fi
16
17 function python_bytecompile()
18 {
19     local options=$1
20     local python_binary=$2
21     local exclude=$3
22     local python_libdir=$4
23     local depth=$5
24     local real_libdir=$6
25
26 cat << EOF | $python_binary $options
27 import compileall, sys, os, re
28
29 python_libdir = "$python_libdir"
30 depth = $depth
31 real_libdir = "$real_libdir"
32 build_root = "$RPM_BUILD_ROOT"
33 exclude = r"$exclude"
34
35 class Filter:
36     def search(self, path):
37         ret = not os.path.realpath(path).startswith(build_root)
38         if exclude:
39             ret = ret or re.search(exclude, path)
40         return ret
41
42 sys.exit(not compileall.compile_dir(python_libdir, depth, real_libdir, force=1, rx=Filter(), quiet=1))
43 EOF
44 }
45
46 # .pyc/.pyo files embed a "magic" value, identifying the ABI version of Python
47 # bytecode that they are for.
48 #
49 # The files below RPM_BUILD_ROOT could be targeting multiple versions of
50 # python (e.g. a single build that emits several subpackages e.g. a
51 # python26-foo subpackage, a python31-foo subpackage etc)
52 #
53 # Support this by assuming that below each /usr/lib/python$VERSION/, all
54 # .pyc/.pyo files are to be compiled for /usr/bin/python$VERSION.
55
56 # For example, below /usr/lib/python2.6/, we're targeting /usr/bin/python2.6
57 # and below /usr/lib/python3.1/, we're targeting /usr/bin/python3.1
58
59 shopt -s nullglob
60 for python_libdir in `find "$RPM_BUILD_ROOT" -type d|grep -E "/usr/lib(64)?/python[0-9]\.[0-9]$"`;
61 do
62         python_binary=/usr/bin/$(basename $python_libdir)
63         real_libdir=${python_libdir/$RPM_BUILD_ROOT/}
64         echo "Bytecompiling .py files below $python_libdir using $python_binary"
65
66         # Generate normal (.pyc) byte-compiled files.
67         python_bytecompile "" "$python_binary" "" "$python_libdir" "$depth" "$real_libdir"
68         if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
69                 # One or more of the files had a syntax error
70                 exit 1
71         fi
72
73         # Generate optimized (.pyo) byte-compiled files.
74         python_bytecompile "-O" "$python_binary" "" "$python_libdir" "$depth" "$real_libdir"
75         if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
76                 # One or more of the files had a syntax error
77                 exit 1
78         fi
79 done
80
81
82 # Handle other locations in the filesystem using the default python
83 # implementation - if we  have a default python interpreter
84
85 default_python=${1:-/usr/bin/python}
86 if [ ! -x "$default_python" ]; then
87         exit 0
88 fi
89
90 # Generate normal (.pyc) byte-compiled files.
91 python_bytecompile "" $default_python "/bin/|/sbin/|/usr/lib(64)?/python[0-9]\.[0-9]|/usr/share/doc" "$RPM_BUILD_ROOT" "$depth" "/"
92 if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
93         # One or more of the files had a syntax error
94         exit 1
95 fi
96
97 # Generate optimized (.pyo) byte-compiled files.
98 python_bytecompile "-O" $default_python "/bin/|/sbin/|/usr/lib(64)?/python[0-9]\.[0-9]|/usr/share/doc" "$RPM_BUILD_ROOT" "$depth" "/"
99 if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
100         # One or more of the files had a syntax error
101         exit 1
102 fi
103 exit 0