py-compile 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/bin/sh
  2. # py-compile - Compile a Python program
  3. scriptversion=2011-06-08.12; # UTC
  4. # Copyright (C) 2000-2013 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 <http://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 [ -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 [ -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 [ -z "$destdir" ]; then
  96. filetrans="filepath = path"
  97. else
  98. filetrans="filepath = os.path.normpath('$destdir' + os.sep + path)"
  99. fi
  100. $PYTHON -c "
  101. import sys, os, py_compile, imp
  102. files = '''$files'''
  103. sys.stdout.write('Byte-compiling python modules...\n')
  104. for file in files.split():
  105. $pathtrans
  106. $filetrans
  107. if not os.path.exists(filepath) or not (len(filepath) >= 3
  108. and filepath[-3:] == '.py'):
  109. continue
  110. sys.stdout.write(file)
  111. sys.stdout.flush()
  112. if hasattr(imp, 'get_tag'):
  113. py_compile.compile(filepath, imp.cache_from_source(filepath), path)
  114. else:
  115. py_compile.compile(filepath, filepath + 'c', path)
  116. sys.stdout.write('\n')" || exit $?
  117. # this will fail for python < 1.5, but that doesn't matter ...
  118. $PYTHON -O -c "
  119. import sys, os, py_compile, imp
  120. # pypy does not use .pyo optimization
  121. if hasattr(sys, 'pypy_translation_info'):
  122. sys.exit(0)
  123. files = '''$files'''
  124. sys.stdout.write('Byte-compiling python modules (optimized versions) ...\n')
  125. for 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. if hasattr(imp, 'get_tag'):
  134. py_compile.compile(filepath, imp.cache_from_source(filepath, False), path)
  135. else:
  136. py_compile.compile(filepath, filepath + 'o', path)
  137. sys.stdout.write('\n')" 2>/dev/null || :
  138. # Local Variables:
  139. # mode: shell-script
  140. # sh-indentation: 2
  141. # eval: (add-hook 'write-file-hooks 'time-stamp)
  142. # time-stamp-start: "scriptversion="
  143. # time-stamp-format: "%:y-%02m-%02d.%02H"
  144. # time-stamp-time-zone: "UTC"
  145. # time-stamp-end: "; # UTC"
  146. # End: