You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

162 lines
4.3 KiB

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