Home | History | Annotate | Line # | Download | only in dist
py-compile revision 1.1
      1  1.1  mrg #!/bin/sh
      2  1.1  mrg # py-compile - Compile a Python program
      3  1.1  mrg 
      4  1.1  mrg scriptversion=2009-04-28.21; # UTC
      5  1.1  mrg 
      6  1.1  mrg # Copyright (C) 2000, 2001, 2003, 2004, 2005, 2008, 2009 Free Software
      7  1.1  mrg # Foundation, Inc.
      8  1.1  mrg 
      9  1.1  mrg # This program is free software; you can redistribute it and/or modify
     10  1.1  mrg # it under the terms of the GNU General Public License as published by
     11  1.1  mrg # the Free Software Foundation; either version 2, or (at your option)
     12  1.1  mrg # any later version.
     13  1.1  mrg 
     14  1.1  mrg # This program is distributed in the hope that it will be useful,
     15  1.1  mrg # but WITHOUT ANY WARRANTY; without even the implied warranty of
     16  1.1  mrg # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17  1.1  mrg # GNU General Public License for more details.
     18  1.1  mrg 
     19  1.1  mrg # You should have received a copy of the GNU General Public License
     20  1.1  mrg # along with this program.  If not, see <http://www.gnu.org/licenses/>.
     21  1.1  mrg 
     22  1.1  mrg # As a special exception to the GNU General Public License, if you
     23  1.1  mrg # distribute this file as part of a program that contains a
     24  1.1  mrg # configuration script generated by Autoconf, you may include it under
     25  1.1  mrg # the same distribution terms that you use for the rest of that program.
     26  1.1  mrg 
     27  1.1  mrg # This file is maintained in Automake, please report
     28  1.1  mrg # bugs to <bug-automake (at] gnu.org> or send patches to
     29  1.1  mrg # <automake-patches (at] gnu.org>.
     30  1.1  mrg 
     31  1.1  mrg if [ -z "$PYTHON" ]; then
     32  1.1  mrg   PYTHON=python
     33  1.1  mrg fi
     34  1.1  mrg 
     35  1.1  mrg basedir=
     36  1.1  mrg destdir=
     37  1.1  mrg files=
     38  1.1  mrg while test $# -ne 0; do
     39  1.1  mrg   case "$1" in
     40  1.1  mrg     --basedir)
     41  1.1  mrg       basedir=$2
     42  1.1  mrg       if test -z "$basedir"; then
     43  1.1  mrg         echo "$0: Missing argument to --basedir." 1>&2
     44  1.1  mrg         exit 1
     45  1.1  mrg       fi
     46  1.1  mrg       shift
     47  1.1  mrg       ;;
     48  1.1  mrg     --destdir)
     49  1.1  mrg       destdir=$2
     50  1.1  mrg       if test -z "$destdir"; then
     51  1.1  mrg         echo "$0: Missing argument to --destdir." 1>&2
     52  1.1  mrg         exit 1
     53  1.1  mrg       fi
     54  1.1  mrg       shift
     55  1.1  mrg       ;;
     56  1.1  mrg     -h|--h*)
     57  1.1  mrg       cat <<\EOF
     58  1.1  mrg Usage: py-compile [--help] [--version] [--basedir DIR] [--destdir DIR] FILES..."
     59  1.1  mrg 
     60  1.1  mrg Byte compile some python scripts FILES.  Use --destdir to specify any
     61  1.1  mrg leading directory path to the FILES that you don't want to include in the
     62  1.1  mrg byte compiled file.  Specify --basedir for any additional path information you
     63  1.1  mrg do want to be shown in the byte compiled file.
     64  1.1  mrg 
     65  1.1  mrg Example:
     66  1.1  mrg   py-compile --destdir /tmp/pkg-root --basedir /usr/share/test test.py test2.py
     67  1.1  mrg 
     68  1.1  mrg Report bugs to <bug-automake (at] gnu.org>.
     69  1.1  mrg EOF
     70  1.1  mrg       exit $?
     71  1.1  mrg       ;;
     72  1.1  mrg     -v|--v*)
     73  1.1  mrg       echo "py-compile $scriptversion"
     74  1.1  mrg       exit $?
     75  1.1  mrg       ;;
     76  1.1  mrg     *)
     77  1.1  mrg       files="$files $1"
     78  1.1  mrg       ;;
     79  1.1  mrg   esac
     80  1.1  mrg   shift
     81  1.1  mrg done
     82  1.1  mrg 
     83  1.1  mrg if test -z "$files"; then
     84  1.1  mrg     echo "$0: No files given.  Try \`$0 --help' for more information." 1>&2
     85  1.1  mrg     exit 1
     86  1.1  mrg fi
     87  1.1  mrg 
     88  1.1  mrg # if basedir was given, then it should be prepended to filenames before
     89  1.1  mrg # byte compilation.
     90  1.1  mrg if [ -z "$basedir" ]; then
     91  1.1  mrg     pathtrans="path = file"
     92  1.1  mrg else
     93  1.1  mrg     pathtrans="path = os.path.join('$basedir', file)"
     94  1.1  mrg fi
     95  1.1  mrg 
     96  1.1  mrg # if destdir was given, then it needs to be prepended to the filename to
     97  1.1  mrg # byte compile but not go into the compiled file.
     98  1.1  mrg if [ -z "$destdir" ]; then
     99  1.1  mrg     filetrans="filepath = path"
    100  1.1  mrg else
    101  1.1  mrg     filetrans="filepath = os.path.normpath('$destdir' + os.sep + path)"
    102  1.1  mrg fi
    103  1.1  mrg 
    104  1.1  mrg $PYTHON -c "
    105  1.1  mrg import sys, os, py_compile
    106  1.1  mrg 
    107  1.1  mrg files = '''$files'''
    108  1.1  mrg 
    109  1.1  mrg sys.stdout.write('Byte-compiling python modules...\n')
    110  1.1  mrg for file in files.split():
    111  1.1  mrg     $pathtrans
    112  1.1  mrg     $filetrans
    113  1.1  mrg     if not os.path.exists(filepath) or not (len(filepath) >= 3
    114  1.1  mrg                                             and filepath[-3:] == '.py'):
    115  1.1  mrg 	    continue
    116  1.1  mrg     sys.stdout.write(file)
    117  1.1  mrg     sys.stdout.flush()
    118  1.1  mrg     py_compile.compile(filepath, filepath + 'c', path)
    119  1.1  mrg sys.stdout.write('\n')" || exit $?
    120  1.1  mrg 
    121  1.1  mrg # this will fail for python < 1.5, but that doesn't matter ...
    122  1.1  mrg $PYTHON -O -c "
    123  1.1  mrg import sys, os, py_compile
    124  1.1  mrg 
    125  1.1  mrg files = '''$files'''
    126  1.1  mrg sys.stdout.write('Byte-compiling python modules (optimized versions) ...\n')
    127  1.1  mrg for file in files.split():
    128  1.1  mrg     $pathtrans
    129  1.1  mrg     $filetrans
    130  1.1  mrg     if not os.path.exists(filepath) or not (len(filepath) >= 3
    131  1.1  mrg                                             and filepath[-3:] == '.py'):
    132  1.1  mrg 	    continue
    133  1.1  mrg     sys.stdout.write(file)
    134  1.1  mrg     sys.stdout.flush()
    135  1.1  mrg     py_compile.compile(filepath, filepath + 'o', path)
    136  1.1  mrg sys.stdout.write('\n')" 2>/dev/null || :
    137  1.1  mrg 
    138  1.1  mrg # Local Variables:
    139  1.1  mrg # mode: shell-script
    140  1.1  mrg # sh-indentation: 2
    141  1.1  mrg # eval: (add-hook 'write-file-hooks 'time-stamp)
    142  1.1  mrg # time-stamp-start: "scriptversion="
    143  1.1  mrg # time-stamp-format: "%:y-%02m-%02d.%02H"
    144  1.1  mrg # time-stamp-time-zone: "UTC"
    145  1.1  mrg # time-stamp-end: "; # UTC"
    146  1.1  mrg # End:
    147