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

Contents of /eclass/python.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations) (download)
Wed Oct 8 14:07:38 2003 UTC (11 years, 1 month ago) by liquidx
Branch: MAIN
utility class for python and python related packages

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

  ViewVC Help
Powered by ViewVC 1.1.20