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 | import os |
| 15 | import sys |
| 16 | import py_compile |
Brett Cannon | 28d1088 | 2009-02-10 02:07:38 +0000 | [diff] [blame] | 17 | import struct |
| 18 | import imp |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 19 | |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 20 | __all__ = ["compile_dir","compile_path"] |
| 21 | |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 22 | def compile_dir(dir, maxlevels=10, ddir=None, |
| 23 | force=0, rx=None, quiet=0): |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 24 | """Byte-compile all modules in the given directory tree. |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 26 | Arguments (only dir is required): |
| 27 | |
| 28 | dir: the directory to byte-compile |
| 29 | maxlevels: maximum recursion level (default 10) |
| 30 | ddir: if given, purported directory name (this is the |
| 31 | directory name that will show up in error messages) |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 32 | force: if 1, force compilation, even if timestamps are up-to-date |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 33 | quiet: if 1, be quiet during compilation |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 34 | |
| 35 | """ |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 36 | if not quiet: |
| 37 | print 'Listing', dir, '...' |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 38 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 39 | names = os.listdir(dir) |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 40 | except os.error: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 41 | print "Can't list", dir |
| 42 | names = [] |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 43 | names.sort() |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 44 | success = 1 |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 45 | for name in names: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 46 | fullname = os.path.join(dir, name) |
Raymond Hettinger | 8989ea6 | 2002-06-01 00:06:20 +0000 | [diff] [blame] | 47 | if ddir is not None: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 48 | dfile = os.path.join(ddir, name) |
| 49 | else: |
| 50 | dfile = None |
Raymond Hettinger | 8989ea6 | 2002-06-01 00:06:20 +0000 | [diff] [blame] | 51 | if rx is not None: |
Jeremy Hylton | 12b6457 | 2001-04-18 01:20:21 +0000 | [diff] [blame] | 52 | mo = rx.search(fullname) |
| 53 | if mo: |
| 54 | continue |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 55 | if os.path.isfile(fullname): |
| 56 | head, tail = name[:-3], name[-3:] |
| 57 | if tail == '.py': |
Brett Cannon | 28d1088 | 2009-02-10 02:07:38 +0000 | [diff] [blame] | 58 | if not force: |
| 59 | try: |
| 60 | mtime = os.stat(fullname).st_mtime |
| 61 | expect = struct.pack('<4sl', imp.get_magic(), mtime) |
| 62 | cfile = fullname + (__debug__ and 'c' or 'o') |
| 63 | with open(cfile, 'rb') as chandle: |
| 64 | actual = chandle.read(8) |
| 65 | if expect == actual: |
| 66 | continue |
| 67 | except IOError: |
| 68 | pass |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 69 | if not quiet: |
| 70 | print 'Compiling', fullname, '...' |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 71 | try: |
Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame] | 72 | ok = py_compile.compile(fullname, None, dfile, True) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 73 | except KeyboardInterrupt: |
| 74 | raise KeyboardInterrupt |
Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame] | 75 | except py_compile.PyCompileError,err: |
Martin v. Löwis | 873a277 | 2004-06-20 20:59:56 +0000 | [diff] [blame] | 76 | if quiet: |
| 77 | print 'Compiling', fullname, '...' |
Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame] | 78 | print err.msg |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 79 | success = 0 |
Martin v. Löwis | 6f2adc7 | 2003-01-16 11:02:43 +0000 | [diff] [blame] | 80 | except IOError, e: |
| 81 | print "Sorry", e |
| 82 | success = 0 |
Jeremy Hylton | 12b6457 | 2001-04-18 01:20:21 +0000 | [diff] [blame] | 83 | else: |
| 84 | if ok == 0: |
| 85 | success = 0 |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 86 | elif maxlevels > 0 and \ |
| 87 | name != os.curdir and name != os.pardir and \ |
| 88 | os.path.isdir(fullname) and \ |
| 89 | not os.path.islink(fullname): |
Brett Cannon | 28d1088 | 2009-02-10 02:07:38 +0000 | [diff] [blame] | 90 | if not compile_dir(fullname, maxlevels - 1, dfile, force, rx, |
| 91 | quiet): |
Jeremy Hylton | 12b6457 | 2001-04-18 01:20:21 +0000 | [diff] [blame] | 92 | success = 0 |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 93 | return success |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 94 | |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 95 | def compile_path(skip_curdir=1, maxlevels=0, force=0, quiet=0): |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 96 | """Byte-compile all module on sys.path. |
| 97 | |
| 98 | Arguments (all optional): |
| 99 | |
| 100 | skip_curdir: if true, skip current directory (default true) |
| 101 | maxlevels: max recursion level (default 0) |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 102 | force: as for compile_dir() (default 0) |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 103 | quiet: as for compile_dir() (default 0) |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 104 | |
| 105 | """ |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 106 | success = 1 |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 107 | for dir in sys.path: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 108 | if (not dir or dir == os.curdir) and skip_curdir: |
| 109 | print 'Skipping current directory' |
| 110 | else: |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 111 | success = success and compile_dir(dir, maxlevels, None, |
| 112 | force, quiet=quiet) |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 113 | return success |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 114 | |
| 115 | def main(): |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 116 | """Script main program.""" |
| 117 | import getopt |
| 118 | try: |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 119 | opts, args = getopt.getopt(sys.argv[1:], 'lfqd:x:') |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 120 | except getopt.error, msg: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 121 | print msg |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 122 | print "usage: python compileall.py [-l] [-f] [-q] [-d destdir] " \ |
Thomas Heller | 4651f53 | 2003-08-27 20:19:35 +0000 | [diff] [blame] | 123 | "[-x regexp] [directory ...]" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 124 | print "-l: don't recurse down" |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 125 | print "-f: force rebuild even if timestamps are up-to-date" |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 126 | print "-q: quiet operation" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 127 | print "-d destdir: purported directory name for error messages" |
Jeremy Hylton | 12b6457 | 2001-04-18 01:20:21 +0000 | [diff] [blame] | 128 | print " if no directory arguments, -l sys.path is assumed" |
| 129 | print "-x regexp: skip files matching the regular expression regexp" |
Fred Drake | a28df13 | 2008-03-04 21:14:04 +0000 | [diff] [blame] | 130 | print " the regexp is searched for in the full path of the file" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 131 | sys.exit(2) |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 132 | maxlevels = 10 |
| 133 | ddir = None |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 134 | force = 0 |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 135 | quiet = 0 |
Jeremy Hylton | 12b6457 | 2001-04-18 01:20:21 +0000 | [diff] [blame] | 136 | rx = None |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 137 | for o, a in opts: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 138 | if o == '-l': maxlevels = 0 |
| 139 | if o == '-d': ddir = a |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 140 | if o == '-f': force = 1 |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 141 | if o == '-q': quiet = 1 |
Jeremy Hylton | 12b6457 | 2001-04-18 01:20:21 +0000 | [diff] [blame] | 142 | if o == '-x': |
| 143 | import re |
| 144 | rx = re.compile(a) |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 145 | if ddir: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 146 | if len(args) != 1: |
| 147 | print "-d destdir require exactly one directory argument" |
| 148 | sys.exit(2) |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 149 | success = 1 |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 150 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 151 | if args: |
| 152 | for dir in args: |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 153 | if not compile_dir(dir, maxlevels, ddir, |
| 154 | force, rx, quiet): |
Jeremy Hylton | 12b6457 | 2001-04-18 01:20:21 +0000 | [diff] [blame] | 155 | success = 0 |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 156 | else: |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 157 | success = compile_path() |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 158 | except KeyboardInterrupt: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 159 | print "\n[interrupt]" |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 160 | success = 0 |
| 161 | return success |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 162 | |
| 163 | if __name__ == '__main__': |
Raymond Hettinger | 7b4b788 | 2004-12-20 00:29:29 +0000 | [diff] [blame] | 164 | exit_status = int(not main()) |
Jeremy Hylton | 12b6457 | 2001-04-18 01:20:21 +0000 | [diff] [blame] | 165 | sys.exit(exit_status) |