py-compile 5.1 KB

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