end
def MACRO_PYTHON_COMPILE
- # XXX TODO
+ if [ -x "%{python3}" ]; then
+ %{python3_bytecompile} \
+ %{BUILDROOT}%{python3_sitearch} \
+ %{BUILDROOT}%{python3_sitelib}
+ fi
+
+ if [ -x "%{python}" ]; then
+ %{python_bytecompile} \
+ %{BUILDROOT}%{python_sitearch} \
+ %{BUILDROOT}%{python_sitelib}
+ fi
end
MACRO_PERL_CLEANUP
# A bunch of predefined things for Python.
+python_bytecompile_script = /usr/lib/pakfire/py-compile
# Python 3 constants.
python3 = /usr/bin/python3
+python3_bytecompile = %{python_bytecompile_script} --python=%{python3}
python3_sitearch = %(%{python3} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(1))")
python3_sitelib = %(%{python3} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")
# Python 2 constants.
python = /usr/bin/python2
+python_bytecompile = %{python_bytecompile_script} --python=%{python}
python_sitearch = %(%{python} -c "from distutils.sysconfig import get_python_lib; print get_python_lib(1)")
python_sitelib = %(%{python} -c "from distutils.sysconfig import get_python_lib; print get_python_lib()")
#!/bin/sh
-PYTHON=$(which python 2>/dev/null)
+python_interpreter="python"
+paths=""
+
+while [ $# -gt 0 ]; do
+ case "${1}" in
+ --python=*)
+ python_interpreter=${1#--python=}
+ ;;
+ *)
+ paths="${paths} ${1}"
+ ;;
+ esac
+ shift
+done
-if [ -z "${PYTHON}" ]; then
- # Python is not present. Fail silently.
- exit 0
+if [ -z "${paths}" ]; then
+ echo >&2 "No path specified!"
+ exit 1
fi
-files=""
-for i in $*; do
- if [ -e ${i}c ] && [ -e ${i}o ]; then
- continue # all files we want are already there
- fi
- files="$files $i"
-done
+if [ "${python_interpreter:0:1}" != "/" ]; then
+ python_interpreter=$(which ${python_interpreter} 2>/dev/null)
+fi
-if [ -z "${files}" ]; then
- # No files need to be proceeded.
- exit 0
+if [ ! -x "${python_interpreter}" ]; then
+ echo >&2 "Python interpreter is not executable: ${python_interpreter}"
+ exit 1
fi
-$PYTHON -c "
-import sys, os, string, py_compile
-
-files = '''$files'''
-print 'Byte-compiling python modules...'
-for file in string.split(files):
- if not os.path.exists(file) or not (len(file) >= 3 and file[-3:] == '.py'):
- continue
- print file,
- sys.stdout.flush()
- py_compile.compile(file)
-print" || exit $?
-
-# this will fail for python < 1.5, but that doesn't matter ...
-$PYTHON -O -c "
-import sys, os, string, py_compile
-
-files = '''$files'''
-print 'Byte-compiling python modules (optimised versions) ...'
-for file in string.split(files):
- if not os.path.exists(file) or not (len(file) >= 3 and file[-3:] == '.py'):
- continue
- print file,
- sys.stdout.flush()
- py_compile.compile(file)
-print" 2>/dev/null || :
+tempfile=$(mktemp)
+trap "rm -f ${tempfile}" EXIT
+
+cat > ${tempfile} <<EOF
+import py_compile
+import sys
+
+for file in sys.argv[1:]:
+ py_compile.compile(file, doraise=0)
+EOF
+
+filelist=$(find ${paths} -type f -a -name "*.py")
+
+# Compile with optimization.
+${python_interpreter} -O ${tempfile} ${filelist}
+
+# Compile without optimization.
+${python_interpreter} ${tempfile} ${filelist}
+
+# Hardlink identical files.
+for pyc in $(find ${paths} -type f -a -name "*.pyc"); do
+ pyo="${pyc/.pyc/.pyo}"
+
+ if cmp -s "${pyc}" "${pyo}"; then
+ ln -f "${pyc}" "${pyo}"
+ fi
+done