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