Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 1 | """Module/script to "compile" all .py files to .pyc (or .pyo) file. |
| 2 | |
| 3 | When called as a script with arguments, this compiles the directories |
| 4 | given as arguments recursively; the -l option prevents it from |
| 5 | recursing into directories. |
| 6 | |
| 7 | Without arguments, if compiles all modules on sys.path, without |
| 8 | recursing into subdirectories. (Even though it should do so for |
| 9 | packages -- for now, you'll have to deal with packages separately.) |
| 10 | |
| 11 | See module py_compile for details of the actual byte-compilation. |
| 12 | |
| 13 | """ |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 14 | |
| 15 | import os |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 16 | import stat |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 17 | import sys |
| 18 | import py_compile |
| 19 | |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 20 | def compile_dir(dir, maxlevels=10, ddir=None, force=0): |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 21 | """Byte-compile all modules in the given directory tree. |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 22 | |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 23 | Arguments (only dir is required): |
| 24 | |
| 25 | dir: the directory to byte-compile |
| 26 | maxlevels: maximum recursion level (default 10) |
| 27 | ddir: if given, purported directory name (this is the |
| 28 | directory name that will show up in error messages) |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 29 | force: if 1, force compilation, even if timestamps are up-to-date |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 30 | |
| 31 | """ |
| 32 | print 'Listing', dir, '...' |
| 33 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 34 | names = os.listdir(dir) |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 35 | except os.error: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 36 | print "Can't list", dir |
| 37 | names = [] |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 38 | names.sort() |
| 39 | for name in names: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 40 | fullname = os.path.join(dir, name) |
| 41 | if ddir: |
| 42 | dfile = os.path.join(ddir, name) |
| 43 | else: |
| 44 | dfile = None |
| 45 | if os.path.isfile(fullname): |
| 46 | head, tail = name[:-3], name[-3:] |
| 47 | if tail == '.py': |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 48 | cfile = fullname + (__debug__ and 'c' or 'o') |
| 49 | ftime = os.stat(fullname)[stat.ST_MTIME] |
| 50 | try: ctime = os.stat(cfile)[stat.ST_MTIME] |
| 51 | except os.error: ctime = 0 |
| 52 | if (ctime > ftime) and not force: continue |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 53 | print 'Compiling', fullname, '...' |
| 54 | try: |
| 55 | py_compile.compile(fullname, None, dfile) |
| 56 | except KeyboardInterrupt: |
| 57 | raise KeyboardInterrupt |
| 58 | except: |
| 59 | if type(sys.exc_type) == type(''): |
| 60 | exc_type_name = sys.exc_type |
| 61 | else: exc_type_name = sys.exc_type.__name__ |
| 62 | print 'Sorry:', exc_type_name + ':', |
| 63 | print sys.exc_value |
| 64 | elif maxlevels > 0 and \ |
| 65 | name != os.curdir and name != os.pardir and \ |
| 66 | os.path.isdir(fullname) and \ |
| 67 | not os.path.islink(fullname): |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 68 | compile_dir(fullname, maxlevels - 1, dfile, force) |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 69 | |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 70 | def compile_path(skip_curdir=1, maxlevels=0, force=0): |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 71 | """Byte-compile all module on sys.path. |
| 72 | |
| 73 | Arguments (all optional): |
| 74 | |
| 75 | skip_curdir: if true, skip current directory (default true) |
| 76 | maxlevels: max recursion level (default 0) |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 77 | force: as for compile_dir() (default 0) |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 78 | |
| 79 | """ |
| 80 | for dir in sys.path: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 81 | if (not dir or dir == os.curdir) and skip_curdir: |
| 82 | print 'Skipping current directory' |
| 83 | else: |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 84 | compile_dir(dir, maxlevels, None, force) |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 85 | |
| 86 | def main(): |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 87 | """Script main program.""" |
| 88 | import getopt |
| 89 | try: |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 90 | opts, args = getopt.getopt(sys.argv[1:], 'lfd:') |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 91 | except getopt.error, msg: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 92 | print msg |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 93 | print "usage: compileall [-l] [-f] [-d destdir] [directory ...]" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 94 | print "-l: don't recurse down" |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 95 | print "-f: force rebuild even if timestamps are up-to-date" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 96 | print "-d destdir: purported directory name for error messages" |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 97 | print "if no directory arguments, -l sys.path is assumed" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 98 | sys.exit(2) |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 99 | maxlevels = 10 |
| 100 | ddir = None |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 101 | force = 0 |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 102 | for o, a in opts: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 103 | if o == '-l': maxlevels = 0 |
| 104 | if o == '-d': ddir = a |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 105 | if o == '-f': force = 1 |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 106 | if ddir: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 107 | if len(args) != 1: |
| 108 | print "-d destdir require exactly one directory argument" |
| 109 | sys.exit(2) |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 110 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 111 | if args: |
| 112 | for dir in args: |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 113 | compile_dir(dir, maxlevels, ddir, force) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 114 | else: |
| 115 | compile_path() |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 116 | except KeyboardInterrupt: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 117 | print "\n[interrupt]" |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 118 | |
| 119 | if __name__ == '__main__': |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 120 | main() |