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 usage_error ()
37 {
38 echo "$me: $*" >&2
39 echo "Try '$me --help' for more information." >&2
40 exit 1
41 }
42
43 basedir=
44 destdir=
45 while test $# -ne 0; do
46 case "$1" in
47 --basedir)
48 if test $# -lt 2; then
49 usage_error "option '--basedir' requires an argument"
50 else
51 basedir=$2
52 fi
53 shift
54 ;;
55 --destdir)
56 if test $# -lt 2; then
57 usage_error "option '--destdir' requires an argument"
58 else
59 destdir=$2
60 fi
61 shift
62 ;;
63 -h|--help)
64 cat <<\EOF
65 Usage: py-compile [options] FILES...
66
67 Byte compile some python scripts FILES. Use --destdir to specify any
68 leading directory path to the FILES that you don't want to include in the
69 byte compiled file. Specify --basedir for any additional path information you
70 do want to be shown in the byte compiled file.
71
72 Options:
73 --basedir DIR Prefix all FILES with DIR, and include in error messages.
74 --destdir DIR Prefix all FILES with DIR before compiling.
75 -v, --version Display version information.
76 -h, --help This help screen.
77
78 Example:
79 py-compile --destdir /tmp/pkg-root --basedir /usr/share/test test.py test2.py
80
81 Report bugs to <[email protected]>.
82 GNU Automake home page: <https://www.gnu.org/software/automake/>.
83 General help using GNU software: <https://www.gnu.org/gethelp/>.
84 EOF
85 exit $?
86 ;;
87 -v|--version)
88 echo "$me (GNU Automake) $scriptversion"
89 exit $?
90 ;;
91 --)
92 shift
93 break
94 ;;
95 -*)
96 usage_error "unrecognized option '$1'"
97 ;;
98 *)
99 break
100 ;;
101 esac
102 shift
103 done
104
105 if test $# -eq 0; then
106 usage_error "no files given"
107 fi
108
109 # if basedir was given, then it should be prepended to filenames before
110 # byte compilation.
111 if test -z "$basedir"; then
112 pathtrans="path = file"
113 else
114 pathtrans="path = os.path.join('$basedir', file)"
115 fi
116
117 # if destdir was given, then it needs to be prepended to the filename to
118 # byte compile but not go into the compiled file.
119 if test -z "$destdir"; then
120 filetrans="filepath = path"
121 else
122 filetrans="filepath = os.path.normpath('$destdir' + os.sep + path)"
123 fi
124
125 python_major=`$PYTHON -c 'import sys; print(sys.version_info[0])'`
126 if test -z "$python_major"; then
127 usage_error "could not determine $PYTHON major version"
128 fi
129
130 case $python_major in
131 [01])
132 usage_error "python version 0.x and 1.x not supported"
133 ;;
134 esac
135
136 python_minor=`$PYTHON -c 'import sys; print(sys.version_info[1])'`
137
138 # NB: When adding support for newer versions, prefer copying & adding new cases
139 # rather than try to keep things merged with shell variables.
140
141 # First byte compile (no optimization) all the modules.
142 # This works for all currently known Python versions.
143 $PYTHON -c "
144 import sys, os, py_compile
145
146 try:
147 import importlib
148 except ImportError:
149 importlib = None
150
151 # importlib.util.cache_from_source was added in 3.4
152 if (
153 hasattr(importlib, 'util')
154 and hasattr(importlib.util, 'cache_from_source')
155 ):
156 destpath = importlib.util.cache_from_source
157 else:
158 destpath = lambda filepath: filepath + 'c'
159
160 sys.stdout.write('Byte-compiling python modules...\n')
161 for file in sys.argv[1:]:
162 $pathtrans
163 $filetrans
164 if (
165 not os.path.exists(filepath)
166 or not (len(filepath) >= 3 and filepath[-3:] == '.py')
167 ):
168 continue
169 sys.stdout.write(file + ' ')
170 sys.stdout.flush()
171 py_compile.compile(filepath, destpath(filepath), path)
172 sys.stdout.write('\n')" "$@" || exit $?
173
174 # Then byte compile w/optimization all the modules.
175 $PYTHON -O -c "
176 import sys, os, py_compile
177
178 try:
179 import importlib
180 except ImportError:
181 importlib = None
182
183 # importlib.util.cache_from_source was added in 3.4
184 if (
185 hasattr(importlib, 'util')
186 and hasattr(importlib.util, 'cache_from_source')
187 ):
188 destpath = importlib.util.cache_from_source
189 else:
190 destpath = lambda filepath: filepath + 'o'
191
192 # pypy2 does not use .pyo optimization
193 if sys.version_info.major <= 2 and hasattr(sys, 'pypy_translation_info'):
194 sys.exit(0)
195
196 sys.stdout.write('Byte-compiling python modules (optimized versions) ...\n')
197 for file in sys.argv[1:]:
198 $pathtrans
199 $filetrans
200 if (
201 not os.path.exists(filepath)
202 or not (len(filepath) >= 3 and filepath[-3:] == '.py')
203 ):
204 continue
205 sys.stdout.write(file + ' ')
206 sys.stdout.flush()
207 py_compile.compile(filepath, destpath(filepath), path)
208 sys.stdout.write('\n')" "$@" 2>/dev/null || exit $?
209
210 # Then byte compile w/more optimization.
211 # Only do this for Python 3.5+, see https://bugs.gnu.org/38043 for background.
212 case $python_major.$python_minor in
213 2.*|3.[0-4])
214 ;;
215 *)
216 $PYTHON -OO -c "
217 import sys, os, py_compile, importlib
218
219 sys.stdout.write('Byte-compiling python modules (more optimized versions)'
220 ' ...\n')
221 for file in sys.argv[1:]:
222 $pathtrans
223 $filetrans
224 if (
225 not os.path.exists(filepath)
226 or not (len(filepath) >= 3 and filepath[-3:] == '.py')
227 ):
228 continue
229 sys.stdout.write(file + ' ')
230 sys.stdout.flush()
231 py_compile.compile(filepath, importlib.util.cache_from_source(filepath), path)
232 sys.stdout.write('\n')" "$@" 2>/dev/null || exit $?
233 ;;
234 esac
235
236 # Local Variables:
237 # mode: shell-script
238 # sh-indentation: 2
239 # eval: (add-hook 'before-save-hook 'time-stamp)
240 # time-stamp-start: "scriptversion="
241 # time-stamp-format: "%:y-%02m-%02d.%02H"
242 # time-stamp-time-zone: "UTC0"
243 # time-stamp-end: "; # UTC"
244 # End: