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

Contents of /eclass/python.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.19 - (hide annotations) (download)
Wed Jul 6 20:20:04 2005 UTC (8 years, 9 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 vapier 1.14 # Copyright 1999-2004 Gentoo Foundation
2 liquidx 1.1 # Distributed under the terms of the GNU General Public License v2
3 agriffis 1.19 # $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.18 2005/04/23 10:29:51 liquidx Exp $
4 liquidx 1.1 #
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 eradicator 1.15 # python_mod_cleanup() - Goes through /usr/lib*/python* to remove
17 liquidx 1.1 # orphaned *.pyc *.pyo
18     # python_makesym() - Makes /usr/bin/python symlinks
19    
20 pythonhead 1.7 inherit alternatives
21 liquidx 1.1
22     INHERITED="$INHERITED $ECLASS"
23 liquidx 1.2
24 liquidx 1.18
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 liquidx 1.5 #
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 liquidx 1.2 python_disable_pyc() {
49 liquidx 1.3 export PYTHON_DONTCOMPILE=1
50 liquidx 1.2 }
51 liquidx 1.1
52 liquidx 1.4 python_enable_pyc() {
53     unset PYTHON_DONTCOMPILE
54     }
55    
56     python_disable_pyc
57    
58 liquidx 1.1 #
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 liquidx 1.18
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 liquidx 1.1 python_version() {
73     local tmpstr
74     python=${python:-/usr/bin/python}
75     tmpstr="$(${python} -V 2>&1 )"
76     export PYVER_ALL="${tmpstr#Python }"
77 liquidx 1.18 __python_version_extract $PYVER_ALL
78 liquidx 1.1 }
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 liquidx 1.10 alternatives_auto_makesym "/usr/bin/python" "python[0-9].[0-9]"
87     alternatives_auto_makesym "/usr/bin/python2" "python2.[0-9]"
88 liquidx 1.1 }
89    
90     #
91     # name: python_tkinter_exists
92 liquidx 1.16 # desc: run without arguments, checks if python was compiled with Tkinter
93     # support. If not, prints an error message and dies.
94 liquidx 1.1 #
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 pythonhead 1.7 # desc: run with the module name as an argument. it will check if a
107 liquidx 1.1 # 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 pythonhead 1.7 # exam:
111 liquidx 1.1 # if python_mod_exists gtk; then
112     # echo "gtk support enabled
113     # fi
114     #
115     python_mod_exists() {
116 liquidx 1.16 [ -z "$1" ] && die "${FUNCTION} requires an argument!"
117 pythonhead 1.7 if ! python -c "import $1" >/dev/null 2>&1; then
118 liquidx 1.1 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 liquidx 1.5 # allow compiling for older python versions
132     if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then
133     PYVER=${PYTHON_OVERRIDE_PYVER}
134 pythonhead 1.7 else
135 liquidx 1.5 python_version
136     fi
137 pythonhead 1.7
138 liquidx 1.1 if [ -f "$1" ]; then
139 liquidx 1.5 python${PYVER} -c "import py_compile; py_compile.compile('${1}')" || \
140 liquidx 1.1 ewarn "Failed to compile ${1}"
141 liquidx 1.5 python${PYVER} -O -c "import py_compile; py_compile.compile('${1}')" || \
142 pythonhead 1.7 ewarn "Failed to compile ${1}"
143 liquidx 1.1 else
144     ewarn "Unable to find ${1}"
145 pythonhead 1.7 fi
146 liquidx 1.1 }
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 pythonhead 1.7 #
154 liquidx 1.1 # 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 liquidx 1.13 local myroot
161     # strip trailing slash
162 liquidx 1.16 myroot="${ROOT%/}"
163 liquidx 1.13
164 liquidx 1.5 # allow compiling for older python versions
165     if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then
166     PYVER=${PYTHON_OVERRIDE_PYVER}
167 pythonhead 1.7 else
168 liquidx 1.5 python_version
169     fi
170 pythonhead 1.7
171 liquidx 1.5 # set opts
172     if [ "${PYVER}" = "2.2" ]; then
173     compileopts=""
174     else
175     compileopts="-q"
176     fi
177 pythonhead 1.7
178 liquidx 1.10 ebegin "Byte compiling python modules for python-${PYVER} .."
179 liquidx 1.13 python${PYVER} ${myroot}/usr/lib/python${PYVER}/compileall.py ${compileopts} $@
180     python${PYVER} -O ${myroot}/usr/lib/python${PYVER}/compileall.py ${compileopts} $@
181 liquidx 1.5 eend $?
182 liquidx 1.1 }
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 pythonhead 1.7 #
189 liquidx 1.1 # 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 liquidx 1.13 local SEARCH_PATH myroot
195 liquidx 1.16
196 liquidx 1.13 # strip trailing slash
197 liquidx 1.16 myroot="${ROOT%/}"
198 liquidx 1.1
199 liquidx 1.5 if [ $# -gt 0 ]; then
200     for path in $@; do
201 liquidx 1.13 SEARCH_PATH="${SEARCH_PATH} ${myroot}/${path#/}"
202 pythonhead 1.7 done
203 liquidx 1.5 else
204 eradicator 1.15 for path in ${myroot}/usr/lib*/python*/site-packages; do
205 liquidx 1.5 SEARCH_PATH="${SEARCH_PATH} ${path}"
206     done
207     fi
208 pythonhead 1.7
209 liquidx 1.1 for path in ${SEARCH_PATH}; do
210 liquidx 1.11 einfo "Cleaning orphaned Python bytecode from ${path} .."
211 pythonhead 1.7 for obj in $(find ${path} -name *.pyc); do
212 liquidx 1.16 src_py="${obj%c}"
213 liquidx 1.1 if [ ! -f "${src_py}" ]; then
214     einfo "Purging ${src_py}[co]"
215     rm -f ${src_py}[co]
216     fi
217 liquidx 1.12 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 liquidx 1.1 done
222 pythonhead 1.7 done
223 liquidx 1.1 }

  ViewVC Help
Powered by ViewVC 1.1.20