]> git.ipfire.org Git - people/stevee/pakfire.git/blob - src/scripts/py-compile
Cleanup database and add indexes.
[people/stevee/pakfire.git] / src / scripts / py-compile
1 #!/bin/sh
2
3 python_interpreter="python"
4 paths=""
5
6 while [ $# -gt 0 ]; do
7 case "${1}" in
8 --python=*)
9 python_interpreter=${1#--python=}
10 ;;
11 *)
12 paths="${paths} ${1}"
13 ;;
14 esac
15 shift
16 done
17
18 if [ -z "${paths}" ]; then
19 echo >&2 "No path specified!"
20 exit 1
21 fi
22
23 if [ "${python_interpreter:0:1}" != "/" ]; then
24 python_interpreter=$(which ${python_interpreter} 2>/dev/null)
25 fi
26
27 if [ ! -x "${python_interpreter}" ]; then
28 echo >&2 "Python interpreter is not executable: ${python_interpreter}"
29 exit 1
30 fi
31
32 tempfile=$(mktemp)
33 trap "rm -f ${tempfile}" EXIT
34
35 cat > ${tempfile} <<EOF
36 import os
37 import py_compile
38 import sys
39
40 for file in sys.argv[1:]:
41 if not os.path.exists(file):
42 continue
43
44 py_compile.compile(file, doraise=0)
45 EOF
46
47 filelist=$(find ${paths} -type f -a -name "*.py" 2>/dev/null)
48
49 # Compile with optimization.
50 ${python_interpreter} -O ${tempfile} ${filelist}
51
52 # Compile without optimization.
53 ${python_interpreter} ${tempfile} ${filelist}
54
55 # Hardlink identical files.
56 for pyc in $(find ${paths} -type f -a -name "*.pyc" 2>/dev/null); do
57 pyo="${pyc/.pyc/.pyo}"
58
59 if cmp -s "${pyc}" "${pyo}"; then
60 ln -f "${pyc}" "${pyo}"
61 fi
62 done