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.

compileall.py 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 stat
  12. import sys
  13. import py_compile
  14. __all__ = ["compile_dir","compile_path"]
  15. def compile_dir(dir, maxlevels=10, ddir=None, force=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. """
  24. print 'Listing', dir, '...'
  25. try:
  26. names = os.listdir(dir)
  27. except os.error:
  28. print "Can't list", dir
  29. names = []
  30. names.sort()
  31. success = 1
  32. for name in names:
  33. fullname = os.path.join(dir, name)
  34. if ddir:
  35. dfile = os.path.join(ddir, name)
  36. else:
  37. dfile = None
  38. if os.path.isfile(fullname):
  39. head, tail = name[:-3], name[-3:]
  40. if tail == '.py':
  41. cfile = fullname + (__debug__ and 'c' or 'o')
  42. ftime = os.stat(fullname)[stat.ST_MTIME]
  43. try: ctime = os.stat(cfile)[stat.ST_MTIME]
  44. except os.error: ctime = 0
  45. if (ctime > ftime) and not force: continue
  46. print 'Compiling', fullname, '...'
  47. try:
  48. py_compile.compile(fullname, None, dfile)
  49. except KeyboardInterrupt:
  50. raise KeyboardInterrupt
  51. except:
  52. if type(sys.exc_type) == type(''):
  53. exc_type_name = sys.exc_type
  54. else: exc_type_name = sys.exc_type.__name__
  55. print 'Sorry:', exc_type_name + ':',
  56. print sys.exc_value
  57. success = 0
  58. elif maxlevels > 0 and \
  59. name != os.curdir and name != os.pardir and \
  60. os.path.isdir(fullname) and \
  61. not os.path.islink(fullname):
  62. compile_dir(fullname, maxlevels - 1, dfile, force)
  63. return success
  64. def compile_path(skip_curdir=1, maxlevels=0, force=0):
  65. """Byte-compile all module on sys.path.
  66. Arguments (all optional):
  67. skip_curdir: if true, skip current directory (default true)
  68. maxlevels: max recursion level (default 0)
  69. force: as for compile_dir() (default 0)
  70. """
  71. success = 1
  72. for dir in sys.path:
  73. if (not dir or dir == os.curdir) and skip_curdir:
  74. print 'Skipping current directory'
  75. else:
  76. success = success and compile_dir(dir, maxlevels, None, force)
  77. return success
  78. def main():
  79. """Script main program."""
  80. import getopt
  81. try:
  82. opts, args = getopt.getopt(sys.argv[1:], 'lfd:')
  83. except getopt.error, msg:
  84. print msg
  85. print "usage: compileall [-l] [-f] [-d destdir] [directory ...]"
  86. print "-l: don't recurse down"
  87. print "-f: force rebuild even if timestamps are up-to-date"
  88. print "-d destdir: purported directory name for error messages"
  89. print "if no directory arguments, -l sys.path is assumed"
  90. sys.exit(2)
  91. maxlevels = 10
  92. ddir = None
  93. force = 0
  94. for o, a in opts:
  95. if o == '-l': maxlevels = 0
  96. if o == '-d': ddir = a
  97. if o == '-f': force = 1
  98. if ddir:
  99. if len(args) != 1:
  100. print "-d destdir require exactly one directory argument"
  101. sys.exit(2)
  102. success = 1
  103. try:
  104. if args:
  105. for dir in args:
  106. success = success and compile_dir(dir, maxlevels, ddir, force)
  107. else:
  108. success = compile_path()
  109. except KeyboardInterrupt:
  110. print "\n[interrupt]"
  111. success = 0
  112. return success
  113. if __name__ == '__main__':
  114. sys.exit(not main())