PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib-python/2.7/compileall.py

https://bitbucket.org/bwesterb/pypy
Python | 227 lines | 211 code | 1 blank | 15 comment | 7 complexity | 4f73989ca7e8d90e9bedb20703acb4ee MD5 | raw file
  1. """Module/script to byte-compile all .py files to .pyc (or .pyo) files.
  2. When called as a script with arguments, this compiles the directories
  3. given as arguments recursively; the -l option prevents it from
  4. recursing into directories.
  5. Without arguments, if compiles all modules on sys.path, without
  6. recursing into subdirectories. (Even though it should do so for
  7. packages -- for now, you'll have to deal with packages separately.)
  8. See module py_compile for details of the actual byte-compilation.
  9. """
  10. import os
  11. import sys
  12. import py_compile
  13. import struct
  14. import imp
  15. __all__ = ["compile_dir","compile_file","compile_path"]
  16. def compile_dir(dir, maxlevels=10, ddir=None,
  17. force=0, rx=None, quiet=0):
  18. """Byte-compile all modules in the given directory tree.
  19. Arguments (only dir is required):
  20. dir: the directory to byte-compile
  21. maxlevels: maximum recursion level (default 10)
  22. ddir: the directory that will be prepended to the path to the
  23. file as it is compiled into each byte-code file.
  24. force: if 1, force compilation, even if timestamps are up-to-date
  25. quiet: if 1, be quiet during compilation
  26. """
  27. if not quiet:
  28. print 'Listing', dir, '...'
  29. try:
  30. names = os.listdir(dir)
  31. except os.error:
  32. print "Can't list", dir
  33. names = []
  34. names.sort()
  35. success = 1
  36. for name in names:
  37. fullname = os.path.join(dir, name)
  38. if ddir is not None:
  39. dfile = os.path.join(ddir, name)
  40. else:
  41. dfile = None
  42. if not os.path.isdir(fullname):
  43. if not compile_file(fullname, ddir, force, rx, quiet):
  44. success = 0
  45. elif maxlevels > 0 and \
  46. name != os.curdir and name != os.pardir and \
  47. os.path.isdir(fullname) and \
  48. not os.path.islink(fullname):
  49. if not compile_dir(fullname, maxlevels - 1, dfile, force, rx,
  50. quiet):
  51. success = 0
  52. return success
  53. def compile_file(fullname, ddir=None, force=0, rx=None, quiet=0):
  54. """Byte-compile one file.
  55. Arguments (only fullname is required):
  56. fullname: the file to byte-compile
  57. ddir: if given, the directory name compiled in to the
  58. byte-code file.
  59. force: if 1, force compilation, even if timestamps are up-to-date
  60. quiet: if 1, be quiet during compilation
  61. """
  62. success = 1
  63. name = os.path.basename(fullname)
  64. if ddir is not None:
  65. dfile = os.path.join(ddir, name)
  66. else:
  67. dfile = None
  68. if rx is not None:
  69. mo = rx.search(fullname)
  70. if mo:
  71. return success
  72. if os.path.isfile(fullname):
  73. head, tail = name[:-3], name[-3:]
  74. if tail == '.py':
  75. if not force:
  76. try:
  77. mtime = int(os.stat(fullname).st_mtime)
  78. expect = struct.pack('<4sl', imp.get_magic(), mtime)
  79. cfile = fullname + (__debug__ and 'c' or 'o')
  80. with open(cfile, 'rb') as chandle:
  81. actual = chandle.read(8)
  82. if expect == actual:
  83. return success
  84. except IOError:
  85. pass
  86. if not quiet:
  87. print 'Compiling', fullname, '...'
  88. try:
  89. ok = py_compile.compile(fullname, None, dfile, True)
  90. except py_compile.PyCompileError,err:
  91. if quiet:
  92. print 'Compiling', fullname, '...'
  93. print err.msg
  94. success = 0
  95. except IOError, e:
  96. print "Sorry", e
  97. success = 0
  98. else:
  99. if ok == 0:
  100. success = 0
  101. return success
  102. def compile_path(skip_curdir=1, maxlevels=0, force=0, quiet=0):
  103. """Byte-compile all module on sys.path.
  104. Arguments (all optional):
  105. skip_curdir: if true, skip current directory (default true)
  106. maxlevels: max recursion level (default 0)
  107. force: as for compile_dir() (default 0)
  108. quiet: as for compile_dir() (default 0)
  109. """
  110. success = 1
  111. for dir in sys.path:
  112. if (not dir or dir == os.curdir) and skip_curdir:
  113. print 'Skipping current directory'
  114. else:
  115. success = success and compile_dir(dir, maxlevels, None,
  116. force, quiet=quiet)
  117. return success
  118. def expand_args(args, flist):
  119. """read names in flist and append to args"""
  120. expanded = args[:]
  121. if flist:
  122. try:
  123. if flist == '-':
  124. fd = sys.stdin
  125. else:
  126. fd = open(flist)
  127. while 1:
  128. line = fd.readline()
  129. if not line:
  130. break
  131. expanded.append(line[:-1])
  132. except IOError:
  133. print "Error reading file list %s" % flist
  134. raise
  135. return expanded
  136. def main():
  137. """Script main program."""
  138. import getopt
  139. try:
  140. opts, args = getopt.getopt(sys.argv[1:], 'lfqd:x:i:')
  141. except getopt.error, msg:
  142. print msg
  143. print "usage: python compileall.py [-l] [-f] [-q] [-d destdir] " \
  144. "[-x regexp] [-i list] [directory|file ...]"
  145. print
  146. print "arguments: zero or more file and directory names to compile; " \
  147. "if no arguments given, "
  148. print " defaults to the equivalent of -l sys.path"
  149. print
  150. print "options:"
  151. print "-l: don't recurse into subdirectories"
  152. print "-f: force rebuild even if timestamps are up-to-date"
  153. print "-q: output only error messages"
  154. print "-d destdir: directory to prepend to file paths for use in " \
  155. "compile-time tracebacks and in"
  156. print " runtime tracebacks in cases where the source " \
  157. "file is unavailable"
  158. print "-x regexp: skip files matching the regular expression regexp; " \
  159. "the regexp is searched for"
  160. print " in the full path of each file considered for " \
  161. "compilation"
  162. print "-i file: add all the files and directories listed in file to " \
  163. "the list considered for"
  164. print ' compilation; if "-", names are read from stdin'
  165. sys.exit(2)
  166. maxlevels = 10
  167. ddir = None
  168. force = 0
  169. quiet = 0
  170. rx = None
  171. flist = None
  172. for o, a in opts:
  173. if o == '-l': maxlevels = 0
  174. if o == '-d': ddir = a
  175. if o == '-f': force = 1
  176. if o == '-q': quiet = 1
  177. if o == '-x':
  178. import re
  179. rx = re.compile(a)
  180. if o == '-i': flist = a
  181. if ddir:
  182. if len(args) != 1 and not os.path.isdir(args[0]):
  183. print "-d destdir require exactly one directory argument"
  184. sys.exit(2)
  185. success = 1
  186. try:
  187. if args or flist:
  188. try:
  189. if flist:
  190. args = expand_args(args, flist)
  191. except IOError:
  192. success = 0
  193. if success:
  194. for arg in args:
  195. if os.path.isdir(arg):
  196. if not compile_dir(arg, maxlevels, ddir,
  197. force, rx, quiet):
  198. success = 0
  199. else:
  200. if not compile_file(arg, ddir, force, rx, quiet):
  201. success = 0
  202. else:
  203. success = compile_path()
  204. except KeyboardInterrupt:
  205. print "\n[interrupted]"
  206. success = 0
  207. return success
  208. if __name__ == '__main__':
  209. exit_status = int(not main())
  210. sys.exit(exit_status)