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

Contents of /eclass/python.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (hide annotations) (download)
Thu Oct 9 15:11:24 2003 UTC (10 years, 10 months ago) by liquidx
Branch: MAIN
Changes since 1.3: +10 -4 lines
make PYTHON_DONTCOMPILE default for the moment

1 liquidx 1.1 # Copyright 1999-2003 Gentoo Technologies, Inc.
2     # Distributed under the terms of the GNU General Public License v2
3 liquidx 1.4 # $Header: /home/cvsroot/gentoo-x86/eclass/python.eclass,v 1.3 2003/10/09 08:57:20 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     # 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 liquidx 1.2
25     python_disable_pyc() {
26 liquidx 1.3 export PYTHON_DONTCOMPILE=1
27 liquidx 1.2 }
28 liquidx 1.1
29 liquidx 1.4 python_enable_pyc() {
30     unset PYTHON_DONTCOMPILE
31     }
32    
33     python_disable_pyc
34    
35 liquidx 1.1 #
36     # name: python_version
37     # desc: run without arguments and it will export the version of python
38     # currently in use as $PYVER
39     #
40     python_version() {
41     local tmpstr
42     python=${python:-/usr/bin/python}
43     tmpstr="$(${python} -V 2>&1 )"
44     export PYVER_ALL="${tmpstr#Python }"
45    
46     export PYVER_MAJOR=$(echo ${PYVER_ALL} | cut -d. -f1)
47     export PYVER_MINOR=$(echo ${PYVER_ALL} | cut -d. -f2)
48     export PYVER_MICRO=$(echo ${PYVER_ALL} | cut -d. -f3-)
49     export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
50     }
51    
52     #
53     # name: python_makesym
54     # desc: run without arguments, it will create the /usr/bin/python symlinks
55     # to the latest installed version
56     #
57     python_makesym() {
58     alternatives_auto_makesym "/usr/bin/python" "/usr/bin/python[0-9].[0-9]"
59     alternatives_auto_makesym "/usr/bin/python2" "/usr/bin/python[0-9].[0-9]"
60     }
61    
62     #
63     # name: python_tkinter_exists
64     # desc: run without arguments, it will return TRUE(0) if python is compiled
65     # with tkinter or FALSE(1) if python is compiled without tkinter.
66     #
67     python_tkinter_exists() {
68     if ! python -c "import Tkinter" >/dev/null 2>&1; then
69     eerror "You need to recompile python with Tkinter support."
70     eerror "That means: USE='tcltk' emerge python"
71     echo
72     die "missing tkinter support with installed python"
73     fi
74     }
75    
76     #
77     # name: python_mod_exists
78     # desc: run with the module name as an argument. it will check if a
79     # python module is installed and loadable. it will return
80     # TRUE(0) if the module exists, and FALSE(1) if the module does
81     # not exist.
82     # exam:
83     # if python_mod_exists gtk; then
84     # echo "gtk support enabled
85     # fi
86     #
87     python_mod_exists() {
88     if ! python -c "import $1" >/dev/null 2>&1; then
89     return 1
90     fi
91     return 0
92     }
93    
94     #
95     # name: python_mod_compile
96     # desc: given a filename, it will pre-compile the module's .pyc and .pyo.
97     # should only be run in pkg_postinst()
98     # exam:
99     # python_mod_compile ${ROOT}usr/lib/python2.3/site-packages/pygoogle.py
100     #
101     python_mod_compile() {
102     if [ -f "$1" ]; then
103     python -c "import py_compile; py_compile.compile('${1}')" || \
104     ewarn "Failed to compile ${1}"
105     python -O -c "import py_compile; py_compile.compile('${1}')" || \
106     ewarn "Failed to compile ${1}"
107     else
108     ewarn "Unable to find ${1}"
109     fi
110     }
111    
112     #
113     # name: python_mod_optimize
114     # desc: if no arguments supplied, it will recompile all modules under
115     # sys.path (eg. /usr/lib/python2.3, /usr/lib/python2.3/site-packages/ ..)
116     # no recursively
117     #
118     # if supplied with arguments, it will recompile all modules recursively
119     # in the supplied directory
120     # exam:
121     # python_mod_optimize ${ROOT}usr/share/codegen
122     #
123     python_mod_optimize() {
124     python_version
125 liquidx 1.4 einfo "Byte Compiling Python modules for ${PYVER} .."
126     python ${ROOT}usr/lib/python${PYVER}/compileall.py -q $@
127     python -O ${ROOT}usr/lib/python${PYVER}/compileall.py -q $@
128 liquidx 1.1 }
129    
130     #
131     # name: python_mod_cleanup
132     # desc: run with optional arguments, where arguments are directories of
133     # python modules. if none given, it will look in /usr/lib/python[0-9].[0-9]
134     #
135     # it will recursively scan all compiled python modules in the directories
136     # and determine if they are orphaned (eg. their corresponding .py is missing.)
137     # if they are, then it will remove their corresponding .pyc and .pyo
138     #
139     python_mod_cleanup() {
140     local SEARCH_PATH
141    
142     for path in $@; do
143     SEARCH_PATH="${SEARCH_PATH} ${ROOT}${dir}"
144     done
145    
146     for path in ${ROOT}usr/lib/python*/site-packages; do
147     SEARCH_PATH="${SEARCH_PATH} ${path}"
148     done
149    
150     for path in ${SEARCH_PATH}; do
151     einfo "Searching ${path} .."
152     for obj in $(find ${path} -name *.pyc); do
153     src_py="$(echo $obj | sed 's:c$::')"
154     if [ ! -f "${src_py}" ]; then
155     einfo "Purging ${src_py}[co]"
156     rm -f ${src_py}[co]
157     fi
158     done
159     done
160     }

  ViewVC Help
Powered by ViewVC 1.1.20