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