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