py-compile revision f591e195
1d4401354Smrg#!/bin/sh 2d4401354Smrg# py-compile - Compile a Python program 3d4401354Smrg 4986c8b3dSmrgscriptversion=2011-06-08.12; # UTC 5d4401354Smrg 6f591e195Smrg# Copyright (C) 2000-2013 Free Software Foundation, Inc. 7d4401354Smrg 8d4401354Smrg# This program is free software; you can redistribute it and/or modify 9d4401354Smrg# it under the terms of the GNU General Public License as published by 10d4401354Smrg# the Free Software Foundation; either version 2, or (at your option) 11d4401354Smrg# any later version. 12d4401354Smrg 13d4401354Smrg# This program is distributed in the hope that it will be useful, 14d4401354Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of 15d4401354Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16d4401354Smrg# GNU General Public License for more details. 17d4401354Smrg 18d4401354Smrg# You should have received a copy of the GNU General Public License 19d4401354Smrg# along with this program. If not, see <http://www.gnu.org/licenses/>. 20d4401354Smrg 21d4401354Smrg# As a special exception to the GNU General Public License, if you 22d4401354Smrg# distribute this file as part of a program that contains a 23d4401354Smrg# configuration script generated by Autoconf, you may include it under 24d4401354Smrg# the same distribution terms that you use for the rest of that program. 25d4401354Smrg 26d4401354Smrg# This file is maintained in Automake, please report 27d4401354Smrg# bugs to <bug-automake@gnu.org> or send patches to 28d4401354Smrg# <automake-patches@gnu.org>. 29d4401354Smrg 30d4401354Smrgif [ -z "$PYTHON" ]; then 31d4401354Smrg PYTHON=python 32d4401354Smrgfi 33d4401354Smrg 34986c8b3dSmrgme=py-compile 35986c8b3dSmrg 36986c8b3dSmrgusage_error () 37986c8b3dSmrg{ 38986c8b3dSmrg echo "$me: $*" >&2 39f591e195Smrg echo "Try '$me --help' for more information." >&2 40986c8b3dSmrg exit 1 41986c8b3dSmrg} 42986c8b3dSmrg 43d4401354Smrgbasedir= 44d4401354Smrgdestdir= 45d4401354Smrgwhile test $# -ne 0; do 46d4401354Smrg case "$1" in 47d4401354Smrg --basedir) 48986c8b3dSmrg if test $# -lt 2; then 49986c8b3dSmrg usage_error "option '--basedir' requires an argument" 50986c8b3dSmrg else 51986c8b3dSmrg basedir=$2 52d4401354Smrg fi 53d4401354Smrg shift 54d4401354Smrg ;; 55d4401354Smrg --destdir) 56986c8b3dSmrg if test $# -lt 2; then 57986c8b3dSmrg usage_error "option '--destdir' requires an argument" 58986c8b3dSmrg else 59986c8b3dSmrg destdir=$2 60d4401354Smrg fi 61d4401354Smrg shift 62d4401354Smrg ;; 63986c8b3dSmrg -h|--help) 64d4401354Smrg cat <<\EOF 65d4401354SmrgUsage: py-compile [--help] [--version] [--basedir DIR] [--destdir DIR] FILES..." 66d4401354Smrg 67d4401354SmrgByte compile some python scripts FILES. Use --destdir to specify any 68d4401354Smrgleading directory path to the FILES that you don't want to include in the 69d4401354Smrgbyte compiled file. Specify --basedir for any additional path information you 70d4401354Smrgdo want to be shown in the byte compiled file. 71d4401354Smrg 72d4401354SmrgExample: 73d4401354Smrg py-compile --destdir /tmp/pkg-root --basedir /usr/share/test test.py test2.py 74d4401354Smrg 75d4401354SmrgReport bugs to <bug-automake@gnu.org>. 76d4401354SmrgEOF 77d4401354Smrg exit $? 78d4401354Smrg ;; 79986c8b3dSmrg -v|--version) 80986c8b3dSmrg echo "$me $scriptversion" 81d4401354Smrg exit $? 82d4401354Smrg ;; 83986c8b3dSmrg --) 84986c8b3dSmrg shift 85986c8b3dSmrg break 86986c8b3dSmrg ;; 87986c8b3dSmrg -*) 88986c8b3dSmrg usage_error "unrecognized option '$1'" 89986c8b3dSmrg ;; 90d4401354Smrg *) 91986c8b3dSmrg break 92d4401354Smrg ;; 93d4401354Smrg esac 94d4401354Smrg shift 95d4401354Smrgdone 96d4401354Smrg 97986c8b3dSmrgfiles=$* 98d4401354Smrgif test -z "$files"; then 99986c8b3dSmrg usage_error "no files given" 100d4401354Smrgfi 101d4401354Smrg 102d4401354Smrg# if basedir was given, then it should be prepended to filenames before 103d4401354Smrg# byte compilation. 104d4401354Smrgif [ -z "$basedir" ]; then 105d4401354Smrg pathtrans="path = file" 106d4401354Smrgelse 107d4401354Smrg pathtrans="path = os.path.join('$basedir', file)" 108d4401354Smrgfi 109d4401354Smrg 110d4401354Smrg# if destdir was given, then it needs to be prepended to the filename to 111d4401354Smrg# byte compile but not go into the compiled file. 112d4401354Smrgif [ -z "$destdir" ]; then 113d4401354Smrg filetrans="filepath = path" 114d4401354Smrgelse 115d4401354Smrg filetrans="filepath = os.path.normpath('$destdir' + os.sep + path)" 116d4401354Smrgfi 117d4401354Smrg 118d4401354Smrg$PYTHON -c " 119f591e195Smrgimport sys, os, py_compile, imp 120d4401354Smrg 121d4401354Smrgfiles = '''$files''' 122d4401354Smrg 123d4401354Smrgsys.stdout.write('Byte-compiling python modules...\n') 124d4401354Smrgfor file in files.split(): 125d4401354Smrg $pathtrans 126d4401354Smrg $filetrans 127d4401354Smrg if not os.path.exists(filepath) or not (len(filepath) >= 3 128d4401354Smrg and filepath[-3:] == '.py'): 129d4401354Smrg continue 130d4401354Smrg sys.stdout.write(file) 131d4401354Smrg sys.stdout.flush() 132f591e195Smrg if hasattr(imp, 'get_tag'): 133f591e195Smrg py_compile.compile(filepath, imp.cache_from_source(filepath), path) 134f591e195Smrg else: 135f591e195Smrg py_compile.compile(filepath, filepath + 'c', path) 136d4401354Smrgsys.stdout.write('\n')" || exit $? 137d4401354Smrg 138d4401354Smrg# this will fail for python < 1.5, but that doesn't matter ... 139d4401354Smrg$PYTHON -O -c " 140f591e195Smrgimport sys, os, py_compile, imp 141f591e195Smrg 142f591e195Smrg# pypy does not use .pyo optimization 143f591e195Smrgif hasattr(sys, 'pypy_translation_info'): 144f591e195Smrg sys.exit(0) 145d4401354Smrg 146d4401354Smrgfiles = '''$files''' 147d4401354Smrgsys.stdout.write('Byte-compiling python modules (optimized versions) ...\n') 148d4401354Smrgfor file in files.split(): 149d4401354Smrg $pathtrans 150d4401354Smrg $filetrans 151d4401354Smrg if not os.path.exists(filepath) or not (len(filepath) >= 3 152d4401354Smrg and filepath[-3:] == '.py'): 153d4401354Smrg continue 154d4401354Smrg sys.stdout.write(file) 155d4401354Smrg sys.stdout.flush() 156f591e195Smrg if hasattr(imp, 'get_tag'): 157f591e195Smrg py_compile.compile(filepath, imp.cache_from_source(filepath, False), path) 158f591e195Smrg else: 159f591e195Smrg py_compile.compile(filepath, filepath + 'o', path) 160d4401354Smrgsys.stdout.write('\n')" 2>/dev/null || : 161d4401354Smrg 162d4401354Smrg# Local Variables: 163d4401354Smrg# mode: shell-script 164d4401354Smrg# sh-indentation: 2 165d4401354Smrg# eval: (add-hook 'write-file-hooks 'time-stamp) 166d4401354Smrg# time-stamp-start: "scriptversion=" 167d4401354Smrg# time-stamp-format: "%:y-%02m-%02d.%02H" 168d4401354Smrg# time-stamp-time-zone: "UTC" 169d4401354Smrg# time-stamp-end: "; # UTC" 170d4401354Smrg# End: 171