yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame^] | 1 | #!/bin/sh |
| 2 | # py-compile - Compile a Python program |
| 3 | |
| 4 | scriptversion=2005-05-14.22 |
| 5 | |
| 6 | # Copyright (C) 2000, 2001, 2003, 2004, 2005 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, write to the Free Software |
| 20 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 21 | # 02110-1301, USA. |
| 22 | |
| 23 | # As a special exception to the GNU General Public License, if you |
| 24 | # distribute this file as part of a program that contains a |
| 25 | # configuration script generated by Autoconf, you may include it under |
| 26 | # the same distribution terms that you use for the rest of that program. |
| 27 | |
| 28 | # This file is maintained in Automake, please report |
| 29 | # bugs to <bug-automake@gnu.org> or send patches to |
| 30 | # <automake-patches@gnu.org>. |
| 31 | |
| 32 | if [ -z "$PYTHON" ]; then |
| 33 | PYTHON=python |
| 34 | fi |
| 35 | |
| 36 | basedir= |
| 37 | destdir= |
| 38 | files= |
| 39 | while test $# -ne 0; do |
| 40 | case "$1" in |
| 41 | --basedir) |
| 42 | basedir=$2 |
| 43 | if test -z "$basedir"; then |
| 44 | echo "$0: Missing argument to --basedir." 1>&2 |
| 45 | exit 1 |
| 46 | fi |
| 47 | shift |
| 48 | ;; |
| 49 | --destdir) |
| 50 | destdir=$2 |
| 51 | if test -z "$destdir"; then |
| 52 | echo "$0: Missing argument to --destdir." 1>&2 |
| 53 | exit 1 |
| 54 | fi |
| 55 | shift |
| 56 | ;; |
| 57 | -h|--h*) |
| 58 | cat <<\EOF |
| 59 | Usage: py-compile [--help] [--version] [--basedir DIR] [--destdir DIR] FILES..." |
| 60 | |
| 61 | Byte compile some python scripts FILES. Use --destdir to specify any |
| 62 | leading directory path to the FILES that you don't want to include in the |
| 63 | byte compiled file. Specify --basedir for any additional path information you |
| 64 | do want to be shown in the byte compiled file. |
| 65 | |
| 66 | Example: |
| 67 | py-compile --destdir /tmp/pkg-root --basedir /usr/share/test test.py test2.py |
| 68 | |
| 69 | Report bugs to <bug-automake@gnu.org>. |
| 70 | EOF |
| 71 | exit $? |
| 72 | ;; |
| 73 | -v|--v*) |
| 74 | echo "py-compile $scriptversion" |
| 75 | exit $? |
| 76 | ;; |
| 77 | *) |
| 78 | files="$files $1" |
| 79 | ;; |
| 80 | esac |
| 81 | shift |
| 82 | done |
| 83 | |
| 84 | if test -z "$files"; then |
| 85 | echo "$0: No files given. Try \`$0 --help' for more information." 1>&2 |
| 86 | exit 1 |
| 87 | fi |
| 88 | |
| 89 | # if basedir was given, then it should be prepended to filenames before |
| 90 | # byte compilation. |
| 91 | if [ -z "$basedir" ]; then |
| 92 | pathtrans="path = file" |
| 93 | else |
| 94 | pathtrans="path = os.path.join('$basedir', file)" |
| 95 | fi |
| 96 | |
| 97 | # if destdir was given, then it needs to be prepended to the filename to |
| 98 | # byte compile but not go into the compiled file. |
| 99 | if [ -z "$destdir" ]; then |
| 100 | filetrans="filepath = path" |
| 101 | else |
| 102 | filetrans="filepath = os.path.normpath('$destdir' + os.sep + path)" |
| 103 | fi |
| 104 | |
| 105 | $PYTHON -c " |
| 106 | import sys, os, string, py_compile |
| 107 | |
| 108 | files = '''$files''' |
| 109 | |
| 110 | print 'Byte-compiling python modules...' |
| 111 | for file in string.split(files): |
| 112 | $pathtrans |
| 113 | $filetrans |
| 114 | if not os.path.exists(filepath) or not (len(filepath) >= 3 |
| 115 | and filepath[-3:] == '.py'): |
| 116 | continue |
| 117 | print file, |
| 118 | sys.stdout.flush() |
| 119 | py_compile.compile(filepath, filepath + 'c', path) |
| 120 | print" || exit $? |
| 121 | |
| 122 | # this will fail for python < 1.5, but that doesn't matter ... |
| 123 | $PYTHON -O -c " |
| 124 | import sys, os, string, py_compile |
| 125 | |
| 126 | files = '''$files''' |
| 127 | print 'Byte-compiling python modules (optimized versions) ...' |
| 128 | for file in string.split(files): |
| 129 | $pathtrans |
| 130 | $filetrans |
| 131 | if not os.path.exists(filepath) or not (len(filepath) >= 3 |
| 132 | and filepath[-3:] == '.py'): |
| 133 | continue |
| 134 | print file, |
| 135 | sys.stdout.flush() |
| 136 | py_compile.compile(filepath, filepath + 'o', path) |
| 137 | print" 2>/dev/null || : |
| 138 | |
| 139 | # Local Variables: |
| 140 | # mode: shell-script |
| 141 | # sh-indentation: 2 |
| 142 | # eval: (add-hook 'write-file-hooks 'time-stamp) |
| 143 | # time-stamp-start: "scriptversion=" |
| 144 | # time-stamp-format: "%:y-%02m-%02d.%02H" |
| 145 | # time-stamp-end: "$" |
| 146 | # End: |