/Lib/compileall.py

http://unladen-swallow.googlecode.com/ · Python · 157 lines · 128 code · 6 blank · 23 comment · 53 complexity · ae6e57adbbca3b335c7e31eb282a6559 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. __all__ = ["compile_dir","compile_path"]
  14. def compile_dir(dir, maxlevels=10, ddir=None,
  15. force=0, rx=None, quiet=0):
  16. """Byte-compile all modules in the given directory tree.
  17. Arguments (only dir is required):
  18. dir: the directory to byte-compile
  19. maxlevels: maximum recursion level (default 10)
  20. ddir: if given, purported directory name (this is the
  21. directory name that will show up in error messages)
  22. force: if 1, force compilation, even if timestamps are up-to-date
  23. quiet: if 1, be quiet during compilation
  24. """
  25. if not quiet:
  26. print 'Listing', dir, '...'
  27. try:
  28. names = os.listdir(dir)
  29. except os.error:
  30. print "Can't list", dir
  31. names = []
  32. names.sort()
  33. success = 1
  34. for name in names:
  35. fullname = os.path.join(dir, name)
  36. if ddir is not None:
  37. dfile = os.path.join(ddir, name)
  38. else:
  39. dfile = None
  40. if rx is not None:
  41. mo = rx.search(fullname)
  42. if mo:
  43. continue
  44. if os.path.isfile(fullname):
  45. head, tail = name[:-3], name[-3:]
  46. if tail == '.py':
  47. cfile = fullname + (__debug__ and 'c' or 'o')
  48. ftime = os.stat(fullname).st_mtime
  49. try: ctime = os.stat(cfile).st_mtime
  50. except os.error: ctime = 0
  51. if (ctime > ftime) and not force: continue
  52. if not quiet:
  53. print 'Compiling', fullname, '...'
  54. try:
  55. ok = py_compile.compile(fullname, None, dfile, True)
  56. except KeyboardInterrupt:
  57. raise KeyboardInterrupt
  58. except py_compile.PyCompileError,err:
  59. if quiet:
  60. print 'Compiling', fullname, '...'
  61. print err.msg
  62. success = 0
  63. except IOError, e:
  64. print "Sorry", e
  65. success = 0
  66. else:
  67. if ok == 0:
  68. success = 0
  69. elif maxlevels > 0 and \
  70. name != os.curdir and name != os.pardir and \
  71. os.path.isdir(fullname) and \
  72. not os.path.islink(fullname):
  73. if not compile_dir(fullname, maxlevels - 1, dfile, force, rx, quiet):
  74. success = 0
  75. return success
  76. def compile_path(skip_curdir=1, maxlevels=0, force=0, quiet=0):
  77. """Byte-compile all module on sys.path.
  78. Arguments (all optional):
  79. skip_curdir: if true, skip current directory (default true)
  80. maxlevels: max recursion level (default 0)
  81. force: as for compile_dir() (default 0)
  82. quiet: as for compile_dir() (default 0)
  83. """
  84. success = 1
  85. for dir in sys.path:
  86. if (not dir or dir == os.curdir) and skip_curdir:
  87. print 'Skipping current directory'
  88. else:
  89. success = success and compile_dir(dir, maxlevels, None,
  90. force, quiet=quiet)
  91. return success
  92. def main():
  93. """Script main program."""
  94. import getopt
  95. try:
  96. opts, args = getopt.getopt(sys.argv[1:], 'lfqd:x:')
  97. except getopt.error, msg:
  98. print msg
  99. print "usage: python compileall.py [-l] [-f] [-q] [-d destdir] " \
  100. "[-x regexp] [directory ...]"
  101. print "-l: don't recurse down"
  102. print "-f: force rebuild even if timestamps are up-to-date"
  103. print "-q: quiet operation"
  104. print "-d destdir: purported directory name for error messages"
  105. print " if no directory arguments, -l sys.path is assumed"
  106. print "-x regexp: skip files matching the regular expression regexp"
  107. print " the regexp is searched for in the full path of the file"
  108. sys.exit(2)
  109. maxlevels = 10
  110. ddir = None
  111. force = 0
  112. quiet = 0
  113. rx = None
  114. for o, a in opts:
  115. if o == '-l': maxlevels = 0
  116. if o == '-d': ddir = a
  117. if o == '-f': force = 1
  118. if o == '-q': quiet = 1
  119. if o == '-x':
  120. import re
  121. rx = re.compile(a)
  122. if ddir:
  123. if len(args) != 1:
  124. print "-d destdir require exactly one directory argument"
  125. sys.exit(2)
  126. success = 1
  127. try:
  128. if args:
  129. for dir in args:
  130. if not compile_dir(dir, maxlevels, ddir,
  131. force, rx, quiet):
  132. success = 0
  133. else:
  134. success = compile_path()
  135. except KeyboardInterrupt:
  136. print "\n[interrupt]"
  137. success = 0
  138. return success
  139. if __name__ == '__main__':
  140. exit_status = int(not main())
  141. sys.exit(exit_status)