Debian Codegraph @

py-compile

raw
1 #!/bin/sh
2 # py-compile - Compile a Python program
3
4 scriptversion=2024-06-19.01; # UTC
5
6 # Copyright (C) 2000-2024 Free Software Foundation, Inc.
7
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2, or (at your option)
11 # any later version.
12
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <https://www.gnu.org/licenses/>.
20
21 # As a special exception to the GNU General Public License, if you
22 # distribute this file as part of a program that contains a
23 # configuration script generated by Autoconf, you may include it under
24 # the same distribution terms that you use for the rest of that program.
25
26 # This file is maintained in Automake, please report
27 # bugs to <[email protected]> or send patches to
28 # <[email protected]>.
29
30 if test -z "$PYTHON"; then
31 PYTHON=python
32 fi
33
34 me=py-compile
35
36 case "$PYTHON" in
37 *python*) ;;
38 *)
39 echo "$me: Invalid python executable ${PYTHON}";
40 echo "$me: Python support disabled";
41 exit 0;;
42 esac
43
44 usage_error ()
45 {
46 echo "$me: $*" >&2
47 echo "Try '$me --help' for more information." >&2
48 exit 1
49 }
50
51 basedir=
52 destdir=
53 while test $# -ne 0; do
54 case "$1" in
55 --basedir)
56 if test $# -lt 2; then
57 usage_error "option '--basedir' requires an argument"
58 else
59 basedir=$2
60 fi
61 shift
62 ;;
63 --destdir)
64 if test $# -lt 2; then
65 usage_error "option '--destdir' requires an argument"
66 else
67 destdir=$2
68 fi
69 shift
70 ;;
71 -h|--help)
72 cat <<\EOF
73 Usage: py-compile [options] FILES...
74
75 Byte compile some python scripts FILES. Use --destdir to specify any
76 leading directory path to the FILES that you don't want to include in the
77 byte compiled file. Specify --basedir for any additional path information you
78 do want to be shown in the byte compiled file.
79
80 Options:
81 --basedir DIR Prefix all FILES with DIR, and include in error messages.
82 --destdir DIR Prefix all FILES with DIR before compiling.
83 -v, --version Display version information.
84 -h, --help This help screen.
85
86 Example:
87 py-compile --destdir /tmp/pkg-root --basedir /usr/share/test test.py test2.py
88
89 Report bugs to <[email protected]>.
90 GNU Automake home page: <https://www.gnu.org/software/automake/>.
91 General help using GNU software: <https://www.gnu.org/gethelp/>.
92 EOF
93 exit $?
94 ;;
95 -v|--version)
96 echo "$me (GNU Automake) $scriptversion"
97 exit $?
98 ;;
99 --)
100 shift
101 break
102 ;;
103 -*)
104 usage_error "unrecognized option '$1'"
105 ;;
106 *)
107 break
108 ;;
109 esac
110 shift
111 done
112
113 if test $# -eq 0; then
114 usage_error "no files given"
115 fi
116
117 # if basedir was given, then it should be prepended to filenames before
118 # byte compilation.
119 if test -z "$basedir"; then
120 pathtrans="path = file"
121 else
122 pathtrans="path = os.path.join('$basedir', file)"
123 fi
124
125 # if destdir was given, then it needs to be prepended to the filename to
126 # byte compile but not go into the compiled file.
127 if test -z "$destdir"; then
128 filetrans="filepath = path"
129 else
130 filetrans="filepath = os.path.normpath('$destdir' + os.sep + path)"
131 fi
132
133 python_major=`$PYTHON -c 'import sys; print(sys.version_info[0])'`
134 if test -z "$python_major"; then
135 usage_error "could not determine $PYTHON major version"
136 fi
137
138 case $python_major in
139 [01])
140 usage_error "python version 0.x and 1.x not supported"
141 ;;
142 esac
143
144 python_minor=`$PYTHON -c 'import sys; print(sys.version_info[1])'`
145
146 # NB: When adding support for newer versions, prefer copying & adding new cases
147 # rather than try to keep things merged with shell variables.
148
149 # First byte compile (no optimization) all the modules.
150 # This works for all currently known Python versions.
151 $PYTHON -c "
152 import sys, os, py_compile
153
154 try:
155 import importlib
156 except ImportError:
157 importlib = None
158
159 # importlib.util.cache_from_source was added in 3.4
160 if (
161 hasattr(importlib, 'util')
162 and hasattr(importlib.util, 'cache_from_source')
163 ):
164 destpath = importlib.util.cache_from_source
165 else:
166 destpath = lambda filepath: filepath + 'c'
167
168 sys.stdout.write('Byte-compiling python modules...\n')
169 for file in sys.argv[1:]:
170 $pathtrans
171 $filetrans
172 if (
173 not os.path.exists(filepath)
174 or not (len(filepath) >= 3 and filepath[-3:] == '.py')
175 ):
176 continue
177 sys.stdout.write(file + ' ')
178 sys.stdout.flush()
179 py_compile.compile(filepath, destpath(filepath), path)
180 sys.stdout.write('\n')" "$@" || exit $?
181
182 # Then byte compile w/optimization all the modules.
183 $PYTHON -O -c "
184 import sys, os, py_compile
185
186 try:
187 import importlib
188 except ImportError:
189 importlib = None
190
191 # importlib.util.cache_from_source was added in 3.4
192 if (
193 hasattr(importlib, 'util')
194 and hasattr(importlib.util, 'cache_from_source')
195 ):
196 destpath = importlib.util.cache_from_source
197 else:
198 destpath = lambda filepath: filepath + 'o'
199
200 # pypy2 does not use .pyo optimization
201 if sys.version_info.major <= 2 and hasattr(sys, 'pypy_translation_info'):
202 sys.exit(0)
203
204 sys.stdout.write('Byte-compiling python modules (optimized versions) ...\n')
205 for file in sys.argv[1:]:
206 $pathtrans
207 $filetrans
208 if (
209 not os.path.exists(filepath)
210 or not (len(filepath) >= 3 and filepath[-3:] == '.py')
211 ):
212 continue
213 sys.stdout.write(file + ' ')
214 sys.stdout.flush()
215 py_compile.compile(filepath, destpath(filepath), path)
216 sys.stdout.write('\n')" "$@" 2>/dev/null || exit $?
217
218 # Then byte compile w/more optimization.
219 # Only do this for Python 3.5+, see https://bugs.gnu.org/38043 for background.
220 case $python_major.$python_minor in
221 2.*|3.[0-4])
222 ;;
223 *)
224 $PYTHON -OO -c "
225 import sys, os, py_compile, importlib
226
227 sys.stdout.write('Byte-compiling python modules (more optimized versions)'
228 ' ...\n')
229 for file in sys.argv[1:]:
230 $pathtrans
231 $filetrans
232 if (
233 not os.path.exists(filepath)
234 or not (len(filepath) >= 3 and filepath[-3:] == '.py')
235 ):
236 continue
237 sys.stdout.write(file + ' ')
238 sys.stdout.flush()
239 py_compile.compile(filepath, importlib.util.cache_from_source(filepath), path)
240 sys.stdout.write('\n')" "$@" 2>/dev/null || exit $?
241 ;;
242 esac
243
244 # Local Variables:
245 # mode: shell-script
246 # sh-indentation: 2
247 # eval: (add-hook 'before-save-hook 'time-stamp)
248 # time-stamp-start: "scriptversion="
249 # time-stamp-format: "%:y-%02m-%02d.%02H"
250 # time-stamp-time-zone: "UTC0"
251 # time-stamp-end: "; # UTC"
252 # End: