]>
Commit | Line | Data |
---|---|---|
11d34126 MT |
1 | From e9e430d4fd5b09327f57651726ffac1b7874ae01 Mon Sep 17 00:00:00 2001 |
2 | From: Michael Tremer <michael.tremer@ipfire.org> | |
3 | Date: Wed, 3 Oct 2012 00:45:11 +0200 | |
4 | Subject: [PATCH 6/6] python: Byte-compile all files in sitelib. | |
5 | ||
6 | For files which are located elsewhere, it is required | |
7 | to call: | |
8 | python_bytecompile <path1> <path2>... (Python 2) | |
9 | or | |
10 | python3_bytecompile <path1> <path2>... (Python 3) | |
11 | --- | |
12 | macros/build.macro | 12 ++++++- | |
13 | macros/python.macro | 3 ++ | |
14 | tools/py-compile | 90 +++++++++++++++++++++++++++++------------------------ | |
15 | 3 files changed, 64 insertions(+), 41 deletions(-) | |
16 | ||
17 | diff --git a/macros/build.macro b/macros/build.macro | |
18 | index 6987068..4e4d830 100644 | |
19 | --- a/macros/build.macro | |
20 | +++ b/macros/build.macro | |
21 | @@ -46,7 +46,17 @@ def MACRO_INSTALL_LOGROTATE_FILES | |
22 | end | |
23 | ||
24 | def MACRO_PYTHON_COMPILE | |
25 | - # XXX TODO | |
26 | + if [ -x "%{python3}" ]; then | |
27 | + %{python3_bytecompile} \ | |
28 | + %{BUILDROOT}%{python3_sitearch} \ | |
29 | + %{BUILDROOT}%{python3_sitelib} | |
30 | + fi | |
31 | + | |
32 | + if [ -x "%{python}" ]; then | |
33 | + %{python_bytecompile} \ | |
34 | + %{BUILDROOT}%{python_sitearch} \ | |
35 | + %{BUILDROOT}%{python_sitelib} | |
36 | + fi | |
37 | end | |
38 | ||
39 | MACRO_PERL_CLEANUP | |
40 | diff --git a/macros/python.macro b/macros/python.macro | |
41 | index 1b417f2..15c1209 100644 | |
42 | --- a/macros/python.macro | |
43 | +++ b/macros/python.macro | |
44 | @@ -1,7 +1,9 @@ | |
45 | # A bunch of predefined things for Python. | |
46 | +python_bytecompile_script = /usr/lib/pakfire/py-compile | |
47 | ||
48 | # Python 3 constants. | |
49 | python3 = /usr/bin/python3 | |
50 | +python3_bytecompile = %{python_bytecompile_script} --python=%{python3} | |
51 | ||
52 | python3_sitearch = %(%{python3} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(1))") | |
53 | python3_sitelib = %(%{python3} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") | |
54 | @@ -9,6 +11,7 @@ python3_version = %(%{python3} -c "import sys; sys.stdout.write(sys.version[:3] | |
55 | ||
56 | # Python 2 constants. | |
57 | python = /usr/bin/python2 | |
58 | +python_bytecompile = %{python_bytecompile_script} --python=%{python} | |
59 | ||
60 | python_sitearch = %(%{python} -c "from distutils.sysconfig import get_python_lib; print get_python_lib(1)") | |
61 | python_sitelib = %(%{python} -c "from distutils.sysconfig import get_python_lib; print get_python_lib()") | |
62 | diff --git a/tools/py-compile b/tools/py-compile | |
63 | index 66c33ed..5ccd8f9 100755 | |
64 | --- a/tools/py-compile | |
65 | +++ b/tools/py-compile | |
66 | @@ -1,48 +1,58 @@ | |
67 | #!/bin/sh | |
68 | ||
69 | -PYTHON=$(which python 2>/dev/null) | |
70 | +python_interpreter="python" | |
71 | +paths="" | |
72 | + | |
73 | +while [ $# -gt 0 ]; do | |
74 | + case "${1}" in | |
75 | + --python=*) | |
76 | + python_interpreter=${1#--python=} | |
77 | + ;; | |
78 | + *) | |
79 | + paths="${paths} ${1}" | |
80 | + ;; | |
81 | + esac | |
82 | + shift | |
83 | +done | |
84 | ||
85 | -if [ -z "${PYTHON}" ]; then | |
86 | - # Python is not present. Fail silently. | |
87 | - exit 0 | |
88 | +if [ -z "${paths}" ]; then | |
89 | + echo >&2 "No path specified!" | |
90 | + exit 1 | |
91 | fi | |
92 | ||
93 | -files="" | |
94 | -for i in $*; do | |
95 | - if [ -e ${i}c ] && [ -e ${i}o ]; then | |
96 | - continue # all files we want are already there | |
97 | - fi | |
98 | - files="$files $i" | |
99 | -done | |
100 | +if [ "${python_interpreter:0:1}" != "/" ]; then | |
101 | + python_interpreter=$(which ${python_interpreter} 2>/dev/null) | |
102 | +fi | |
103 | ||
104 | -if [ -z "${files}" ]; then | |
105 | - # No files need to be proceeded. | |
106 | - exit 0 | |
107 | +if [ ! -x "${python_interpreter}" ]; then | |
108 | + echo >&2 "Python interpreter is not executable: ${python_interpreter}" | |
109 | + exit 1 | |
110 | fi | |
111 | ||
112 | -$PYTHON -c " | |
113 | -import sys, os, string, py_compile | |
114 | - | |
115 | -files = '''$files''' | |
116 | -print 'Byte-compiling python modules...' | |
117 | -for file in string.split(files): | |
118 | - if not os.path.exists(file) or not (len(file) >= 3 and file[-3:] == '.py'): | |
119 | - continue | |
120 | - print file, | |
121 | - sys.stdout.flush() | |
122 | - py_compile.compile(file) | |
123 | -print" || exit $? | |
124 | - | |
125 | -# this will fail for python < 1.5, but that doesn't matter ... | |
126 | -$PYTHON -O -c " | |
127 | -import sys, os, string, py_compile | |
128 | - | |
129 | -files = '''$files''' | |
130 | -print 'Byte-compiling python modules (optimised versions) ...' | |
131 | -for file in string.split(files): | |
132 | - if not os.path.exists(file) or not (len(file) >= 3 and file[-3:] == '.py'): | |
133 | - continue | |
134 | - print file, | |
135 | - sys.stdout.flush() | |
136 | - py_compile.compile(file) | |
137 | -print" 2>/dev/null || : | |
138 | +tempfile=$(mktemp) | |
139 | +trap "rm -f ${tempfile}" EXIT | |
140 | + | |
141 | +cat > ${tempfile} <<EOF | |
142 | +import py_compile | |
143 | +import sys | |
144 | + | |
145 | +for file in sys.argv[1:]: | |
146 | + py_compile.compile(file, doraise=0) | |
147 | +EOF | |
148 | + | |
149 | +filelist=$(find ${paths} -type f -a -name "*.py") | |
150 | + | |
151 | +# Compile with optimization. | |
152 | +${python_interpreter} -O ${tempfile} ${filelist} | |
153 | + | |
154 | +# Compile without optimization. | |
155 | +${python_interpreter} ${tempfile} ${filelist} | |
156 | + | |
157 | +# Hardlink identical files. | |
158 | +for pyc in $(find ${paths} -type f -a -name "*.pyc"); do | |
159 | + pyo="${pyc/.pyc/.pyo}" | |
160 | + | |
161 | + if cmp -s "${pyc}" "${pyo}"; then | |
162 | + ln -f "${pyc}" "${pyo}" | |
163 | + fi | |
164 | +done | |
165 | -- | |
166 | 1.7.11.4 | |
167 |