0fe7b8e
# Note that the path could itself be a python file, or a directory
0fe7b8e
0fe7b8e
# Python's compile_all module only works on directories, and requires a max
0fe7b8e
# recursion depth
0fe7b8e
0fe7b8e
# Note that the py_byte_compile macro should work for all Python versions
0fe7b8e
# Which unfortunately makes the definition more complicated than it should be
0fe7b8e
0fe7b8e
%py_byte_compile()\
0fe7b8e
py2_byte_compile () {\
0fe7b8e
    python_binary="%1"\
0fe7b8e
    bytecode_compilation_path="%2"\
0fe7b8e
    find $bytecode_compilation_path -type f -a -name "*.py" -print0 | xargs -0 $python_binary -c 'import py_compile, sys; [py_compile.compile(f, dfile=f.partition("$RPM_BUILD_ROOT")[2]) for f in sys.argv[1:]]' || :\
0fe7b8e
    find $bytecode_compilation_path -type f -a -name "*.py" -print0 | xargs -0 $python_binary -O -c 'import py_compile, sys; [py_compile.compile(f, dfile=f.partition("$RPM_BUILD_ROOT")[2]) for f in sys.argv[1:]]' || :\
0fe7b8e
}\
0fe7b8e
\
0fe7b8e
py3_byte_compile () {\
0fe7b8e
    python_binary="%1"\
0fe7b8e
    bytecode_compilation_path="%2"\
0fe7b8e
    find $bytecode_compilation_path -type f -a -name "*.py" -print0 | xargs -0 $python_binary -O -c 'import py_compile, sys; [py_compile.compile(f, dfile=f.partition("$RPM_BUILD_ROOT")[2], optimize=opt) for opt in range(2) for f in sys.argv[1:]]' || :\
0fe7b8e
}\
0fe7b8e
\
0fe7b8e
# Get version without a dot (36 instead of 3.6), bash doesn't compare floats well \
0fe7b8e
python_version=$(%1 -c "import sys; sys.stdout.write('{0.major}{0.minor}'.format(sys.version_info))") \
0fe7b8e
# The bytecompilation syntax has changed between Python 3.4 and Python 3.5, so for 3.4 and earlier we use the "Python 2" syntax \
0fe7b8e
[ "$python_version" -ge 35 ] && py3_byte_compile "%1" "%2" || py2_byte_compile "%1" "%2" \
0fe7b8e
%{nil}