compileall.py revision 6f2adc7cd07783f5eaff3ce64a1ae466bd516e46
1"""Module/script to "compile" all .py files to .pyc (or .pyo) file. 2 3When called as a script with arguments, this compiles the directories 4given as arguments recursively; the -l option prevents it from 5recursing into directories. 6 7Without arguments, if compiles all modules on sys.path, without 8recursing into subdirectories. (Even though it should do so for 9packages -- for now, you'll have to deal with packages separately.) 10 11See module py_compile for details of the actual byte-compilation. 12 13""" 14 15import os 16import sys 17import py_compile 18 19__all__ = ["compile_dir","compile_path"] 20 21def compile_dir(dir, maxlevels=10, ddir=None, 22 force=0, rx=None, quiet=0): 23 """Byte-compile all modules in the given directory tree. 24 25 Arguments (only dir is required): 26 27 dir: the directory to byte-compile 28 maxlevels: maximum recursion level (default 10) 29 ddir: if given, purported directory name (this is the 30 directory name that will show up in error messages) 31 force: if 1, force compilation, even if timestamps are up-to-date 32 quiet: if 1, be quiet during compilation 33 34 """ 35 if not quiet: 36 print 'Listing', dir, '...' 37 try: 38 names = os.listdir(dir) 39 except os.error: 40 print "Can't list", dir 41 names = [] 42 names.sort() 43 success = 1 44 for name in names: 45 fullname = os.path.join(dir, name) 46 if ddir is not None: 47 dfile = os.path.join(ddir, name) 48 else: 49 dfile = None 50 if rx is not None: 51 mo = rx.search(fullname) 52 if mo: 53 continue 54 if os.path.isfile(fullname): 55 head, tail = name[:-3], name[-3:] 56 if tail == '.py': 57 cfile = fullname + (__debug__ and 'c' or 'o') 58 ftime = os.stat(fullname).st_mtime 59 try: ctime = os.stat(cfile).st_mtime 60 except os.error: ctime = 0 61 if (ctime > ftime) and not force: continue 62 if not quiet: 63 print 'Compiling', fullname, '...' 64 try: 65 ok = py_compile.compile(fullname, None, dfile, True) 66 except KeyboardInterrupt: 67 raise KeyboardInterrupt 68 except py_compile.PyCompileError,err: 69 print err.msg 70 success = 0 71 except IOError, e: 72 print "Sorry", e 73 success = 0 74 else: 75 if ok == 0: 76 success = 0 77 elif maxlevels > 0 and \ 78 name != os.curdir and name != os.pardir and \ 79 os.path.isdir(fullname) and \ 80 not os.path.islink(fullname): 81 if not compile_dir(fullname, maxlevels - 1, dfile, force, rx, quiet): 82 success = 0 83 return success 84 85def compile_path(skip_curdir=1, maxlevels=0, force=0, quiet=0): 86 """Byte-compile all module on sys.path. 87 88 Arguments (all optional): 89 90 skip_curdir: if true, skip current directory (default true) 91 maxlevels: max recursion level (default 0) 92 force: as for compile_dir() (default 0) 93 quiet: as for compile_dir() (default 0) 94 95 """ 96 success = 1 97 for dir in sys.path: 98 if (not dir or dir == os.curdir) and skip_curdir: 99 print 'Skipping current directory' 100 else: 101 success = success and compile_dir(dir, maxlevels, None, 102 force, quiet=quiet) 103 return success 104 105def main(): 106 """Script main program.""" 107 import getopt 108 try: 109 opts, args = getopt.getopt(sys.argv[1:], 'lfqd:x:') 110 except getopt.error, msg: 111 print msg 112 print "usage: python compileall.py [-l] [-f] [-q] [-d destdir] " \ 113 "[-s regexp] [directory ...]" 114 print "-l: don't recurse down" 115 print "-f: force rebuild even if timestamps are up-to-date" 116 print "-q: quiet operation" 117 print "-d destdir: purported directory name for error messages" 118 print " if no directory arguments, -l sys.path is assumed" 119 print "-x regexp: skip files matching the regular expression regexp" 120 print " the regexp is search for in the full path of the file" 121 sys.exit(2) 122 maxlevels = 10 123 ddir = None 124 force = 0 125 quiet = 0 126 rx = None 127 for o, a in opts: 128 if o == '-l': maxlevels = 0 129 if o == '-d': ddir = a 130 if o == '-f': force = 1 131 if o == '-q': quiet = 1 132 if o == '-x': 133 import re 134 rx = re.compile(a) 135 if ddir: 136 if len(args) != 1: 137 print "-d destdir require exactly one directory argument" 138 sys.exit(2) 139 success = 1 140 try: 141 if args: 142 for dir in args: 143 if not compile_dir(dir, maxlevels, ddir, 144 force, rx, quiet): 145 success = 0 146 else: 147 success = compile_path() 148 except KeyboardInterrupt: 149 print "\n[interrupt]" 150 success = 0 151 return success 152 153if __name__ == '__main__': 154 exit_status = not main() 155 sys.exit(exit_status) 156