/[gentoo-x86]/eclass/python.eclass
Gentoo

Contents of /eclass/python.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.18 - (show annotations) (download)
Sat Apr 23 10:29:51 2005 UTC (9 years, 7 months ago) by liquidx
Branch: MAIN
Changes since 1.17: +27 -5 lines
fix python version extraction and add version extract test to be sure

1 # Copyright 1999-2004 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.17 2005/04/23 03:40:10 pythonhead Exp $
4 #
5 # Author: Alastair Tse <liquidx@gentoo.org>
6 #
7 # A Utility Eclass that should be inherited by anything that deals with
8 # Python or Python modules.
9 #
10 # - Features:
11 # python_version() - sets PYVER/PYVER_MAJOR/PYVER_MINOR
12 # python_tkinter_exists() - Checks for tkinter support in python
13 # python_mod_exists() - Checks if a python module exists
14 # python_mod_compile() - Compiles a .py file to a .pyc/.pyo
15 # python_mod_optimize() - Generates .pyc/.pyo precompiled scripts
16 # python_mod_cleanup() - Goes through /usr/lib*/python* to remove
17 # orphaned *.pyc *.pyo
18 # python_makesym() - Makes /usr/bin/python symlinks
19
20 inherit alternatives
21
22 ECLASS="python"
23 INHERITED="$INHERITED $ECLASS"
24
25
26 __python_eclass_test() {
27 __python_version_extract 2.3
28 echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
29 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
30 __python_version_extract 2.3.4
31 echo -n "2.3.4 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
32 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
33 __python_version_extract 2.3.5
34 echo -n "2.3.5 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
35 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
36 __python_version_extract 2.4
37 echo -n "2.4 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
38 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
39 }
40
41 #
42 # name: python_disable/enable_pyc
43 # desc: tells python not to automatically recompile modules to .pyc/.pyo
44 # even if the timestamps/version stamps don't match. this is
45 # done to protect sandbox.
46 #
47 # note: supported by >=dev-lang/python-2.2.3-r3 only.
48 #
49 python_disable_pyc() {
50 export PYTHON_DONTCOMPILE=1
51 }
52
53 python_enable_pyc() {
54 unset PYTHON_DONTCOMPILE
55 }
56
57 python_disable_pyc
58
59 #
60 # name: python_version
61 # desc: run without arguments and it will export the version of python
62 # currently in use as $PYVER
63 #
64
65 __python_version_extract() {
66 verstr=$1
67 export PYVER_MAJOR=${verstr:0:1}
68 export PYVER_MINOR=${verstr:2:1}
69 export PYVER_MICRO=${verstr:4}
70 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
71 }
72
73 python_version() {
74 local tmpstr
75 python=${python:-/usr/bin/python}
76 tmpstr="$(${python} -V 2>&1 )"
77 export PYVER_ALL="${tmpstr#Python }"
78 __python_version_extract $PYVER_ALL
79 }
80
81 #
82 # name: python_makesym
83 # desc: run without arguments, it will create the /usr/bin/python symlinks
84 # to the latest installed version
85 #
86 python_makesym() {
87 alternatives_auto_makesym "/usr/bin/python" "python[0-9].[0-9]"
88 alternatives_auto_makesym "/usr/bin/python2" "python2.[0-9]"
89 }
90
91 #
92 # name: python_tkinter_exists
93 # desc: run without arguments, checks if python was compiled with Tkinter
94 # support. If not, prints an error message and dies.
95 #
96 python_tkinter_exists() {
97 if ! python -c "import Tkinter" >/dev/null 2>&1; then
98 eerror "You need to recompile python with Tkinter support."
99 eerror "That means: USE='tcltk' emerge python"
100 echo
101 die "missing tkinter support with installed python"
102 fi
103 }
104
105 #
106 # name: python_mod_exists
107 # desc: run with the module name as an argument. it will check if a
108 # python module is installed and loadable. it will return
109 # TRUE(0) if the module exists, and FALSE(1) if the module does
110 # not exist.
111 # exam:
112 # if python_mod_exists gtk; then
113 # echo "gtk support enabled
114 # fi
115 #
116 python_mod_exists() {
117 [ -z "$1" ] && die "${FUNCTION} requires an argument!"
118 if ! python -c "import $1" >/dev/null 2>&1; then
119 return 1
120 fi
121 return 0
122 }
123
124 #
125 # name: python_mod_compile
126 # desc: given a filename, it will pre-compile the module's .pyc and .pyo.
127 # should only be run in pkg_postinst()
128 # exam:
129 # python_mod_compile ${ROOT}usr/lib/python2.3/site-packages/pygoogle.py
130 #
131 python_mod_compile() {
132 # allow compiling for older python versions
133 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then
134 PYVER=${PYTHON_OVERRIDE_PYVER}
135 else
136 python_version
137 fi
138
139 if [ -f "$1" ]; then
140 python${PYVER} -c "import py_compile; py_compile.compile('${1}')" || \
141 ewarn "Failed to compile ${1}"
142 python${PYVER} -O -c "import py_compile; py_compile.compile('${1}')" || \
143 ewarn "Failed to compile ${1}"
144 else
145 ewarn "Unable to find ${1}"
146 fi
147 }
148
149 #
150 # name: python_mod_optimize
151 # desc: if no arguments supplied, it will recompile all modules under
152 # sys.path (eg. /usr/lib/python2.3, /usr/lib/python2.3/site-packages/ ..)
153 # no recursively
154 #
155 # if supplied with arguments, it will recompile all modules recursively
156 # in the supplied directory
157 # exam:
158 # python_mod_optimize ${ROOT}usr/share/codegen
159 #
160 python_mod_optimize() {
161 local myroot
162 # strip trailing slash
163 myroot="${ROOT%/}"
164
165 # allow compiling for older python versions
166 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then
167 PYVER=${PYTHON_OVERRIDE_PYVER}
168 else
169 python_version
170 fi
171
172 # set opts
173 if [ "${PYVER}" = "2.2" ]; then
174 compileopts=""
175 else
176 compileopts="-q"
177 fi
178
179 ebegin "Byte compiling python modules for python-${PYVER} .."
180 python${PYVER} ${myroot}/usr/lib/python${PYVER}/compileall.py ${compileopts} $@
181 python${PYVER} -O ${myroot}/usr/lib/python${PYVER}/compileall.py ${compileopts} $@
182 eend $?
183 }
184
185 #
186 # name: python_mod_cleanup
187 # desc: run with optional arguments, where arguments are directories of
188 # python modules. if none given, it will look in /usr/lib/python[0-9].[0-9]
189 #
190 # it will recursively scan all compiled python modules in the directories
191 # and determine if they are orphaned (eg. their corresponding .py is missing.)
192 # if they are, then it will remove their corresponding .pyc and .pyo
193 #
194 python_mod_cleanup() {
195 local SEARCH_PATH myroot
196
197 # strip trailing slash
198 myroot="${ROOT%/}"
199
200 if [ $# -gt 0 ]; then
201 for path in $@; do
202 SEARCH_PATH="${SEARCH_PATH} ${myroot}/${path#/}"
203 done
204 else
205 for path in ${myroot}/usr/lib*/python*/site-packages; do
206 SEARCH_PATH="${SEARCH_PATH} ${path}"
207 done
208 fi
209
210 for path in ${SEARCH_PATH}; do
211 einfo "Cleaning orphaned Python bytecode from ${path} .."
212 for obj in $(find ${path} -name *.pyc); do
213 src_py="${obj%c}"
214 if [ ! -f "${src_py}" ]; then
215 einfo "Purging ${src_py}[co]"
216 rm -f ${src_py}[co]
217 fi
218 done
219 # attempt to remove directories that maybe empty
220 for dir in $(find ${path} -type d | sort -r); do
221 rmdir ${dir} 2>/dev/null
222 done
223 done
224 }

  ViewVC Help
Powered by ViewVC 1.1.20