Home | History | Annotate | Line # | Download | only in dist
      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=2021-02-27.01; # UTC
      5  1.1  mrg 
      6  1.1  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  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 test -z "$PYTHON"; then
     31  1.1  mrg   PYTHON=python
     32  1.1  mrg fi
     33  1.1  mrg 
     34  1.1  mrg me=py-compile
     35  1.1  mrg 
     36  1.1  mrg usage_error ()
     37  1.1  mrg {
     38  1.1  mrg   echo "$me: $*" >&2
     39  1.1  mrg   echo "Try '$me --help' for more information." >&2
     40  1.1  mrg   exit 1
     41  1.1  mrg }
     42  1.1  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  mrg       if test $# -lt 2; then
     49  1.1  mrg         usage_error "option '--basedir' requires an argument"
     50  1.1  mrg       else
     51  1.1  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  mrg       if test $# -lt 2; then
     57  1.1  mrg         usage_error "option '--destdir' requires an argument"
     58  1.1  mrg       else
     59  1.1  mrg         destdir=$2
     60  1.1  mrg       fi
     61  1.1  mrg       shift
     62  1.1  mrg       ;;
     63  1.1  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  mrg     -v|--version)
     80  1.1  mrg       echo "$me $scriptversion"
     81  1.1  mrg       exit $?
     82  1.1  mrg       ;;
     83  1.1  mrg     --)
     84  1.1  mrg       shift
     85  1.1  mrg       break
     86  1.1  mrg       ;;
     87  1.1  mrg     -*)
     88  1.1  mrg       usage_error "unrecognized option '$1'"
     89  1.1  mrg       ;;
     90  1.1  mrg     *)
     91  1.1  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  mrg files=$*
     98  1.1  mrg if test -z "$files"; then
     99  1.1  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 test -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 test -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_major=`$PYTHON -V 2>&1 | sed -e 's/.* //;s/\..*$//;1q'`
    119  1.1  mrg if test -z "$python_major"; then
    120  1.1  mrg   echo "$me: could not determine $PYTHON major version, guessing 3" >&2
    121  1.1  mrg   python_major=3
    122  1.1  mrg fi
    123  1.1  mrg 
    124  1.1  mrg # The old way to import libraries was deprecated.
    125  1.1  mrg if test "$python_major" -le 2; then
    126  1.1  mrg   import_lib=imp
    127  1.1  mrg   import_test="hasattr(imp, 'get_tag')"
    128  1.1  mrg   import_call=imp.cache_from_source
    129  1.1  mrg   import_arg2=', False' # needed in one call and not the other
    130  1.1  mrg else
    131  1.1  mrg   import_lib=importlib
    132  1.1  mrg   import_test="hasattr(sys.implementation, 'cache_tag')"
    133  1.1  mrg   import_call=importlib.util.cache_from_source
    134  1.1  mrg   import_arg2=
    135  1.1  mrg fi
    136  1.1  mrg 
    137  1.1  mrg $PYTHON -c "
    138  1.1  mrg import sys, os, py_compile, $import_lib
    139  1.1  mrg 
    140  1.1  mrg files = '''$files'''
    141  1.1  mrg 
    142  1.1  mrg sys.stdout.write('Byte-compiling python modules...\n')
    143  1.1  mrg for file in files.split():
    144  1.1  mrg     $pathtrans
    145  1.1  mrg     $filetrans
    146  1.1  mrg     if not os.path.exists(filepath) or not (len(filepath) >= 3
    147  1.1  mrg                                             and filepath[-3:] == '.py'):
    148  1.1  mrg 	    continue
    149  1.1  mrg     sys.stdout.write(file)
    150  1.1  mrg     sys.stdout.flush()
    151  1.1  mrg     if $import_test:
    152  1.1  mrg         py_compile.compile(filepath, $import_call(filepath), path)
    153  1.1  mrg     else:
    154  1.1  mrg         py_compile.compile(filepath, filepath + 'c', path)
    155  1.1  mrg sys.stdout.write('\n')" || exit $?
    156  1.1  mrg 
    157  1.1  mrg # this will fail for python < 1.5, but that doesn't matter ...
    158  1.1  mrg $PYTHON -O -c "
    159  1.1  mrg import sys, os, py_compile, $import_lib
    160  1.1  mrg 
    161  1.1  mrg # pypy does not use .pyo optimization
    162  1.1  mrg if hasattr(sys, 'pypy_translation_info'):
    163  1.1  mrg     sys.exit(0)
    164  1.1  mrg 
    165  1.1  mrg files = '''$files'''
    166  1.1  mrg sys.stdout.write('Byte-compiling python modules (optimized versions) ...\n')
    167  1.1  mrg for file in files.split():
    168  1.1  mrg     $pathtrans
    169  1.1  mrg     $filetrans
    170  1.1  mrg     if not os.path.exists(filepath) or not (len(filepath) >= 3
    171  1.1  mrg                                             and filepath[-3:] == '.py'):
    172  1.1  mrg 	    continue
    173  1.1  mrg     sys.stdout.write(file)
    174  1.1  mrg     sys.stdout.flush()
    175  1.1  mrg     if $import_test:
    176  1.1  mrg         py_compile.compile(filepath, $import_call(filepath$import_arg2), path)
    177  1.1  mrg     else:
    178  1.1  mrg         py_compile.compile(filepath, filepath + 'o', path)
    179  1.1  mrg sys.stdout.write('\n')" 2>/dev/null || exit $?
    180  1.1  mrg 
    181  1.1  mrg # Local Variables:
    182  1.1  mrg # mode: shell-script
    183  1.1  mrg # sh-indentation: 2
    184  1.1  mrg # eval: (add-hook 'before-save-hook 'time-stamp)
    185  1.1  mrg # time-stamp-start: "scriptversion="
    186  1.1  mrg # time-stamp-format: "%:y-%02m-%02d.%02H"
    187  1.1  mrg # time-stamp-time-zone: "UTC0"
    188  1.1  mrg # time-stamp-end: "; # UTC"
    189  1.1  mrg # End:
    190