Blame build-aux/py-compile

Packit 130fc8
#!/bin/sh
Packit 130fc8
# py-compile - Compile a Python program
Packit 130fc8
Packit 130fc8
scriptversion=2011-06-08.12; # UTC
Packit 130fc8
Packit 130fc8
# Copyright (C) 2000-2014 Free Software Foundation, Inc.
Packit 130fc8
Packit 130fc8
# This program is free software; you can redistribute it and/or modify
Packit 130fc8
# it under the terms of the GNU General Public License as published by
Packit 130fc8
# the Free Software Foundation; either version 2, or (at your option)
Packit 130fc8
# any later version.
Packit 130fc8
Packit 130fc8
# This program is distributed in the hope that it will be useful,
Packit 130fc8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 130fc8
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 130fc8
# GNU General Public License for more details.
Packit 130fc8
Packit 130fc8
# You should have received a copy of the GNU General Public License
Packit 130fc8
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 130fc8
Packit 130fc8
# As a special exception to the GNU General Public License, if you
Packit 130fc8
# distribute this file as part of a program that contains a
Packit 130fc8
# configuration script generated by Autoconf, you may include it under
Packit 130fc8
# the same distribution terms that you use for the rest of that program.
Packit 130fc8
Packit 130fc8
# This file is maintained in Automake, please report
Packit 130fc8
# bugs to <bug-automake@gnu.org> or send patches to
Packit 130fc8
# <automake-patches@gnu.org>.
Packit 130fc8
Packit 130fc8
if [ -z "$PYTHON" ]; then
Packit 130fc8
  PYTHON=python
Packit 130fc8
fi
Packit 130fc8
Packit 130fc8
me=py-compile
Packit 130fc8
Packit 130fc8
usage_error ()
Packit 130fc8
{
Packit 130fc8
  echo "$me: $*" >&2
Packit 130fc8
  echo "Try '$me --help' for more information." >&2
Packit 130fc8
  exit 1
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
basedir=
Packit 130fc8
destdir=
Packit 130fc8
while test $# -ne 0; do
Packit 130fc8
  case "$1" in
Packit 130fc8
    --basedir)
Packit 130fc8
      if test $# -lt 2; then
Packit 130fc8
        usage_error "option '--basedir' requires an argument"
Packit 130fc8
      else
Packit 130fc8
        basedir=$2
Packit 130fc8
      fi
Packit 130fc8
      shift
Packit 130fc8
      ;;
Packit 130fc8
    --destdir)
Packit 130fc8
      if test $# -lt 2; then
Packit 130fc8
        usage_error "option '--destdir' requires an argument"
Packit 130fc8
      else
Packit 130fc8
        destdir=$2
Packit 130fc8
      fi
Packit 130fc8
      shift
Packit 130fc8
      ;;
Packit 130fc8
    -h|--help)
Packit 130fc8
      cat <<\EOF
Packit 130fc8
Usage: py-compile [--help] [--version] [--basedir DIR] [--destdir DIR] FILES..."
Packit 130fc8
Packit 130fc8
Byte compile some python scripts FILES.  Use --destdir to specify any
Packit 130fc8
leading directory path to the FILES that you don't want to include in the
Packit 130fc8
byte compiled file.  Specify --basedir for any additional path information you
Packit 130fc8
do want to be shown in the byte compiled file.
Packit 130fc8
Packit 130fc8
Example:
Packit 130fc8
  py-compile --destdir /tmp/pkg-root --basedir /usr/share/test test.py test2.py
Packit 130fc8
Packit 130fc8
Report bugs to <bug-automake@gnu.org>.
Packit 130fc8
EOF
Packit 130fc8
      exit $?
Packit 130fc8
      ;;
Packit 130fc8
    -v|--version)
Packit 130fc8
      echo "$me $scriptversion"
Packit 130fc8
      exit $?
Packit 130fc8
      ;;
Packit 130fc8
    --)
Packit 130fc8
      shift
Packit 130fc8
      break
Packit 130fc8
      ;;
Packit 130fc8
    -*)
Packit 130fc8
      usage_error "unrecognized option '$1'"
Packit 130fc8
      ;;
Packit 130fc8
    *)
Packit 130fc8
      break
Packit 130fc8
      ;;
Packit 130fc8
  esac
Packit 130fc8
  shift
Packit 130fc8
done
Packit 130fc8
Packit 130fc8
files=$*
Packit 130fc8
if test -z "$files"; then
Packit 130fc8
    usage_error "no files given"
Packit 130fc8
fi
Packit 130fc8
Packit 130fc8
# if basedir was given, then it should be prepended to filenames before
Packit 130fc8
# byte compilation.
Packit 130fc8
if [ -z "$basedir" ]; then
Packit 130fc8
    pathtrans="path = file"
Packit 130fc8
else
Packit 130fc8
    pathtrans="path = os.path.join('$basedir', file)"
Packit 130fc8
fi
Packit 130fc8
Packit 130fc8
# if destdir was given, then it needs to be prepended to the filename to
Packit 130fc8
# byte compile but not go into the compiled file.
Packit 130fc8
if [ -z "$destdir" ]; then
Packit 130fc8
    filetrans="filepath = path"
Packit 130fc8
else
Packit 130fc8
    filetrans="filepath = os.path.normpath('$destdir' + os.sep + path)"
Packit 130fc8
fi
Packit 130fc8
Packit 130fc8
$PYTHON -c "
Packit 130fc8
import sys, os, py_compile, imp
Packit 130fc8
Packit 130fc8
files = '''$files'''
Packit 130fc8
Packit 130fc8
sys.stdout.write('Byte-compiling python modules...\n')
Packit 130fc8
for file in files.split():
Packit 130fc8
    $pathtrans
Packit 130fc8
    $filetrans
Packit 130fc8
    if not os.path.exists(filepath) or not (len(filepath) >= 3
Packit 130fc8
                                            and filepath[-3:] == '.py'):
Packit 130fc8
	    continue
Packit 130fc8
    sys.stdout.write(file)
Packit 130fc8
    sys.stdout.flush()
Packit 130fc8
    if hasattr(imp, 'get_tag'):
Packit 130fc8
        py_compile.compile(filepath, imp.cache_from_source(filepath), path)
Packit 130fc8
    else:
Packit 130fc8
        py_compile.compile(filepath, filepath + 'c', path)
Packit 130fc8
sys.stdout.write('\n')" || exit $?
Packit 130fc8
Packit 130fc8
# this will fail for python < 1.5, but that doesn't matter ...
Packit 130fc8
$PYTHON -O -c "
Packit 130fc8
import sys, os, py_compile, imp
Packit 130fc8
Packit 130fc8
# pypy does not use .pyo optimization
Packit 130fc8
if hasattr(sys, 'pypy_translation_info'):
Packit 130fc8
    sys.exit(0)
Packit 130fc8
Packit 130fc8
files = '''$files'''
Packit 130fc8
sys.stdout.write('Byte-compiling python modules (optimized versions) ...\n')
Packit 130fc8
for file in files.split():
Packit 130fc8
    $pathtrans
Packit 130fc8
    $filetrans
Packit 130fc8
    if not os.path.exists(filepath) or not (len(filepath) >= 3
Packit 130fc8
                                            and filepath[-3:] == '.py'):
Packit 130fc8
	    continue
Packit 130fc8
    sys.stdout.write(file)
Packit 130fc8
    sys.stdout.flush()
Packit 130fc8
    if hasattr(imp, 'get_tag'):
Packit 130fc8
        py_compile.compile(filepath, imp.cache_from_source(filepath, False), path)
Packit 130fc8
    else:
Packit 130fc8
        py_compile.compile(filepath, filepath + 'o', path)
Packit 130fc8
sys.stdout.write('\n')" 2>/dev/null || :
Packit 130fc8
Packit 130fc8
# Local Variables:
Packit 130fc8
# mode: shell-script
Packit 130fc8
# sh-indentation: 2
Packit 130fc8
# eval: (add-hook 'write-file-hooks 'time-stamp)
Packit 130fc8
# time-stamp-start: "scriptversion="
Packit 130fc8
# time-stamp-format: "%:y-%02m-%02d.%02H"
Packit 130fc8
# time-stamp-time-zone: "UTC"
Packit 130fc8
# time-stamp-end: "; # UTC"
Packit 130fc8
# End: