1 1.1 mrg #!/bin/sh 2 1.1 mrg # py-compile - Compile a Python program 3 1.1 mrg 4 1.1.1.7 mrg scriptversion=2021-02-27.01; # UTC 5 1.1 mrg 6 1.1.1.7 mrg # Copyright (C) 2000-2021 Free Software Foundation, Inc. 7 1.1 mrg 8 1.1 mrg # This program is free software; you can redistribute it and/or modify 9 1.1 mrg # it under the terms of the GNU General Public License as published by 10 1.1 mrg # the Free Software Foundation; either version 2, or (at your option) 11 1.1 mrg # any later version. 12 1.1 mrg 13 1.1 mrg # This program is distributed in the hope that it will be useful, 14 1.1 mrg # but WITHOUT ANY WARRANTY; without even the implied warranty of 15 1.1 mrg # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 1.1 mrg # GNU General Public License for more details. 17 1.1 mrg 18 1.1 mrg # You should have received a copy of the GNU General Public License 19 1.1.1.6 mrg # along with this program. If not, see <https://www.gnu.org/licenses/>. 20 1.1 mrg 21 1.1 mrg # As a special exception to the GNU General Public License, if you 22 1.1 mrg # distribute this file as part of a program that contains a 23 1.1 mrg # configuration script generated by Autoconf, you may include it under 24 1.1 mrg # the same distribution terms that you use for the rest of that program. 25 1.1 mrg 26 1.1 mrg # This file is maintained in Automake, please report 27 1.1 mrg # bugs to <bug-automake (at] gnu.org> or send patches to 28 1.1 mrg # <automake-patches (at] gnu.org>. 29 1.1 mrg 30 1.1.1.7 mrg if test -z "$PYTHON"; then 31 1.1 mrg PYTHON=python 32 1.1 mrg fi 33 1.1 mrg 34 1.1.1.2 mrg me=py-compile 35 1.1.1.2 mrg 36 1.1.1.2 mrg usage_error () 37 1.1.1.2 mrg { 38 1.1.1.2 mrg echo "$me: $*" >&2 39 1.1.1.3 mrg echo "Try '$me --help' for more information." >&2 40 1.1.1.2 mrg exit 1 41 1.1.1.2 mrg } 42 1.1.1.2 mrg 43 1.1 mrg basedir= 44 1.1 mrg destdir= 45 1.1 mrg while test $# -ne 0; do 46 1.1 mrg case "$1" in 47 1.1 mrg --basedir) 48 1.1.1.2 mrg if test $# -lt 2; then 49 1.1.1.2 mrg usage_error "option '--basedir' requires an argument" 50 1.1.1.2 mrg else 51 1.1.1.2 mrg basedir=$2 52 1.1 mrg fi 53 1.1 mrg shift 54 1.1 mrg ;; 55 1.1 mrg --destdir) 56 1.1.1.2 mrg if test $# -lt 2; then 57 1.1.1.2 mrg usage_error "option '--destdir' requires an argument" 58 1.1.1.2 mrg else 59 1.1.1.2 mrg destdir=$2 60 1.1 mrg fi 61 1.1 mrg shift 62 1.1 mrg ;; 63 1.1.1.2 mrg -h|--help) 64 1.1 mrg cat <<\EOF 65 1.1 mrg Usage: py-compile [--help] [--version] [--basedir DIR] [--destdir DIR] FILES..." 66 1.1 mrg 67 1.1 mrg Byte compile some python scripts FILES. Use --destdir to specify any 68 1.1 mrg leading directory path to the FILES that you don't want to include in the 69 1.1 mrg byte compiled file. Specify --basedir for any additional path information you 70 1.1 mrg do want to be shown in the byte compiled file. 71 1.1 mrg 72 1.1 mrg Example: 73 1.1 mrg py-compile --destdir /tmp/pkg-root --basedir /usr/share/test test.py test2.py 74 1.1 mrg 75 1.1 mrg Report bugs to <bug-automake (at] gnu.org>. 76 1.1 mrg EOF 77 1.1 mrg exit $? 78 1.1 mrg ;; 79 1.1.1.2 mrg -v|--version) 80 1.1.1.2 mrg echo "$me $scriptversion" 81 1.1 mrg exit $? 82 1.1 mrg ;; 83 1.1.1.2 mrg --) 84 1.1.1.2 mrg shift 85 1.1.1.2 mrg break 86 1.1.1.2 mrg ;; 87 1.1.1.2 mrg -*) 88 1.1.1.2 mrg usage_error "unrecognized option '$1'" 89 1.1.1.2 mrg ;; 90 1.1 mrg *) 91 1.1.1.2 mrg break 92 1.1 mrg ;; 93 1.1 mrg esac 94 1.1 mrg shift 95 1.1 mrg done 96 1.1 mrg 97 1.1.1.2 mrg files=$* 98 1.1 mrg if test -z "$files"; then 99 1.1.1.7 mrg usage_error "no files given" 100 1.1 mrg fi 101 1.1 mrg 102 1.1 mrg # if basedir was given, then it should be prepended to filenames before 103 1.1 mrg # byte compilation. 104 1.1.1.7 mrg if test -z "$basedir"; then 105 1.1.1.7 mrg pathtrans="path = file" 106 1.1 mrg else 107 1.1.1.7 mrg pathtrans="path = os.path.join('$basedir', file)" 108 1.1 mrg fi 109 1.1 mrg 110 1.1 mrg # if destdir was given, then it needs to be prepended to the filename to 111 1.1 mrg # byte compile but not go into the compiled file. 112 1.1.1.7 mrg if test -z "$destdir"; then 113 1.1.1.7 mrg filetrans="filepath = path" 114 1.1 mrg else 115 1.1.1.7 mrg filetrans="filepath = os.path.normpath('$destdir' + os.sep + path)" 116 1.1.1.7 mrg fi 117 1.1.1.7 mrg 118 1.1.1.7 mrg python_major=`$PYTHON -V 2>&1 | sed -e 's/.* //;s/\..*$//;1q'` 119 1.1.1.7 mrg if test -z "$python_major"; then 120 1.1.1.7 mrg echo "$me: could not determine $PYTHON major version, guessing 3" >&2 121 1.1.1.7 mrg python_major=3 122 1.1.1.7 mrg fi 123 1.1.1.7 mrg 124 1.1.1.7 mrg # The old way to import libraries was deprecated. 125 1.1.1.7 mrg if test "$python_major" -le 2; then 126 1.1.1.7 mrg import_lib=imp 127 1.1.1.7 mrg import_test="hasattr(imp, 'get_tag')" 128 1.1.1.7 mrg import_call=imp.cache_from_source 129 1.1.1.7 mrg else 130 1.1.1.7 mrg import_lib=importlib 131 1.1.1.7 mrg import_test="hasattr(sys.implementation, 'cache_tag')" 132 1.1.1.7 mrg import_call=importlib.util.cache_from_source 133 1.1 mrg fi 134 1.1 mrg 135 1.1 mrg $PYTHON -c " 136 1.1.1.7 mrg import sys, os, py_compile, $import_lib 137 1.1 mrg 138 1.1 mrg files = '''$files''' 139 1.1 mrg 140 1.1 mrg sys.stdout.write('Byte-compiling python modules...\n') 141 1.1 mrg for file in files.split(): 142 1.1 mrg $pathtrans 143 1.1 mrg $filetrans 144 1.1 mrg if not os.path.exists(filepath) or not (len(filepath) >= 3 145 1.1 mrg and filepath[-3:] == '.py'): 146 1.1 mrg continue 147 1.1 mrg sys.stdout.write(file) 148 1.1 mrg sys.stdout.flush() 149 1.1.1.7 mrg if $import_test: 150 1.1.1.7 mrg py_compile.compile(filepath, $import_call(filepath), path) 151 1.1.1.3 mrg else: 152 1.1.1.3 mrg py_compile.compile(filepath, filepath + 'c', path) 153 1.1 mrg sys.stdout.write('\n')" || exit $? 154 1.1 mrg 155 1.1 mrg # this will fail for python < 1.5, but that doesn't matter ... 156 1.1 mrg $PYTHON -O -c " 157 1.1.1.7 mrg import sys, os, py_compile, $import_lib 158 1.1.1.3 mrg 159 1.1.1.3 mrg # pypy does not use .pyo optimization 160 1.1.1.7 mrg if hasattr(sys, 'pypy_translation_info') and sys.hexversion < 0x03050000: 161 1.1.1.3 mrg sys.exit(0) 162 1.1 mrg 163 1.1 mrg files = '''$files''' 164 1.1 mrg sys.stdout.write('Byte-compiling python modules (optimized versions) ...\n') 165 1.1 mrg for file in files.split(): 166 1.1 mrg $pathtrans 167 1.1 mrg $filetrans 168 1.1 mrg if not os.path.exists(filepath) or not (len(filepath) >= 3 169 1.1 mrg and filepath[-3:] == '.py'): 170 1.1 mrg continue 171 1.1 mrg sys.stdout.write(file) 172 1.1 mrg sys.stdout.flush() 173 1.1.1.7 mrg if $import_test: 174 1.1.1.7 mrg py_compile.compile(filepath, $import_call(filepath), path) 175 1.1.1.3 mrg else: 176 1.1.1.3 mrg py_compile.compile(filepath, filepath + 'o', path) 177 1.1 mrg sys.stdout.write('\n')" 2>/dev/null || : 178 1.1 mrg 179 1.1.1.7 mrg $PYTHON -OO -c " 180 1.1.1.7 mrg import sys, os, py_compile, $import_lib 181 1.1.1.7 mrg 182 1.1.1.7 mrg # python<3.5 does not have split files for -O and -OO 183 1.1.1.7 mrg if sys.hexversion < 0x03050000: 184 1.1.1.7 mrg sys.exit(0) 185 1.1.1.7 mrg 186 1.1.1.7 mrg files = '''$files''' 187 1.1.1.7 mrg sys.stdout.write('Byte-compiling python modules (optimized versions) ...\n') 188 1.1.1.7 mrg for file in files.split(): 189 1.1.1.7 mrg $pathtrans 190 1.1.1.7 mrg $filetrans 191 1.1.1.7 mrg if not os.path.exists(filepath) or not (len(filepath) >= 3 192 1.1.1.7 mrg and filepath[-3:] == '.py'): 193 1.1.1.7 mrg continue 194 1.1.1.7 mrg sys.stdout.write(file) 195 1.1.1.7 mrg sys.stdout.flush() 196 1.1.1.7 mrg if $import_test: 197 1.1.1.7 mrg py_compile.compile(filepath, $import_call(filepath), path) 198 1.1.1.7 mrg else: 199 1.1.1.7 mrg py_compile.compile(filepath, filepath + 'o', path) 200 1.1.1.7 mrg sys.stdout.write('\n')" 2>/dev/null || exit $? 201 1.1.1.7 mrg 202 1.1 mrg # Local Variables: 203 1.1 mrg # mode: shell-script 204 1.1 mrg # sh-indentation: 2 205 1.1.1.6 mrg # eval: (add-hook 'before-save-hook 'time-stamp) 206 1.1 mrg # time-stamp-start: "scriptversion=" 207 1.1 mrg # time-stamp-format: "%:y-%02m-%02d.%02H" 208 1.1.1.5 mrg # time-stamp-time-zone: "UTC0" 209 1.1 mrg # time-stamp-end: "; # UTC" 210 1.1 mrg # End: 211