]> git.ipfire.org Git - pakfire.git/commitdiff
python: Byte-compile all files in sitelib.
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 2 Oct 2012 22:45:11 +0000 (00:45 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 2 Oct 2012 22:45:11 +0000 (00:45 +0200)
For files which are located elsewhere, it is required
to call:
  python_bytecompile <path1> <path2>... (Python 2)
or
  python3_bytecompile <path1> <path2>... (Python 3)

macros/build.macro
macros/python.macro
tools/py-compile

index 698706806ab7ef0ce4ceaf184573b379d6164ed0..4e4d830bf7cef0bd6ef2969c9f519edc715170df 100644 (file)
@@ -46,7 +46,17 @@ def MACRO_INSTALL_LOGROTATE_FILES
 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
index 1b417f2696e6ae026445362956ced01c25cd3641..15c1209eceded2ef65093fcc4ee643fae0514c6b 100644 (file)
@@ -1,7 +1,9 @@
 # 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())")
@@ -9,6 +11,7 @@ python3_version  = %(%{python3} -c "import sys; sys.stdout.write(sys.version[:3]
 
 # 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()")
index 66c33edfe787e189965e07417d1eaff8d5e22892..5ccd8f9e6198f0c4e06d19182bd7597061ec3dc1 100755 (executable)
@@ -1,48 +1,58 @@
 #!/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