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