py-compile revision d4401354
1d4401354Smrg#!/bin/sh
2d4401354Smrg# py-compile - Compile a Python program
3d4401354Smrg
4d4401354Smrgscriptversion=2009-04-28.21; # UTC
5d4401354Smrg
6d4401354Smrg# Copyright (C) 2000, 2001, 2003, 2004, 2005, 2008, 2009 Free Software
7d4401354Smrg# 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
35d4401354Smrgbasedir=
36d4401354Smrgdestdir=
37d4401354Smrgfiles=
38d4401354Smrgwhile test $# -ne 0; do
39d4401354Smrg  case "$1" in
40d4401354Smrg    --basedir)
41d4401354Smrg      basedir=$2
42d4401354Smrg      if test -z "$basedir"; then
43d4401354Smrg        echo "$0: Missing argument to --basedir." 1>&2
44d4401354Smrg        exit 1
45d4401354Smrg      fi
46d4401354Smrg      shift
47d4401354Smrg      ;;
48d4401354Smrg    --destdir)
49d4401354Smrg      destdir=$2
50d4401354Smrg      if test -z "$destdir"; then
51d4401354Smrg        echo "$0: Missing argument to --destdir." 1>&2
52d4401354Smrg        exit 1
53d4401354Smrg      fi
54d4401354Smrg      shift
55d4401354Smrg      ;;
56d4401354Smrg    -h|--h*)
57d4401354Smrg      cat <<\EOF
58d4401354SmrgUsage: py-compile [--help] [--version] [--basedir DIR] [--destdir DIR] FILES..."
59d4401354Smrg
60d4401354SmrgByte compile some python scripts FILES.  Use --destdir to specify any
61d4401354Smrgleading directory path to the FILES that you don't want to include in the
62d4401354Smrgbyte compiled file.  Specify --basedir for any additional path information you
63d4401354Smrgdo want to be shown in the byte compiled file.
64d4401354Smrg
65d4401354SmrgExample:
66d4401354Smrg  py-compile --destdir /tmp/pkg-root --basedir /usr/share/test test.py test2.py
67d4401354Smrg
68d4401354SmrgReport bugs to <bug-automake@gnu.org>.
69d4401354SmrgEOF
70d4401354Smrg      exit $?
71d4401354Smrg      ;;
72d4401354Smrg    -v|--v*)
73d4401354Smrg      echo "py-compile $scriptversion"
74d4401354Smrg      exit $?
75d4401354Smrg      ;;
76d4401354Smrg    *)
77d4401354Smrg      files="$files $1"
78d4401354Smrg      ;;
79d4401354Smrg  esac
80d4401354Smrg  shift
81d4401354Smrgdone
82d4401354Smrg
83d4401354Smrgif test -z "$files"; then
84d4401354Smrg    echo "$0: No files given.  Try \`$0 --help' for more information." 1>&2
85d4401354Smrg    exit 1
86d4401354Smrgfi
87d4401354Smrg
88d4401354Smrg# if basedir was given, then it should be prepended to filenames before
89d4401354Smrg# byte compilation.
90d4401354Smrgif [ -z "$basedir" ]; then
91d4401354Smrg    pathtrans="path = file"
92d4401354Smrgelse
93d4401354Smrg    pathtrans="path = os.path.join('$basedir', file)"
94d4401354Smrgfi
95d4401354Smrg
96d4401354Smrg# if destdir was given, then it needs to be prepended to the filename to
97d4401354Smrg# byte compile but not go into the compiled file.
98d4401354Smrgif [ -z "$destdir" ]; then
99d4401354Smrg    filetrans="filepath = path"
100d4401354Smrgelse
101d4401354Smrg    filetrans="filepath = os.path.normpath('$destdir' + os.sep + path)"
102d4401354Smrgfi
103d4401354Smrg
104d4401354Smrg$PYTHON -c "
105d4401354Smrgimport sys, os, py_compile
106d4401354Smrg
107d4401354Smrgfiles = '''$files'''
108d4401354Smrg
109d4401354Smrgsys.stdout.write('Byte-compiling python modules...\n')
110d4401354Smrgfor file in files.split():
111d4401354Smrg    $pathtrans
112d4401354Smrg    $filetrans
113d4401354Smrg    if not os.path.exists(filepath) or not (len(filepath) >= 3
114d4401354Smrg                                            and filepath[-3:] == '.py'):
115d4401354Smrg	    continue
116d4401354Smrg    sys.stdout.write(file)
117d4401354Smrg    sys.stdout.flush()
118d4401354Smrg    py_compile.compile(filepath, filepath + 'c', path)
119d4401354Smrgsys.stdout.write('\n')" || exit $?
120d4401354Smrg
121d4401354Smrg# this will fail for python < 1.5, but that doesn't matter ...
122d4401354Smrg$PYTHON -O -c "
123d4401354Smrgimport sys, os, py_compile
124d4401354Smrg
125d4401354Smrgfiles = '''$files'''
126d4401354Smrgsys.stdout.write('Byte-compiling python modules (optimized versions) ...\n')
127d4401354Smrgfor file in files.split():
128d4401354Smrg    $pathtrans
129d4401354Smrg    $filetrans
130d4401354Smrg    if not os.path.exists(filepath) or not (len(filepath) >= 3
131d4401354Smrg                                            and filepath[-3:] == '.py'):
132d4401354Smrg	    continue
133d4401354Smrg    sys.stdout.write(file)
134d4401354Smrg    sys.stdout.flush()
135d4401354Smrg    py_compile.compile(filepath, filepath + 'o', path)
136d4401354Smrgsys.stdout.write('\n')" 2>/dev/null || :
137d4401354Smrg
138d4401354Smrg# Local Variables:
139d4401354Smrg# mode: shell-script
140d4401354Smrg# sh-indentation: 2
141d4401354Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
142d4401354Smrg# time-stamp-start: "scriptversion="
143d4401354Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
144d4401354Smrg# time-stamp-time-zone: "UTC"
145d4401354Smrg# time-stamp-end: "; # UTC"
146d4401354Smrg# End:
147