xref: /netbsd-src/external/mit/isl/dist/py-compile (revision 5971e316fdea024efff6be8f03536623db06833e)
1*5971e316Smrg#!/bin/sh
2*5971e316Smrg# py-compile - Compile a Python program
3*5971e316Smrg
4*5971e316Smrgscriptversion=2021-02-27.01; # UTC
5*5971e316Smrg
6*5971e316Smrg# Copyright (C) 2000-2021 Free Software Foundation, Inc.
7*5971e316Smrg
8*5971e316Smrg# This program is free software; you can redistribute it and/or modify
9*5971e316Smrg# it under the terms of the GNU General Public License as published by
10*5971e316Smrg# the Free Software Foundation; either version 2, or (at your option)
11*5971e316Smrg# any later version.
12*5971e316Smrg
13*5971e316Smrg# This program is distributed in the hope that it will be useful,
14*5971e316Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
15*5971e316Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*5971e316Smrg# GNU General Public License for more details.
17*5971e316Smrg
18*5971e316Smrg# You should have received a copy of the GNU General Public License
19*5971e316Smrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
20*5971e316Smrg
21*5971e316Smrg# As a special exception to the GNU General Public License, if you
22*5971e316Smrg# distribute this file as part of a program that contains a
23*5971e316Smrg# configuration script generated by Autoconf, you may include it under
24*5971e316Smrg# the same distribution terms that you use for the rest of that program.
25*5971e316Smrg
26*5971e316Smrg# This file is maintained in Automake, please report
27*5971e316Smrg# bugs to <bug-automake@gnu.org> or send patches to
28*5971e316Smrg# <automake-patches@gnu.org>.
29*5971e316Smrg
30*5971e316Smrgif test -z "$PYTHON"; then
31*5971e316Smrg  PYTHON=python
32*5971e316Smrgfi
33*5971e316Smrg
34*5971e316Smrgme=py-compile
35*5971e316Smrg
36*5971e316Smrgusage_error ()
37*5971e316Smrg{
38*5971e316Smrg  echo "$me: $*" >&2
39*5971e316Smrg  echo "Try '$me --help' for more information." >&2
40*5971e316Smrg  exit 1
41*5971e316Smrg}
42*5971e316Smrg
43*5971e316Smrgbasedir=
44*5971e316Smrgdestdir=
45*5971e316Smrgwhile test $# -ne 0; do
46*5971e316Smrg  case "$1" in
47*5971e316Smrg    --basedir)
48*5971e316Smrg      if test $# -lt 2; then
49*5971e316Smrg        usage_error "option '--basedir' requires an argument"
50*5971e316Smrg      else
51*5971e316Smrg        basedir=$2
52*5971e316Smrg      fi
53*5971e316Smrg      shift
54*5971e316Smrg      ;;
55*5971e316Smrg    --destdir)
56*5971e316Smrg      if test $# -lt 2; then
57*5971e316Smrg        usage_error "option '--destdir' requires an argument"
58*5971e316Smrg      else
59*5971e316Smrg        destdir=$2
60*5971e316Smrg      fi
61*5971e316Smrg      shift
62*5971e316Smrg      ;;
63*5971e316Smrg    -h|--help)
64*5971e316Smrg      cat <<\EOF
65*5971e316SmrgUsage: py-compile [--help] [--version] [--basedir DIR] [--destdir DIR] FILES..."
66*5971e316Smrg
67*5971e316SmrgByte compile some python scripts FILES.  Use --destdir to specify any
68*5971e316Smrgleading directory path to the FILES that you don't want to include in the
69*5971e316Smrgbyte compiled file.  Specify --basedir for any additional path information you
70*5971e316Smrgdo want to be shown in the byte compiled file.
71*5971e316Smrg
72*5971e316SmrgExample:
73*5971e316Smrg  py-compile --destdir /tmp/pkg-root --basedir /usr/share/test test.py test2.py
74*5971e316Smrg
75*5971e316SmrgReport bugs to <bug-automake@gnu.org>.
76*5971e316SmrgEOF
77*5971e316Smrg      exit $?
78*5971e316Smrg      ;;
79*5971e316Smrg    -v|--version)
80*5971e316Smrg      echo "$me $scriptversion"
81*5971e316Smrg      exit $?
82*5971e316Smrg      ;;
83*5971e316Smrg    --)
84*5971e316Smrg      shift
85*5971e316Smrg      break
86*5971e316Smrg      ;;
87*5971e316Smrg    -*)
88*5971e316Smrg      usage_error "unrecognized option '$1'"
89*5971e316Smrg      ;;
90*5971e316Smrg    *)
91*5971e316Smrg      break
92*5971e316Smrg      ;;
93*5971e316Smrg  esac
94*5971e316Smrg  shift
95*5971e316Smrgdone
96*5971e316Smrg
97*5971e316Smrgfiles=$*
98*5971e316Smrgif test -z "$files"; then
99*5971e316Smrg  usage_error "no files given"
100*5971e316Smrgfi
101*5971e316Smrg
102*5971e316Smrg# if basedir was given, then it should be prepended to filenames before
103*5971e316Smrg# byte compilation.
104*5971e316Smrgif test -z "$basedir"; then
105*5971e316Smrg  pathtrans="path = file"
106*5971e316Smrgelse
107*5971e316Smrg  pathtrans="path = os.path.join('$basedir', file)"
108*5971e316Smrgfi
109*5971e316Smrg
110*5971e316Smrg# if destdir was given, then it needs to be prepended to the filename to
111*5971e316Smrg# byte compile but not go into the compiled file.
112*5971e316Smrgif test -z "$destdir"; then
113*5971e316Smrg  filetrans="filepath = path"
114*5971e316Smrgelse
115*5971e316Smrg  filetrans="filepath = os.path.normpath('$destdir' + os.sep + path)"
116*5971e316Smrgfi
117*5971e316Smrg
118*5971e316Smrgpython_major=`$PYTHON -V 2>&1 | sed -e 's/.* //;s/\..*$//;1q'`
119*5971e316Smrgif test -z "$python_major"; then
120*5971e316Smrg  echo "$me: could not determine $PYTHON major version, guessing 3" >&2
121*5971e316Smrg  python_major=3
122*5971e316Smrgfi
123*5971e316Smrg
124*5971e316Smrg# The old way to import libraries was deprecated.
125*5971e316Smrgif test "$python_major" -le 2; then
126*5971e316Smrg  import_lib=imp
127*5971e316Smrg  import_test="hasattr(imp, 'get_tag')"
128*5971e316Smrg  import_call=imp.cache_from_source
129*5971e316Smrg  import_arg2=', False' # needed in one call and not the other
130*5971e316Smrgelse
131*5971e316Smrg  import_lib=importlib
132*5971e316Smrg  import_test="hasattr(sys.implementation, 'cache_tag')"
133*5971e316Smrg  import_call=importlib.util.cache_from_source
134*5971e316Smrg  import_arg2=
135*5971e316Smrgfi
136*5971e316Smrg
137*5971e316Smrg$PYTHON -c "
138*5971e316Smrgimport sys, os, py_compile, $import_lib
139*5971e316Smrg
140*5971e316Smrgfiles = '''$files'''
141*5971e316Smrg
142*5971e316Smrgsys.stdout.write('Byte-compiling python modules...\n')
143*5971e316Smrgfor file in files.split():
144*5971e316Smrg    $pathtrans
145*5971e316Smrg    $filetrans
146*5971e316Smrg    if not os.path.exists(filepath) or not (len(filepath) >= 3
147*5971e316Smrg                                            and filepath[-3:] == '.py'):
148*5971e316Smrg	    continue
149*5971e316Smrg    sys.stdout.write(file)
150*5971e316Smrg    sys.stdout.flush()
151*5971e316Smrg    if $import_test:
152*5971e316Smrg        py_compile.compile(filepath, $import_call(filepath), path)
153*5971e316Smrg    else:
154*5971e316Smrg        py_compile.compile(filepath, filepath + 'c', path)
155*5971e316Smrgsys.stdout.write('\n')" || exit $?
156*5971e316Smrg
157*5971e316Smrg# this will fail for python < 1.5, but that doesn't matter ...
158*5971e316Smrg$PYTHON -O -c "
159*5971e316Smrgimport sys, os, py_compile, $import_lib
160*5971e316Smrg
161*5971e316Smrg# pypy does not use .pyo optimization
162*5971e316Smrgif hasattr(sys, 'pypy_translation_info'):
163*5971e316Smrg    sys.exit(0)
164*5971e316Smrg
165*5971e316Smrgfiles = '''$files'''
166*5971e316Smrgsys.stdout.write('Byte-compiling python modules (optimized versions) ...\n')
167*5971e316Smrgfor file in files.split():
168*5971e316Smrg    $pathtrans
169*5971e316Smrg    $filetrans
170*5971e316Smrg    if not os.path.exists(filepath) or not (len(filepath) >= 3
171*5971e316Smrg                                            and filepath[-3:] == '.py'):
172*5971e316Smrg	    continue
173*5971e316Smrg    sys.stdout.write(file)
174*5971e316Smrg    sys.stdout.flush()
175*5971e316Smrg    if $import_test:
176*5971e316Smrg        py_compile.compile(filepath, $import_call(filepath$import_arg2), path)
177*5971e316Smrg    else:
178*5971e316Smrg        py_compile.compile(filepath, filepath + 'o', path)
179*5971e316Smrgsys.stdout.write('\n')" 2>/dev/null || exit $?
180*5971e316Smrg
181*5971e316Smrg# Local Variables:
182*5971e316Smrg# mode: shell-script
183*5971e316Smrg# sh-indentation: 2
184*5971e316Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
185*5971e316Smrg# time-stamp-start: "scriptversion="
186*5971e316Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
187*5971e316Smrg# time-stamp-time-zone: "UTC0"
188*5971e316Smrg# time-stamp-end: "; # UTC"
189*5971e316Smrg# End:
190