Éric Araujo | 2e579f0 | 2010-11-20 21:53:02 +0000 | [diff] [blame] | 1 | """Module/script to byte-compile all .py files to .pyc (or .pyo) files. |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 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. |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 12 | """ |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 13 | import os |
| 14 | import sys |
Éric Araujo | 2e579f0 | 2010-11-20 21:53:02 +0000 | [diff] [blame] | 15 | import errno |
| 16 | import imp |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 17 | import py_compile |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 18 | import struct |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 19 | |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 20 | __all__ = ["compile_dir","compile_file","compile_path"] |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 21 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 22 | def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None, |
| 23 | quiet=False, legacy=False, optimize=-1): |
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) |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame^] | 30 | ddir: the directory that will be prepended to the path to the |
| 31 | file as it is compiled into each byte-code file. |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 32 | force: if True, force compilation, even if timestamps are up-to-date |
| 33 | quiet: if True, be quiet during compilation |
| 34 | legacy: if True, produce legacy pyc paths instead of PEP 3147 paths |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 35 | optimize: optimization level or -1 for level of the interpreter |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 36 | """ |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 37 | if not quiet: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 38 | print('Listing', dir, '...') |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 39 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 40 | names = os.listdir(dir) |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 41 | except os.error: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 42 | print("Can't list", dir) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 43 | names = [] |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 44 | names.sort() |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 45 | success = 1 |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 46 | for name in names: |
Barry Warsaw | c04317f | 2010-04-26 15:59:03 +0000 | [diff] [blame] | 47 | if name == '__pycache__': |
| 48 | continue |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 49 | fullname = os.path.join(dir, name) |
Raymond Hettinger | 8989ea6 | 2002-06-01 00:06:20 +0000 | [diff] [blame] | 50 | if ddir is not None: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 51 | dfile = os.path.join(ddir, name) |
| 52 | else: |
| 53 | dfile = None |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 54 | if not os.path.isdir(fullname): |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 55 | if not compile_file(fullname, ddir, force, rx, quiet, |
| 56 | legacy, optimize): |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 57 | success = 0 |
Éric Araujo | 2e579f0 | 2010-11-20 21:53:02 +0000 | [diff] [blame] | 58 | elif (maxlevels > 0 and name != os.curdir and name != os.pardir and |
| 59 | os.path.isdir(fullname) and not os.path.islink(fullname)): |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 60 | if not compile_dir(fullname, maxlevels - 1, dfile, force, rx, |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 61 | quiet, legacy): |
Jeremy Hylton | 12b6457 | 2001-04-18 01:20:21 +0000 | [diff] [blame] | 62 | success = 0 |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 63 | return success |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 64 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 65 | def compile_file(fullname, ddir=None, force=0, rx=None, quiet=False, |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 66 | legacy=False, optimize=-1): |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 67 | """Byte-compile file. |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 68 | fullname: the file to byte-compile |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame^] | 69 | ddir: if given, the directory name compiled in to the |
| 70 | byte-code file. |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 71 | force: if True, force compilation, even if timestamps are up-to-date |
| 72 | quiet: if True, be quiet during compilation |
| 73 | legacy: if True, produce legacy pyc paths instead of PEP 3147 paths |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 74 | optimize: optimization level or -1 for level of the interpreter |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 75 | """ |
| 76 | success = 1 |
| 77 | name = os.path.basename(fullname) |
| 78 | if ddir is not None: |
| 79 | dfile = os.path.join(ddir, name) |
| 80 | else: |
| 81 | dfile = None |
| 82 | if rx is not None: |
| 83 | mo = rx.search(fullname) |
| 84 | if mo: |
| 85 | return success |
| 86 | if os.path.isfile(fullname): |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 87 | if legacy: |
| 88 | cfile = fullname + ('c' if __debug__ else 'o') |
| 89 | else: |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 90 | if optimize >= 0: |
| 91 | cfile = imp.cache_from_source(fullname, |
| 92 | debug_override=not optimize) |
| 93 | else: |
| 94 | cfile = imp.cache_from_source(fullname) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 95 | cache_dir = os.path.dirname(cfile) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 96 | head, tail = name[:-3], name[-3:] |
| 97 | if tail == '.py': |
| 98 | if not force: |
| 99 | try: |
| 100 | mtime = int(os.stat(fullname).st_mtime) |
| 101 | expect = struct.pack('<4sl', imp.get_magic(), mtime) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 102 | with open(cfile, 'rb') as chandle: |
| 103 | actual = chandle.read(8) |
| 104 | if expect == actual: |
| 105 | return success |
| 106 | except IOError: |
| 107 | pass |
| 108 | if not quiet: |
| 109 | print('Compiling', fullname, '...') |
| 110 | try: |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 111 | ok = py_compile.compile(fullname, cfile, dfile, True, |
| 112 | optimize=optimize) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 113 | except py_compile.PyCompileError as err: |
| 114 | if quiet: |
| 115 | print('*** Error compiling', fullname, '...') |
| 116 | else: |
| 117 | print('*** ', end='') |
Martin v. Löwis | 4b00307 | 2010-03-16 13:19:21 +0000 | [diff] [blame] | 118 | # escape non-printable characters in msg |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 119 | msg = err.msg.encode(sys.stdout.encoding, |
| 120 | errors='backslashreplace') |
Martin v. Löwis | 4b00307 | 2010-03-16 13:19:21 +0000 | [diff] [blame] | 121 | msg = msg.decode(sys.stdout.encoding) |
| 122 | print(msg) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 123 | success = 0 |
| 124 | except (SyntaxError, UnicodeError, IOError) as e: |
| 125 | if quiet: |
| 126 | print('*** Error compiling', fullname, '...') |
| 127 | else: |
| 128 | print('*** ', end='') |
| 129 | print(e.__class__.__name__ + ':', e) |
| 130 | success = 0 |
| 131 | else: |
| 132 | if ok == 0: |
| 133 | success = 0 |
| 134 | return success |
| 135 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 136 | def compile_path(skip_curdir=1, maxlevels=0, force=False, quiet=False, |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 137 | legacy=False, optimize=-1): |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 138 | """Byte-compile all module on sys.path. |
| 139 | |
| 140 | Arguments (all optional): |
| 141 | |
| 142 | skip_curdir: if true, skip current directory (default true) |
| 143 | maxlevels: max recursion level (default 0) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 144 | force: as for compile_dir() (default False) |
| 145 | quiet: as for compile_dir() (default False) |
| 146 | legacy: as for compile_dir() (default False) |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 147 | optimize: as for compile_dir() (default -1) |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 148 | """ |
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 | for dir in sys.path: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 151 | if (not dir or dir == os.curdir) and skip_curdir: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 152 | print('Skipping current directory') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 153 | else: |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 154 | success = success and compile_dir(dir, maxlevels, None, |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 155 | force, quiet=quiet, |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 156 | legacy=legacy, optimize=optimize) |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 157 | return success |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 158 | |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 159 | |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 160 | def main(): |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 161 | """Script main program.""" |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 162 | import argparse |
| 163 | |
| 164 | parser = argparse.ArgumentParser( |
| 165 | description='Utilities to support installing Python libraries.') |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame^] | 166 | parser.add_argument('-l', action='store_const', const=0, |
| 167 | default=10, dest='maxlevels', |
| 168 | help="don't recurse into subdirectories") |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 169 | parser.add_argument('-f', action='store_true', dest='force', |
| 170 | help='force rebuild even if timestamps are up to date') |
| 171 | parser.add_argument('-q', action='store_true', dest='quiet', |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame^] | 172 | help='output only error messages') |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 173 | parser.add_argument('-b', action='store_true', dest='legacy', |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame^] | 174 | help='use legacy (pre-PEP3147) compiled file locations') |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 175 | parser.add_argument('-d', metavar='DESTDIR', dest='ddir', default=None, |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame^] | 176 | help=('directory to prepend to file paths for use in ' |
| 177 | 'compile time tracebacks and in runtime ' |
| 178 | 'tracebacks in cases where the source file is ' |
| 179 | 'unavailable')) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 180 | parser.add_argument('-x', metavar='REGEXP', dest='rx', default=None, |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame^] | 181 | help=('skip files matching the regular expression. ' |
Éric Araujo | 2e579f0 | 2010-11-20 21:53:02 +0000 | [diff] [blame] | 182 | 'The regexp is searched for in the full path ' |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame^] | 183 | 'to each file considered for compilation.')) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 184 | parser.add_argument('-i', metavar='FILE', dest='flist', |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame^] | 185 | help=('add all the files and directories listed in ' |
| 186 | 'FILE to the list considered for compilation. ' |
| 187 | 'If "-", names are read from stdin.')) |
| 188 | parser.add_argument('compile_dest', metavar='FILE|DIR', nargs='*', |
| 189 | help=('zero or more file and directory names ' |
| 190 | 'to compile; if no arguments given, defaults ' |
| 191 | 'to the equivalent of -l sys.path')) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 192 | args = parser.parse_args() |
| 193 | |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 194 | compile_dests = args.compile_dest |
| 195 | |
| 196 | if (args.ddir and (len(compile_dests) != 1 |
| 197 | or not os.path.isdir(compile_dests[0]))): |
| 198 | parser.exit('-d destdir requires exactly one directory argument') |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 199 | if args.rx: |
| 200 | import re |
| 201 | args.rx = re.compile(args.rx) |
| 202 | |
| 203 | # if flist is provided then load it |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 204 | if args.flist: |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 205 | try: |
| 206 | with (sys.stdin if args.flist=='-' else open(args.flist)) as f: |
| 207 | for line in f: |
| 208 | compile_dests.append(line.strip()) |
| 209 | except EnvironmentError: |
| 210 | print("Error reading file list {}".format(args.flist)) |
| 211 | return False |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 212 | |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 213 | success = True |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 214 | try: |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 215 | if compile_dests: |
| 216 | for dest in compile_dests: |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 217 | if os.path.isfile(dest): |
| 218 | if not compile_file(dest, args.ddir, args.force, args.rx, |
| 219 | args.quiet, args.legacy): |
| 220 | success = False |
| 221 | else: |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 222 | if not compile_dir(dest, args.maxlevels, args.ddir, |
| 223 | args.force, args.rx, args.quiet, |
| 224 | args.legacy): |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 225 | success = False |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 226 | return success |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 227 | else: |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 228 | return compile_path(legacy=args.legacy) |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 229 | except KeyboardInterrupt: |
Éric Araujo | 2e579f0 | 2010-11-20 21:53:02 +0000 | [diff] [blame] | 230 | print("\n[interrupted]") |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 231 | return False |
| 232 | return True |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 233 | |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 234 | |
| 235 | if __name__ == '__main__': |
Raymond Hettinger | 7b4b788 | 2004-12-20 00:29:29 +0000 | [diff] [blame] | 236 | exit_status = int(not main()) |
Jeremy Hylton | 12b6457 | 2001-04-18 01:20:21 +0000 | [diff] [blame] | 237 | sys.exit(exit_status) |