Blame py-compile

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