]> git.ipfire.org Git - pakfire.git/blame - src/scripts/py-compile
Merge branch 'master' of ssh://git.ipfire.org/pub/git/pakfire
[pakfire.git] / src / scripts / py-compile
CommitLineData
6566e5f2
MT
1#!/bin/sh
2
e9e430d4
MT
3python_interpreter="python"
4paths=""
5
6while [ $# -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
16done
6566e5f2 17
e9e430d4
MT
18if [ -z "${paths}" ]; then
19 echo >&2 "No path specified!"
20 exit 1
6566e5f2
MT
21fi
22
e9e430d4
MT
23if [ "${python_interpreter:0:1}" != "/" ]; then
24 python_interpreter=$(which ${python_interpreter} 2>/dev/null)
25fi
6566e5f2 26
e9e430d4
MT
27if [ ! -x "${python_interpreter}" ]; then
28 echo >&2 "Python interpreter is not executable: ${python_interpreter}"
29 exit 1
6566e5f2
MT
30fi
31
e9e430d4
MT
32tempfile=$(mktemp)
33trap "rm -f ${tempfile}" EXIT
34
35cat > ${tempfile} <<EOF
61918bfa 36import os
e9e430d4
MT
37import py_compile
38import sys
39
40for file in sys.argv[1:]:
61918bfa
MT
41 if not os.path.exists(file):
42 continue
43
e9e430d4
MT
44 py_compile.compile(file, doraise=0)
45EOF
46
61918bfa 47filelist=$(find ${paths} -type f -a -name "*.py" 2>/dev/null)
e9e430d4
MT
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.
61918bfa 56for pyc in $(find ${paths} -type f -a -name "*.pyc" 2>/dev/null); do
e9e430d4
MT
57 pyo="${pyc/.pyc/.pyo}"
58
59 if cmp -s "${pyc}" "${pyo}"; then
60 ln -f "${pyc}" "${pyo}"
61 fi
62done