Blame py-compile

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