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 |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 15 | import errno |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 16 | import sys |
| 17 | import py_compile |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 18 | import struct |
| 19 | import imp |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 20 | |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 21 | __all__ = ["compile_dir","compile_file","compile_path"] |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 22 | |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 23 | def compile_dir(dir, maxlevels=10, ddir=None, |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 24 | force=False, rx=None, quiet=False, legacy=False): |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 25 | """Byte-compile all modules in the given directory tree. |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 27 | Arguments (only dir is required): |
| 28 | |
| 29 | dir: the directory to byte-compile |
| 30 | maxlevels: maximum recursion level (default 10) |
| 31 | ddir: if given, purported directory name (this is the |
| 32 | directory name that will show up in error messages) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 33 | force: if True, force compilation, even if timestamps are up-to-date |
| 34 | quiet: if True, be quiet during compilation |
| 35 | legacy: if True, produce legacy pyc paths instead of PEP 3147 paths |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 36 | |
| 37 | """ |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 38 | if not quiet: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 39 | print('Listing', dir, '...') |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 40 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 41 | names = os.listdir(dir) |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 42 | except os.error: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 43 | print("Can't list", dir) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 44 | names = [] |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 45 | names.sort() |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 46 | success = 1 |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 47 | for name in names: |
Barry Warsaw | c04317f | 2010-04-26 15:59:03 +0000 | [diff] [blame^] | 48 | if name == '__pycache__': |
| 49 | continue |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 50 | fullname = os.path.join(dir, name) |
Raymond Hettinger | 8989ea6 | 2002-06-01 00:06:20 +0000 | [diff] [blame] | 51 | if ddir is not None: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 52 | dfile = os.path.join(ddir, name) |
| 53 | else: |
| 54 | dfile = None |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 55 | if not os.path.isdir(fullname): |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 56 | if not compile_file(fullname, ddir, force, rx, quiet, legacy): |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 57 | success = 0 |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 58 | elif maxlevels > 0 and \ |
| 59 | name != os.curdir and name != os.pardir and \ |
| 60 | os.path.isdir(fullname) and \ |
| 61 | not os.path.islink(fullname): |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 62 | if not compile_dir(fullname, maxlevels - 1, dfile, force, rx, |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 63 | quiet, legacy): |
Jeremy Hylton | 12b6457 | 2001-04-18 01:20:21 +0000 | [diff] [blame] | 64 | success = 0 |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 65 | return success |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 66 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 67 | def compile_file(fullname, ddir=None, force=0, rx=None, quiet=False, |
| 68 | legacy=False): |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 69 | """Byte-compile file. |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 70 | fullname: the file to byte-compile |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 71 | ddir: if given, purported directory name (this is the |
| 72 | directory name that will show up in error messages) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 73 | force: if True, force compilation, even if timestamps are up-to-date |
| 74 | quiet: if True, be quiet during compilation |
| 75 | legacy: if True, produce legacy pyc paths instead of PEP 3147 paths |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 76 | |
| 77 | """ |
| 78 | success = 1 |
| 79 | name = os.path.basename(fullname) |
| 80 | if ddir is not None: |
| 81 | dfile = os.path.join(ddir, name) |
| 82 | else: |
| 83 | dfile = None |
| 84 | if rx is not None: |
| 85 | mo = rx.search(fullname) |
| 86 | if mo: |
| 87 | return success |
| 88 | if os.path.isfile(fullname): |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 89 | if legacy: |
| 90 | cfile = fullname + ('c' if __debug__ else 'o') |
| 91 | else: |
| 92 | cfile = imp.cache_from_source(fullname) |
| 93 | cache_dir = os.path.dirname(cfile) |
| 94 | try: |
| 95 | os.mkdir(cache_dir) |
| 96 | except OSError as error: |
| 97 | if error.errno != errno.EEXIST: |
| 98 | raise |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 99 | head, tail = name[:-3], name[-3:] |
| 100 | if tail == '.py': |
| 101 | if not force: |
| 102 | try: |
| 103 | mtime = int(os.stat(fullname).st_mtime) |
| 104 | expect = struct.pack('<4sl', imp.get_magic(), mtime) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 105 | with open(cfile, 'rb') as chandle: |
| 106 | actual = chandle.read(8) |
| 107 | if expect == actual: |
| 108 | return success |
| 109 | except IOError: |
| 110 | pass |
| 111 | if not quiet: |
| 112 | print('Compiling', fullname, '...') |
| 113 | try: |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 114 | ok = py_compile.compile(fullname, cfile, dfile, True) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 115 | except py_compile.PyCompileError as err: |
| 116 | if quiet: |
| 117 | print('*** Error compiling', fullname, '...') |
| 118 | else: |
| 119 | print('*** ', end='') |
Martin v. Löwis | 4b00307 | 2010-03-16 13:19:21 +0000 | [diff] [blame] | 120 | # escape non-printable characters in msg |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 121 | msg = err.msg.encode(sys.stdout.encoding, |
| 122 | errors='backslashreplace') |
Martin v. Löwis | 4b00307 | 2010-03-16 13:19:21 +0000 | [diff] [blame] | 123 | msg = msg.decode(sys.stdout.encoding) |
| 124 | print(msg) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 125 | success = 0 |
| 126 | except (SyntaxError, UnicodeError, IOError) as e: |
| 127 | if quiet: |
| 128 | print('*** Error compiling', fullname, '...') |
| 129 | else: |
| 130 | print('*** ', end='') |
| 131 | print(e.__class__.__name__ + ':', e) |
| 132 | success = 0 |
| 133 | else: |
| 134 | if ok == 0: |
| 135 | success = 0 |
| 136 | return success |
| 137 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 138 | def compile_path(skip_curdir=1, maxlevels=0, force=False, quiet=False, |
| 139 | legacy=False): |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 140 | """Byte-compile all module on sys.path. |
| 141 | |
| 142 | Arguments (all optional): |
| 143 | |
| 144 | skip_curdir: if true, skip current directory (default true) |
| 145 | maxlevels: max recursion level (default 0) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 146 | force: as for compile_dir() (default False) |
| 147 | quiet: as for compile_dir() (default False) |
| 148 | legacy: as for compile_dir() (default False) |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 149 | |
| 150 | """ |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 151 | success = 1 |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 152 | for dir in sys.path: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 153 | if (not dir or dir == os.curdir) and skip_curdir: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 154 | print('Skipping current directory') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 155 | else: |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 156 | success = success and compile_dir(dir, maxlevels, None, |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 157 | force, quiet=quiet, |
| 158 | legacy=legacy) |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 159 | return success |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 160 | |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 161 | def expand_args(args, flist): |
| 162 | """read names in flist and append to args""" |
| 163 | expanded = args[:] |
| 164 | if flist: |
| 165 | try: |
| 166 | if flist == '-': |
| 167 | fd = sys.stdin |
| 168 | else: |
| 169 | fd = open(flist) |
| 170 | while 1: |
| 171 | line = fd.readline() |
| 172 | if not line: |
| 173 | break |
| 174 | expanded.append(line[:-1]) |
| 175 | except IOError: |
| 176 | print("Error reading file list %s" % flist) |
| 177 | raise |
| 178 | return expanded |
| 179 | |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 180 | def main(): |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 181 | """Script main program.""" |
| 182 | import getopt |
| 183 | try: |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 184 | opts, args = getopt.getopt(sys.argv[1:], 'lfqd:x:i:b') |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 185 | except getopt.error as msg: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 186 | print(msg) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 187 | print("usage: python compileall.py [-l] [-f] [-q] [-d destdir] " |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 188 | "[-x regexp] [-i list] [directory|file ...]") |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 189 | print("-l: don't recurse down") |
| 190 | print("-f: force rebuild even if timestamps are up-to-date") |
| 191 | print("-q: quiet operation") |
| 192 | print("-d destdir: purported directory name for error messages") |
| 193 | print(" if no directory arguments, -l sys.path is assumed") |
| 194 | print("-x regexp: skip files matching the regular expression regexp") |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 195 | print(" the regexp is searched for in the full path of the file") |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 196 | print("-i list: expand list with its content " |
| 197 | "(file and directory names)") |
| 198 | print("-b: Produce legacy byte-compile file paths") |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 199 | sys.exit(2) |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 200 | maxlevels = 10 |
| 201 | ddir = None |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 202 | force = False |
| 203 | quiet = False |
Jeremy Hylton | 12b6457 | 2001-04-18 01:20:21 +0000 | [diff] [blame] | 204 | rx = None |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 205 | flist = None |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 206 | legacy = False |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 207 | for o, a in opts: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 208 | if o == '-l': maxlevels = 0 |
| 209 | if o == '-d': ddir = a |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 210 | if o == '-f': force = True |
| 211 | if o == '-q': quiet = True |
Jeremy Hylton | 12b6457 | 2001-04-18 01:20:21 +0000 | [diff] [blame] | 212 | if o == '-x': |
| 213 | import re |
| 214 | rx = re.compile(a) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 215 | if o == '-i': flist = a |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 216 | if o == '-b': legacy = True |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 217 | if ddir: |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 218 | if len(args) != 1 and not os.path.isdir(args[0]): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 219 | print("-d destdir require exactly one directory argument") |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 220 | sys.exit(2) |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 221 | success = 1 |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 222 | try: |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 223 | if args or flist: |
| 224 | try: |
| 225 | if flist: |
| 226 | args = expand_args(args, flist) |
| 227 | except IOError: |
| 228 | success = 0 |
| 229 | if success: |
| 230 | for arg in args: |
| 231 | if os.path.isdir(arg): |
| 232 | if not compile_dir(arg, maxlevels, ddir, |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 233 | force, rx, quiet, legacy): |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 234 | success = 0 |
| 235 | else: |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 236 | if not compile_file(arg, ddir, force, rx, |
| 237 | quiet, legacy): |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 238 | success = 0 |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 239 | else: |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 240 | success = compile_path(legacy=legacy) |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 241 | except KeyboardInterrupt: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 242 | print("\n[interrupt]") |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 243 | success = 0 |
| 244 | return success |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 245 | |
| 246 | if __name__ == '__main__': |
Raymond Hettinger | 7b4b788 | 2004-12-20 00:29:29 +0000 | [diff] [blame] | 247 | exit_status = int(not main()) |
Jeremy Hylton | 12b6457 | 2001-04-18 01:20:21 +0000 | [diff] [blame] | 248 | sys.exit(exit_status) |