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