PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/Python/system/compileall.py

https://bitbucket.org/cwalther/moulscript-dlanor
Python | 155 lines | 155 code | 0 blank | 0 comment | 4 complexity | 47ac283c32ff3ae3cc82bc66a94dabee MD5 | raw file
Possible License(s): AGPL-1.0, GPL-3.0
  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. print err.msg
  60. success = 0
  61. except IOError, e:
  62. print "Sorry", e
  63. success = 0
  64. else:
  65. if ok == 0:
  66. success = 0
  67. elif maxlevels > 0 and \
  68. name != os.curdir and name != os.pardir and \
  69. os.path.isdir(fullname) and \
  70. not os.path.islink(fullname):
  71. if not compile_dir(fullname, maxlevels - 1, dfile, force, rx, quiet):
  72. success = 0
  73. return success
  74. def compile_path(skip_curdir=1, maxlevels=0, force=0, quiet=0):
  75. """Byte-compile all module on sys.path.
  76. Arguments (all optional):
  77. skip_curdir: if true, skip current directory (default true)
  78. maxlevels: max recursion level (default 0)
  79. force: as for compile_dir() (default 0)
  80. quiet: as for compile_dir() (default 0)
  81. """
  82. success = 1
  83. for dir in sys.path:
  84. if (not dir or dir == os.curdir) and skip_curdir:
  85. print 'Skipping current directory'
  86. else:
  87. success = success and compile_dir(dir, maxlevels, None,
  88. force, quiet=quiet)
  89. return success
  90. def main():
  91. """Script main program."""
  92. import getopt
  93. try:
  94. opts, args = getopt.getopt(sys.argv[1:], 'lfqd:x:')
  95. except getopt.error, msg:
  96. print msg
  97. print "usage: python compileall.py [-l] [-f] [-q] [-d destdir] " \
  98. "[-x regexp] [directory ...]"
  99. print "-l: don't recurse down"
  100. print "-f: force rebuild even if timestamps are up-to-date"
  101. print "-q: quiet operation"
  102. print "-d destdir: purported directory name for error messages"
  103. print " if no directory arguments, -l sys.path is assumed"
  104. print "-x regexp: skip files matching the regular expression regexp"
  105. print " the regexp is search for in the full path of the file"
  106. sys.exit(2)
  107. maxlevels = 10
  108. ddir = None
  109. force = 0
  110. quiet = 0
  111. rx = None
  112. for o, a in opts:
  113. if o == '-l': maxlevels = 0
  114. if o == '-d': ddir = a
  115. if o == '-f': force = 1
  116. if o == '-q': quiet = 1
  117. if o == '-x':
  118. import re
  119. rx = re.compile(a)
  120. if ddir:
  121. if len(args) != 1:
  122. print "-d destdir require exactly one directory argument"
  123. sys.exit(2)
  124. success = 1
  125. try:
  126. if args:
  127. for dir in args:
  128. if not compile_dir(dir, maxlevels, ddir,
  129. force, rx, quiet):
  130. success = 0
  131. else:
  132. success = compile_path()
  133. except KeyboardInterrupt:
  134. print "\n[interrupt]"
  135. success = 0
  136. return success
  137. if __name__ == '__main__':
  138. exit_status = not main()
  139. sys.exit(exit_status)