1#!/bin/sh
2# py-compile - Compile a Python program
3
4scriptversion=2005-05-14.22
5
6# Copyright (C) 2000, 2001, 2003, 2004, 2005  Free Software Foundation, Inc.
7
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 3, or (at your option)
11# any later version.
12
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17
18# You should have received a copy of the GNU General Public License
19# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21# As a special exception to the GNU General Public License, if you
22# distribute this file as part of a program that contains a
23# configuration script generated by Autoconf, you may include it under
24# the same distribution terms that you use for the rest of that program.
25
26# This file is maintained in Automake, please report
27# bugs to <bug-automake@gnu.org> or send patches to
28# <automake-patches@gnu.org>.
29
30if [ -z "$PYTHON" ]; then
31  PYTHON=python
32fi
33
34basedir=
35destdir=
36files=
37while test $# -ne 0; do
38  case "$1" in
39    --basedir)
40      basedir=$2
41      if test -z "$basedir"; then
42        echo "$0: Missing argument to --basedir." 1>&2
43        exit 1
44      fi
45      shift
46      ;;
47    --destdir)
48      destdir=$2
49      if test -z "$destdir"; then
50        echo "$0: Missing argument to --destdir." 1>&2
51        exit 1
52      fi
53      shift
54      ;;
55    -h|--h*)
56      cat <<\EOF
57Usage: py-compile [--help] [--version] [--basedir DIR] [--destdir DIR] FILES..."
58
59Byte compile some python scripts FILES.  Use --destdir to specify any
60leading directory path to the FILES that you don't want to include in the
61byte compiled file.  Specify --basedir for any additional path information you
62do want to be shown in the byte compiled file.
63
64Example:
65  py-compile --destdir /tmp/pkg-root --basedir /usr/share/test test.py test2.py
66
67Report bugs to <bug-automake@gnu.org>.
68EOF
69      exit $?
70      ;;
71    -v|--v*)
72      echo "py-compile $scriptversion"
73      exit $?
74      ;;
75    *)
76      files="$files $1"
77      ;;
78  esac
79  shift
80done
81
82if test -z "$files"; then
83    echo "$0: No files given.  Try \`$0 --help' for more information." 1>&2
84    exit 1
85fi
86
87# if basedir was given, then it should be prepended to filenames before
88# byte compilation.
89if [ -z "$basedir" ]; then
90    pathtrans="path = file"
91else
92    pathtrans="path = os.path.join('$basedir', file)"
93fi
94
95# if destdir was given, then it needs to be prepended to the filename to
96# byte compile but not go into the compiled file.
97if [ -z "$destdir" ]; then
98    filetrans="filepath = path"
99else
100    filetrans="filepath = os.path.normpath('$destdir' + os.sep + path)"
101fi
102
103$PYTHON -c "
104import sys, os, string, py_compile
105
106files = '''$files'''
107
108print 'Byte-compiling python modules...'
109for file in string.split(files):
110    $pathtrans
111    $filetrans
112    if not os.path.exists(filepath) or not (len(filepath) >= 3
113                                            and filepath[-3:] == '.py'):
114	continue
115    print file,
116    sys.stdout.flush()
117    py_compile.compile(filepath, filepath + 'c', path)
118print" || exit $?
119
120# this will fail for python < 1.5, but that doesn't matter ...
121$PYTHON -O -c "
122import sys, os, string, py_compile
123
124files = '''$files'''
125print 'Byte-compiling python modules (optimized versions) ...'
126for file in string.split(files):
127    $pathtrans
128    $filetrans
129    if not os.path.exists(filepath) or not (len(filepath) >= 3
130                                            and filepath[-3:] == '.py'):
131	continue
132    print file,
133    sys.stdout.flush()
134    py_compile.compile(filepath, filepath + 'o', path)
135print" 2>/dev/null || :
136
137# Local Variables:
138# mode: shell-script
139# sh-indentation: 2
140# eval: (add-hook 'write-file-hooks 'time-stamp)
141# time-stamp-start: "scriptversion="
142# time-stamp-format: "%:y-%02m-%02d.%02H"
143# time-stamp-end: "$"
144# End:
145