Home | History | Annotate | Line # | Download | only in dist
py-compile revision 1.1.1.6
      1      1.1  mrg #!/bin/sh
      2      1.1  mrg # py-compile - Compile a Python program
      3      1.1  mrg 
      4  1.1.1.6  mrg scriptversion=2018-03-07.03; # UTC
      5      1.1  mrg 
      6  1.1.1.6  mrg # Copyright (C) 2000-2018 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  mrg if [ -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.2  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  mrg if [ -z "$basedir" ]; then
    105      1.1  mrg     pathtrans="path = file"
    106      1.1  mrg else
    107      1.1  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  mrg if [ -z "$destdir" ]; then
    113      1.1  mrg     filetrans="filepath = path"
    114      1.1  mrg else
    115      1.1  mrg     filetrans="filepath = os.path.normpath('$destdir' + os.sep + path)"
    116      1.1  mrg fi
    117      1.1  mrg 
    118      1.1  mrg $PYTHON -c "
    119  1.1.1.3  mrg import sys, os, py_compile, imp
    120      1.1  mrg 
    121      1.1  mrg files = '''$files'''
    122      1.1  mrg 
    123      1.1  mrg sys.stdout.write('Byte-compiling python modules...\n')
    124      1.1  mrg for file in files.split():
    125      1.1  mrg     $pathtrans
    126      1.1  mrg     $filetrans
    127      1.1  mrg     if not os.path.exists(filepath) or not (len(filepath) >= 3
    128      1.1  mrg                                             and filepath[-3:] == '.py'):
    129      1.1  mrg 	    continue
    130      1.1  mrg     sys.stdout.write(file)
    131      1.1  mrg     sys.stdout.flush()
    132  1.1.1.3  mrg     if hasattr(imp, 'get_tag'):
    133  1.1.1.3  mrg         py_compile.compile(filepath, imp.cache_from_source(filepath), path)
    134  1.1.1.3  mrg     else:
    135  1.1.1.3  mrg         py_compile.compile(filepath, filepath + 'c', path)
    136      1.1  mrg sys.stdout.write('\n')" || exit $?
    137      1.1  mrg 
    138      1.1  mrg # this will fail for python < 1.5, but that doesn't matter ...
    139      1.1  mrg $PYTHON -O -c "
    140  1.1.1.3  mrg import sys, os, py_compile, imp
    141  1.1.1.3  mrg 
    142  1.1.1.3  mrg # pypy does not use .pyo optimization
    143  1.1.1.3  mrg if hasattr(sys, 'pypy_translation_info'):
    144  1.1.1.3  mrg     sys.exit(0)
    145      1.1  mrg 
    146      1.1  mrg files = '''$files'''
    147      1.1  mrg sys.stdout.write('Byte-compiling python modules (optimized versions) ...\n')
    148      1.1  mrg for file in files.split():
    149      1.1  mrg     $pathtrans
    150      1.1  mrg     $filetrans
    151      1.1  mrg     if not os.path.exists(filepath) or not (len(filepath) >= 3
    152      1.1  mrg                                             and filepath[-3:] == '.py'):
    153      1.1  mrg 	    continue
    154      1.1  mrg     sys.stdout.write(file)
    155      1.1  mrg     sys.stdout.flush()
    156  1.1.1.3  mrg     if hasattr(imp, 'get_tag'):
    157  1.1.1.3  mrg         py_compile.compile(filepath, imp.cache_from_source(filepath, False), path)
    158  1.1.1.3  mrg     else:
    159  1.1.1.3  mrg         py_compile.compile(filepath, filepath + 'o', path)
    160      1.1  mrg sys.stdout.write('\n')" 2>/dev/null || :
    161      1.1  mrg 
    162      1.1  mrg # Local Variables:
    163      1.1  mrg # mode: shell-script
    164      1.1  mrg # sh-indentation: 2
    165  1.1.1.6  mrg # eval: (add-hook 'before-save-hook 'time-stamp)
    166      1.1  mrg # time-stamp-start: "scriptversion="
    167      1.1  mrg # time-stamp-format: "%:y-%02m-%02d.%02H"
    168  1.1.1.5  mrg # time-stamp-time-zone: "UTC0"
    169      1.1  mrg # time-stamp-end: "; # UTC"
    170      1.1  mrg # End:
    171