Modify eu-strip option to perform strip in post script of rpm package & add option...
[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 # If we don't have a python interpreter, avoid changing anything.
10 default_python=${1:-/usr/bin/python}
11 if [ ! -x "$default_python" ]; then
12         exit 0
13 fi
14
15 # Figure out how deep we need to descend.  We could pick an insanely high
16 # number and hope it's enough, but somewhere, somebody's sure to run into it.
17 depth=`(find "$RPM_BUILD_ROOT" -type f -name "*.py" -print0 ; echo /) | \
18        xargs -0 -n 1 dirname | sed 's,[^/],,g' | sort -u | tail -n 1 | wc -c`
19 if [ -z "$depth" -o "$depth" -le "1" ]; then
20         exit 0
21 fi
22
23 # .pyc/.pyo files embed a "magic" value, identifying the ABI version of Python
24 # bytecode that they are for.
25 #
26 # The files below RPM_BUILD_ROOT could be targetting multiple versions of
27 # python (e.g. a single build that emits several subpackages e.g. a
28 # python26-foo subpackage, a python31-foo subpackage etc)
29 #
30 # Support this by assuming that below each /usr/lib/python$VERSION/, all
31 # .pyc/.pyo files are to be compiled for /usr/bin/python$VERSION.
32
33 # For example, below /usr/lib/python2.6/, we're targetting /usr/bin/python2.6
34 # and below /usr/lib/python3.1/, we're targetting /usr/bin/python3.1
35
36 shopt -s nullglob
37 for python_libdir in "$RPM_BUILD_ROOT"/usr/lib{,64}/python[0-9].[0-9]/ ;
38 do
39         python_binary=/usr/bin/$(basename $python_libdir)
40         real_libdir=${python_libdir/$RPM_BUILD_ROOT/}
41         echo "Bytecompiling .py files below $python_libdir using $python_binary"
42
43         # Generate normal (.pyc) byte-compiled files.
44         $python_binary -c 'import compileall, sys; sys.exit(not compileall.compile_dir("'"$python_libdir"'", '"$depth"', "'"$real_libdir"'", force=1, quiet=1))'
45         if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
46                 # One or more of the files had a syntax error
47                 exit 1
48         fi
49
50         # Generate optimized (.pyo) byte-compiled files.
51         $python_binary -O -c 'import compileall, sys; sys.exit(not compileall.compile_dir("'"$python_libdir"'", '"$depth"', "'"$real_libdir"'", force=1, quiet=1))'
52         if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
53                 # One or more of the files had a syntax error
54                 exit 1
55         fi
56 done
57
58
59 # Handle other locations in the filesystem using the default python
60 # implementation:
61
62 # Generate normal (.pyc) byte-compiled files.
63 $default_python -c 'import compileall, re, sys; sys.exit (not compileall.compile_dir("'"$RPM_BUILD_ROOT"'", '"$depth"', "/", 1, re.compile(r"'"/bin/|/sbin/|/usr/lib(64)?/python[0-9]\.[0-9]"'"), quiet=1))'
64 if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
65         # One or more of the files had a syntax error
66         exit 1
67 fi
68
69 # Generate optimized (.pyo) byte-compiled files.
70 $default_python -O -c 'import compileall, re, sys; sys.exit(not compileall.compile_dir("'"$RPM_BUILD_ROOT"'", '"$depth"', "/", 1, re.compile(r"'"/bin/|/sbin/|/usr/lib(64)?/python[0-9]\.[0-9]"'"), quiet=1))' > /dev/null
71 if [ $? -ne 0 -a 0$errors_terminate -ne 0 ]; then
72         # One or more of the files had a syntax error
73         exit 1
74 fi
75 exit 0