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