py-compile revision d5c9b07b
1d4401354Smrg#!/bin/sh
2d4401354Smrg# py-compile - Compile a Python program
3d4401354Smrg
4d5c9b07bSmrgscriptversion=2021-02-27.01; # UTC
5d4401354Smrg
6d5c9b07bSmrg# Copyright (C) 2000-2021 Free Software Foundation, Inc.
7d4401354Smrg
8d4401354Smrg# This program is free software; you can redistribute it and/or modify
9d4401354Smrg# it under the terms of the GNU General Public License as published by
10d4401354Smrg# the Free Software Foundation; either version 2, or (at your option)
11d4401354Smrg# any later version.
12d4401354Smrg
13d4401354Smrg# This program is distributed in the hope that it will be useful,
14d4401354Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
15d4401354Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16d4401354Smrg# GNU General Public License for more details.
17d4401354Smrg
18d4401354Smrg# You should have received a copy of the GNU General Public License
198d8e295fSmrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
20d4401354Smrg
21d4401354Smrg# As a special exception to the GNU General Public License, if you
22d4401354Smrg# distribute this file as part of a program that contains a
23d4401354Smrg# configuration script generated by Autoconf, you may include it under
24d4401354Smrg# the same distribution terms that you use for the rest of that program.
25d4401354Smrg
26d4401354Smrg# This file is maintained in Automake, please report
27d4401354Smrg# bugs to <bug-automake@gnu.org> or send patches to
28d4401354Smrg# <automake-patches@gnu.org>.
29d4401354Smrg
30d5c9b07bSmrgif test -z "$PYTHON"; then
31d4401354Smrg  PYTHON=python
32d4401354Smrgfi
33d4401354Smrg
34986c8b3dSmrgme=py-compile
35986c8b3dSmrg
36986c8b3dSmrgusage_error ()
37986c8b3dSmrg{
38986c8b3dSmrg  echo "$me: $*" >&2
39f591e195Smrg  echo "Try '$me --help' for more information." >&2
40986c8b3dSmrg  exit 1
41986c8b3dSmrg}
42986c8b3dSmrg
43d4401354Smrgbasedir=
44d4401354Smrgdestdir=
45d4401354Smrgwhile test $# -ne 0; do
46d4401354Smrg  case "$1" in
47d4401354Smrg    --basedir)
48986c8b3dSmrg      if test $# -lt 2; then
49986c8b3dSmrg        usage_error "option '--basedir' requires an argument"
50986c8b3dSmrg      else
51986c8b3dSmrg        basedir=$2
52d4401354Smrg      fi
53d4401354Smrg      shift
54d4401354Smrg      ;;
55d4401354Smrg    --destdir)
56986c8b3dSmrg      if test $# -lt 2; then
57986c8b3dSmrg        usage_error "option '--destdir' requires an argument"
58986c8b3dSmrg      else
59986c8b3dSmrg        destdir=$2
60d4401354Smrg      fi
61d4401354Smrg      shift
62d4401354Smrg      ;;
63986c8b3dSmrg    -h|--help)
64d4401354Smrg      cat <<\EOF
65d4401354SmrgUsage: py-compile [--help] [--version] [--basedir DIR] [--destdir DIR] FILES..."
66d4401354Smrg
67d4401354SmrgByte compile some python scripts FILES.  Use --destdir to specify any
68d4401354Smrgleading directory path to the FILES that you don't want to include in the
69d4401354Smrgbyte compiled file.  Specify --basedir for any additional path information you
70d4401354Smrgdo want to be shown in the byte compiled file.
71d4401354Smrg
72d4401354SmrgExample:
73d4401354Smrg  py-compile --destdir /tmp/pkg-root --basedir /usr/share/test test.py test2.py
74d4401354Smrg
75d4401354SmrgReport bugs to <bug-automake@gnu.org>.
76d4401354SmrgEOF
77d4401354Smrg      exit $?
78d4401354Smrg      ;;
79986c8b3dSmrg    -v|--version)
80986c8b3dSmrg      echo "$me $scriptversion"
81d4401354Smrg      exit $?
82d4401354Smrg      ;;
83986c8b3dSmrg    --)
84986c8b3dSmrg      shift
85986c8b3dSmrg      break
86986c8b3dSmrg      ;;
87986c8b3dSmrg    -*)
88986c8b3dSmrg      usage_error "unrecognized option '$1'"
89986c8b3dSmrg      ;;
90d4401354Smrg    *)
91986c8b3dSmrg      break
92d4401354Smrg      ;;
93d4401354Smrg  esac
94d4401354Smrg  shift
95d4401354Smrgdone
96d4401354Smrg
97986c8b3dSmrgfiles=$*
98d4401354Smrgif test -z "$files"; then
99d5c9b07bSmrg  usage_error "no files given"
100d4401354Smrgfi
101d4401354Smrg
102d4401354Smrg# if basedir was given, then it should be prepended to filenames before
103d4401354Smrg# byte compilation.
104d5c9b07bSmrgif test -z "$basedir"; then
105d5c9b07bSmrg  pathtrans="path = file"
106d4401354Smrgelse
107d5c9b07bSmrg  pathtrans="path = os.path.join('$basedir', file)"
108d4401354Smrgfi
109d4401354Smrg
110d4401354Smrg# if destdir was given, then it needs to be prepended to the filename to
111d4401354Smrg# byte compile but not go into the compiled file.
112d5c9b07bSmrgif test -z "$destdir"; then
113d5c9b07bSmrg  filetrans="filepath = path"
114d4401354Smrgelse
115d5c9b07bSmrg  filetrans="filepath = os.path.normpath('$destdir' + os.sep + path)"
116d5c9b07bSmrgfi
117d5c9b07bSmrg
118d5c9b07bSmrgpython_major=`$PYTHON -V 2>&1 | sed -e 's/.* //;s/\..*$//;1q'`
119d5c9b07bSmrgif test -z "$python_major"; then
120d5c9b07bSmrg  echo "$me: could not determine $PYTHON major version, guessing 3" >&2
121d5c9b07bSmrg  python_major=3
122d5c9b07bSmrgfi
123d5c9b07bSmrg
124d5c9b07bSmrg# The old way to import libraries was deprecated.
125d5c9b07bSmrgif test "$python_major" -le 2; then
126d5c9b07bSmrg  import_lib=imp
127d5c9b07bSmrg  import_test="hasattr(imp, 'get_tag')"
128d5c9b07bSmrg  import_call=imp.cache_from_source
129d5c9b07bSmrgelse
130d5c9b07bSmrg  import_lib=importlib
131d5c9b07bSmrg  import_test="hasattr(sys.implementation, 'cache_tag')"
132d5c9b07bSmrg  import_call=importlib.util.cache_from_source
133d4401354Smrgfi
134d4401354Smrg
135d4401354Smrg$PYTHON -c "
136d5c9b07bSmrgimport sys, os, py_compile, $import_lib
137d4401354Smrg
138d4401354Smrgfiles = '''$files'''
139d4401354Smrg
140d4401354Smrgsys.stdout.write('Byte-compiling python modules...\n')
141d4401354Smrgfor file in files.split():
142d4401354Smrg    $pathtrans
143d4401354Smrg    $filetrans
144d4401354Smrg    if not os.path.exists(filepath) or not (len(filepath) >= 3
145d4401354Smrg                                            and filepath[-3:] == '.py'):
146d4401354Smrg	    continue
147d4401354Smrg    sys.stdout.write(file)
148d4401354Smrg    sys.stdout.flush()
149d5c9b07bSmrg    if $import_test:
150d5c9b07bSmrg        py_compile.compile(filepath, $import_call(filepath), path)
151f591e195Smrg    else:
152f591e195Smrg        py_compile.compile(filepath, filepath + 'c', path)
153d4401354Smrgsys.stdout.write('\n')" || exit $?
154d4401354Smrg
155d4401354Smrg# this will fail for python < 1.5, but that doesn't matter ...
156d4401354Smrg$PYTHON -O -c "
157d5c9b07bSmrgimport sys, os, py_compile, $import_lib
158f591e195Smrg
159f591e195Smrg# pypy does not use .pyo optimization
160d5c9b07bSmrgif hasattr(sys, 'pypy_translation_info') and sys.hexversion < 0x03050000:
161f591e195Smrg    sys.exit(0)
162d4401354Smrg
163d4401354Smrgfiles = '''$files'''
164d4401354Smrgsys.stdout.write('Byte-compiling python modules (optimized versions) ...\n')
165d4401354Smrgfor file in files.split():
166d4401354Smrg    $pathtrans
167d4401354Smrg    $filetrans
168d4401354Smrg    if not os.path.exists(filepath) or not (len(filepath) >= 3
169d4401354Smrg                                            and filepath[-3:] == '.py'):
170d4401354Smrg	    continue
171d4401354Smrg    sys.stdout.write(file)
172d4401354Smrg    sys.stdout.flush()
173d5c9b07bSmrg    if $import_test:
174d5c9b07bSmrg        py_compile.compile(filepath, $import_call(filepath), path)
175f591e195Smrg    else:
176f591e195Smrg        py_compile.compile(filepath, filepath + 'o', path)
177d4401354Smrgsys.stdout.write('\n')" 2>/dev/null || :
178d4401354Smrg
179d5c9b07bSmrg$PYTHON -OO -c "
180d5c9b07bSmrgimport sys, os, py_compile, $import_lib
181d5c9b07bSmrg
182d5c9b07bSmrg# python<3.5 does not have split files for -O and -OO
183d5c9b07bSmrgif sys.hexversion < 0x03050000:
184d5c9b07bSmrg    sys.exit(0)
185d5c9b07bSmrg
186d5c9b07bSmrgfiles = '''$files'''
187d5c9b07bSmrgsys.stdout.write('Byte-compiling python modules (optimized versions) ...\n')
188d5c9b07bSmrgfor file in files.split():
189d5c9b07bSmrg    $pathtrans
190d5c9b07bSmrg    $filetrans
191d5c9b07bSmrg    if not os.path.exists(filepath) or not (len(filepath) >= 3
192d5c9b07bSmrg                                            and filepath[-3:] == '.py'):
193d5c9b07bSmrg	    continue
194d5c9b07bSmrg    sys.stdout.write(file)
195d5c9b07bSmrg    sys.stdout.flush()
196d5c9b07bSmrg    if $import_test:
197d5c9b07bSmrg        py_compile.compile(filepath, $import_call(filepath), path)
198d5c9b07bSmrg    else:
199d5c9b07bSmrg        py_compile.compile(filepath, filepath + 'o', path)
200d5c9b07bSmrgsys.stdout.write('\n')" 2>/dev/null || exit $?
201d5c9b07bSmrg
202d4401354Smrg# Local Variables:
203d4401354Smrg# mode: shell-script
204d4401354Smrg# sh-indentation: 2
2058d8e295fSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
206d4401354Smrg# time-stamp-start: "scriptversion="
207d4401354Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
208a27842ffSmrg# time-stamp-time-zone: "UTC0"
209d4401354Smrg# time-stamp-end: "; # UTC"
210d4401354Smrg# End:
211