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

Contents of /eclass/python.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.19 - (show annotations) (download)
Wed Jul 6 20:20:04 2005 UTC (9 years, 10 months ago) by agriffis
Branch: MAIN
Changes since 1.18: +1 -2 lines
Remove ECLASS= and INHERITED= which haven't been necessary for a long time now

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

  ViewVC Help
Powered by ViewVC 1.1.20