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