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() |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 39 | success = 1 |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 40 | for name in names: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 41 | fullname = os.path.join(dir, name) |
| 42 | if ddir: |
| 43 | dfile = os.path.join(ddir, name) |
| 44 | else: |
| 45 | dfile = None |
| 46 | if os.path.isfile(fullname): |
| 47 | head, tail = name[:-3], name[-3:] |
| 48 | if tail == '.py': |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 49 | cfile = fullname + (__debug__ and 'c' or 'o') |
| 50 | ftime = os.stat(fullname)[stat.ST_MTIME] |
| 51 | try: ctime = os.stat(cfile)[stat.ST_MTIME] |
| 52 | except os.error: ctime = 0 |
| 53 | if (ctime > ftime) and not force: continue |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 54 | print 'Compiling', fullname, '...' |
| 55 | try: |
| 56 | py_compile.compile(fullname, None, dfile) |
| 57 | except KeyboardInterrupt: |
| 58 | raise KeyboardInterrupt |
| 59 | except: |
| 60 | if type(sys.exc_type) == type(''): |
| 61 | exc_type_name = sys.exc_type |
| 62 | else: exc_type_name = sys.exc_type.__name__ |
| 63 | print 'Sorry:', exc_type_name + ':', |
| 64 | print sys.exc_value |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 65 | success = 0 |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 66 | elif maxlevels > 0 and \ |
| 67 | name != os.curdir and name != os.pardir and \ |
| 68 | os.path.isdir(fullname) and \ |
| 69 | not os.path.islink(fullname): |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 70 | compile_dir(fullname, maxlevels - 1, dfile, force) |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 71 | return success |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 72 | |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 73 | def compile_path(skip_curdir=1, maxlevels=0, force=0): |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 74 | """Byte-compile all module on sys.path. |
| 75 | |
| 76 | Arguments (all optional): |
| 77 | |
| 78 | skip_curdir: if true, skip current directory (default true) |
| 79 | maxlevels: max recursion level (default 0) |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 80 | force: as for compile_dir() (default 0) |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 81 | |
| 82 | """ |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 83 | success = 1 |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 84 | for dir in sys.path: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 85 | if (not dir or dir == os.curdir) and skip_curdir: |
| 86 | print 'Skipping current directory' |
| 87 | else: |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 88 | success = success and compile_dir(dir, maxlevels, None, force) |
| 89 | return success |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 90 | |
| 91 | def main(): |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 92 | """Script main program.""" |
| 93 | import getopt |
| 94 | try: |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 95 | opts, args = getopt.getopt(sys.argv[1:], 'lfd:') |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 96 | except getopt.error, msg: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 97 | print msg |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 98 | print "usage: compileall [-l] [-f] [-d destdir] [directory ...]" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 99 | print "-l: don't recurse down" |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 100 | print "-f: force rebuild even if timestamps are up-to-date" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 101 | print "-d destdir: purported directory name for error messages" |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 102 | print "if no directory arguments, -l sys.path is assumed" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 103 | sys.exit(2) |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 104 | maxlevels = 10 |
| 105 | ddir = None |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 106 | force = 0 |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 107 | for o, a in opts: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 108 | if o == '-l': maxlevels = 0 |
| 109 | if o == '-d': ddir = a |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 110 | if o == '-f': force = 1 |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 111 | if ddir: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 112 | if len(args) != 1: |
| 113 | print "-d destdir require exactly one directory argument" |
| 114 | sys.exit(2) |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 115 | success = 1 |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 116 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 117 | if args: |
| 118 | for dir in args: |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 119 | success = success and compile_dir(dir, maxlevels, ddir, force) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 120 | else: |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 121 | success = compile_path() |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 122 | except KeyboardInterrupt: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 123 | print "\n[interrupt]" |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 124 | success = 0 |
| 125 | return success |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 126 | |
| 127 | if __name__ == '__main__': |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 128 | sys.exit(not main()) |