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

Contents of /eclass/python.eclass

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20