PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/lib-python/2.7/compileall.py

https://bitbucket.org/varialus/jyjy
Python | 214 lines | 214 code | 0 blank | 0 comment | 6 complexity | 94f05d90a4f04534b4dd444ab5f251bb MD5 | raw file
  1. """Module/script to "compile" all .py files to .pyc (or .pyo) file.
  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: if given, purported directory name (this is the
  23. directory name that will show up in error messages)
  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, purported directory name (this is the
  58. directory name that will show up in error messages)
  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 "-l: don't recurse down"
  146. print "-f: force rebuild even if timestamps are up-to-date"
  147. print "-q: quiet operation"
  148. print "-d destdir: purported directory name for error messages"
  149. print " if no directory arguments, -l sys.path is assumed"
  150. print "-x regexp: skip files matching the regular expression regexp"
  151. print " the regexp is searched for in the full path of the file"
  152. print "-i list: expand list with its content (file and directory names)"
  153. sys.exit(2)
  154. maxlevels = 10
  155. ddir = None
  156. force = 0
  157. quiet = 0
  158. rx = None
  159. flist = None
  160. for o, a in opts:
  161. if o == '-l': maxlevels = 0
  162. if o == '-d': ddir = a
  163. if o == '-f': force = 1
  164. if o == '-q': quiet = 1
  165. if o == '-x':
  166. import re
  167. rx = re.compile(a)
  168. if o == '-i': flist = a
  169. if ddir:
  170. if len(args) != 1 and not os.path.isdir(args[0]):
  171. print "-d destdir require exactly one directory argument"
  172. sys.exit(2)
  173. success = 1
  174. try:
  175. if args or flist:
  176. try:
  177. if flist:
  178. args = expand_args(args, flist)
  179. except IOError:
  180. success = 0
  181. if success:
  182. for arg in args:
  183. if os.path.isdir(arg):
  184. if not compile_dir(arg, maxlevels, ddir,
  185. force, rx, quiet):
  186. success = 0
  187. else:
  188. if not compile_file(arg, ddir, force, rx, quiet):
  189. success = 0
  190. else:
  191. success = compile_path()
  192. except KeyboardInterrupt:
  193. print "\n[interrupt]"
  194. success = 0
  195. return success
  196. if __name__ == '__main__':
  197. exit_status = int(not main())
  198. sys.exit(exit_status)