Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 1 | """Module/script to byte-compile all .py files to .pyc 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 |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 15 | import importlib.util |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 16 | import py_compile |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 17 | import struct |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 18 | |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 19 | from functools import partial |
| 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 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 23 | def _walk_dir(dir, ddir=None, maxlevels=10, quiet=0): |
Berker Peksag | 812a2b6 | 2016-10-01 00:54:18 +0300 | [diff] [blame] | 24 | if quiet < 2 and isinstance(dir, os.PathLike): |
| 25 | dir = os.fspath(dir) |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 26 | if not quiet: |
| 27 | print('Listing {!r}...'.format(dir)) |
| 28 | try: |
| 29 | names = os.listdir(dir) |
| 30 | except OSError: |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 31 | if quiet < 2: |
| 32 | print("Can't list {!r}".format(dir)) |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 33 | names = [] |
| 34 | names.sort() |
| 35 | for name in names: |
| 36 | if name == '__pycache__': |
| 37 | continue |
| 38 | fullname = os.path.join(dir, name) |
| 39 | if ddir is not None: |
| 40 | dfile = os.path.join(ddir, name) |
| 41 | else: |
| 42 | dfile = None |
| 43 | if not os.path.isdir(fullname): |
| 44 | yield fullname |
| 45 | elif (maxlevels > 0 and name != os.curdir and name != os.pardir and |
| 46 | os.path.isdir(fullname) and not os.path.islink(fullname)): |
| 47 | yield from _walk_dir(fullname, ddir=dfile, |
| 48 | maxlevels=maxlevels - 1, quiet=quiet) |
| 49 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 50 | def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None, |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 51 | quiet=0, legacy=False, optimize=-1, workers=1, |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 52 | invalidation_mode=None): |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 53 | """Byte-compile all modules in the given directory tree. |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 55 | Arguments (only dir is required): |
| 56 | |
| 57 | dir: the directory to byte-compile |
| 58 | maxlevels: maximum recursion level (default 10) |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 59 | ddir: the directory that will be prepended to the path to the |
| 60 | file as it is compiled into each byte-code file. |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 61 | force: if True, force compilation, even if timestamps are up-to-date |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 62 | quiet: full output with False or 0, errors only with 1, |
| 63 | no output with 2 |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 64 | legacy: if True, produce legacy pyc paths instead of PEP 3147 paths |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 65 | optimize: optimization level or -1 for level of the interpreter |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 66 | workers: maximum number of parallel workers |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 67 | invalidation_mode: how the up-to-dateness of the pyc will be checked |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 68 | """ |
Dustin Spicuzza | 1d817e4 | 2018-11-23 12:06:55 -0500 | [diff] [blame] | 69 | ProcessPoolExecutor = None |
Antoine Pitrou | 1a2dd82 | 2019-05-15 23:45:18 +0200 | [diff] [blame] | 70 | if workers < 0: |
| 71 | raise ValueError('workers must be greater or equal to 0') |
| 72 | if workers != 1: |
| 73 | try: |
| 74 | # Only import when needed, as low resource platforms may |
| 75 | # fail to import it |
| 76 | from concurrent.futures import ProcessPoolExecutor |
| 77 | except ImportError: |
| 78 | workers = 1 |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 79 | files = _walk_dir(dir, quiet=quiet, maxlevels=maxlevels, |
| 80 | ddir=ddir) |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 81 | success = True |
Antoine Pitrou | 1a2dd82 | 2019-05-15 23:45:18 +0200 | [diff] [blame] | 82 | if workers != 1 and ProcessPoolExecutor is not None: |
| 83 | # If workers == 0, let ProcessPoolExecutor choose |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 84 | workers = workers or None |
| 85 | with ProcessPoolExecutor(max_workers=workers) as executor: |
| 86 | results = executor.map(partial(compile_file, |
| 87 | ddir=ddir, force=force, |
| 88 | rx=rx, quiet=quiet, |
| 89 | legacy=legacy, |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 90 | optimize=optimize, |
| 91 | invalidation_mode=invalidation_mode), |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 92 | files) |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 93 | success = min(results, default=True) |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 94 | else: |
| 95 | for file in files: |
| 96 | if not compile_file(file, ddir, force, rx, quiet, |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 97 | legacy, optimize, invalidation_mode): |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 98 | success = False |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 99 | return success |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 100 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 101 | def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 102 | legacy=False, optimize=-1, |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 103 | invalidation_mode=None): |
Éric Araujo | 413d7b4 | 2010-12-23 18:44:31 +0000 | [diff] [blame] | 104 | """Byte-compile one file. |
| 105 | |
| 106 | Arguments (only fullname is required): |
| 107 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 108 | fullname: the file to byte-compile |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 109 | ddir: if given, the directory name compiled in to the |
| 110 | byte-code file. |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 111 | force: if True, force compilation, even if timestamps are up-to-date |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 112 | quiet: full output with False or 0, errors only with 1, |
| 113 | no output with 2 |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 114 | legacy: if True, produce legacy pyc paths instead of PEP 3147 paths |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 115 | optimize: optimization level or -1 for level of the interpreter |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 116 | invalidation_mode: how the up-to-dateness of the pyc will be checked |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 117 | """ |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 118 | success = True |
Berker Peksag | 812a2b6 | 2016-10-01 00:54:18 +0300 | [diff] [blame] | 119 | if quiet < 2 and isinstance(fullname, os.PathLike): |
| 120 | fullname = os.fspath(fullname) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 121 | name = os.path.basename(fullname) |
| 122 | if ddir is not None: |
| 123 | dfile = os.path.join(ddir, name) |
| 124 | else: |
| 125 | dfile = None |
| 126 | if rx is not None: |
| 127 | mo = rx.search(fullname) |
| 128 | if mo: |
| 129 | return success |
| 130 | if os.path.isfile(fullname): |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 131 | if legacy: |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 132 | cfile = fullname + 'c' |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 133 | else: |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 134 | if optimize >= 0: |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 135 | opt = optimize if optimize >= 1 else '' |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 136 | cfile = importlib.util.cache_from_source( |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 137 | fullname, optimization=opt) |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 138 | else: |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 139 | cfile = importlib.util.cache_from_source(fullname) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 140 | cache_dir = os.path.dirname(cfile) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 141 | head, tail = name[:-3], name[-3:] |
| 142 | if tail == '.py': |
| 143 | if not force: |
| 144 | try: |
| 145 | mtime = int(os.stat(fullname).st_mtime) |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 146 | expect = struct.pack('<4sll', importlib.util.MAGIC_NUMBER, |
| 147 | 0, mtime) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 148 | with open(cfile, 'rb') as chandle: |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 149 | actual = chandle.read(12) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 150 | if expect == actual: |
| 151 | return success |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 152 | except OSError: |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 153 | pass |
| 154 | if not quiet: |
Victor Stinner | 5307126 | 2011-05-11 00:36:28 +0200 | [diff] [blame] | 155 | print('Compiling {!r}...'.format(fullname)) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 156 | try: |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 157 | ok = py_compile.compile(fullname, cfile, dfile, True, |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 158 | optimize=optimize, |
| 159 | invalidation_mode=invalidation_mode) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 160 | except py_compile.PyCompileError as err: |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 161 | success = False |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 162 | if quiet >= 2: |
| 163 | return success |
| 164 | elif quiet: |
Victor Stinner | 5307126 | 2011-05-11 00:36:28 +0200 | [diff] [blame] | 165 | print('*** Error compiling {!r}...'.format(fullname)) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 166 | else: |
| 167 | print('*** ', end='') |
Martin v. Löwis | 4b00307 | 2010-03-16 13:19:21 +0000 | [diff] [blame] | 168 | # escape non-printable characters in msg |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 169 | msg = err.msg.encode(sys.stdout.encoding, |
| 170 | errors='backslashreplace') |
Martin v. Löwis | 4b00307 | 2010-03-16 13:19:21 +0000 | [diff] [blame] | 171 | msg = msg.decode(sys.stdout.encoding) |
| 172 | print(msg) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 173 | except (SyntaxError, UnicodeError, OSError) as e: |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 174 | success = False |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 175 | if quiet >= 2: |
| 176 | return success |
| 177 | elif quiet: |
Victor Stinner | 5307126 | 2011-05-11 00:36:28 +0200 | [diff] [blame] | 178 | print('*** Error compiling {!r}...'.format(fullname)) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 179 | else: |
| 180 | print('*** ', end='') |
| 181 | print(e.__class__.__name__ + ':', e) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 182 | else: |
| 183 | if ok == 0: |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 184 | success = False |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 185 | return success |
| 186 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 187 | def compile_path(skip_curdir=1, maxlevels=0, force=False, quiet=0, |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 188 | legacy=False, optimize=-1, |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 189 | invalidation_mode=None): |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 190 | """Byte-compile all module on sys.path. |
| 191 | |
| 192 | Arguments (all optional): |
| 193 | |
Éric Araujo | 3b371cf | 2011-09-01 20:00:33 +0200 | [diff] [blame] | 194 | skip_curdir: if true, skip current directory (default True) |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 195 | maxlevels: max recursion level (default 0) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 196 | force: as for compile_dir() (default False) |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 197 | quiet: as for compile_dir() (default 0) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 198 | legacy: as for compile_dir() (default False) |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 199 | optimize: as for compile_dir() (default -1) |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 200 | invalidation_mode: as for compiler_dir() |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 201 | """ |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 202 | success = True |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 203 | for dir in sys.path: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 204 | if (not dir or dir == os.curdir) and skip_curdir: |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 205 | if quiet < 2: |
| 206 | print('Skipping current directory') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 207 | else: |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 208 | success = success and compile_dir( |
| 209 | dir, |
| 210 | maxlevels, |
| 211 | None, |
| 212 | force, |
| 213 | quiet=quiet, |
| 214 | legacy=legacy, |
| 215 | optimize=optimize, |
| 216 | invalidation_mode=invalidation_mode, |
| 217 | ) |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 218 | return success |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 219 | |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 220 | |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 221 | def main(): |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 222 | """Script main program.""" |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 223 | import argparse |
| 224 | |
| 225 | parser = argparse.ArgumentParser( |
| 226 | description='Utilities to support installing Python libraries.') |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 227 | parser.add_argument('-l', action='store_const', const=0, |
| 228 | default=10, dest='maxlevels', |
| 229 | help="don't recurse into subdirectories") |
Benjamin Peterson | 344ff4a | 2014-08-19 16:13:26 -0500 | [diff] [blame] | 230 | parser.add_argument('-r', type=int, dest='recursion', |
| 231 | help=('control the maximum recursion level. ' |
| 232 | 'if `-l` and `-r` options are specified, ' |
| 233 | 'then `-r` takes precedence.')) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 234 | parser.add_argument('-f', action='store_true', dest='force', |
| 235 | help='force rebuild even if timestamps are up to date') |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 236 | parser.add_argument('-q', action='count', dest='quiet', default=0, |
| 237 | help='output only error messages; -qq will suppress ' |
| 238 | 'the error messages as well.') |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 239 | parser.add_argument('-b', action='store_true', dest='legacy', |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 240 | help='use legacy (pre-PEP3147) compiled file locations') |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 241 | parser.add_argument('-d', metavar='DESTDIR', dest='ddir', default=None, |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 242 | help=('directory to prepend to file paths for use in ' |
Éric Araujo | 3b371cf | 2011-09-01 20:00:33 +0200 | [diff] [blame] | 243 | 'compile-time tracebacks and in runtime ' |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 244 | 'tracebacks in cases where the source file is ' |
| 245 | 'unavailable')) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 246 | parser.add_argument('-x', metavar='REGEXP', dest='rx', default=None, |
Éric Araujo | 3b371cf | 2011-09-01 20:00:33 +0200 | [diff] [blame] | 247 | help=('skip files matching the regular expression; ' |
| 248 | 'the regexp is searched for in the full path ' |
| 249 | 'of each file considered for compilation')) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 250 | parser.add_argument('-i', metavar='FILE', dest='flist', |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 251 | help=('add all the files and directories listed in ' |
Éric Araujo | 3b371cf | 2011-09-01 20:00:33 +0200 | [diff] [blame] | 252 | 'FILE to the list considered for compilation; ' |
| 253 | 'if "-", names are read from stdin')) |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 254 | parser.add_argument('compile_dest', metavar='FILE|DIR', nargs='*', |
| 255 | help=('zero or more file and directory names ' |
| 256 | 'to compile; if no arguments given, defaults ' |
| 257 | 'to the equivalent of -l sys.path')) |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 258 | parser.add_argument('-j', '--workers', default=1, |
| 259 | type=int, help='Run compileall concurrently') |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 260 | invalidation_modes = [mode.name.lower().replace('_', '-') |
| 261 | for mode in py_compile.PycInvalidationMode] |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 262 | parser.add_argument('--invalidation-mode', |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 263 | choices=sorted(invalidation_modes), |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 264 | help=('set .pyc invalidation mode; defaults to ' |
| 265 | '"checked-hash" if the SOURCE_DATE_EPOCH ' |
| 266 | 'environment variable is set, and ' |
| 267 | '"timestamp" otherwise.')) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 268 | |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 269 | args = parser.parse_args() |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 270 | compile_dests = args.compile_dest |
| 271 | |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 272 | if args.rx: |
| 273 | import re |
| 274 | args.rx = re.compile(args.rx) |
| 275 | |
Benjamin Peterson | 344ff4a | 2014-08-19 16:13:26 -0500 | [diff] [blame] | 276 | |
| 277 | if args.recursion is not None: |
| 278 | maxlevels = args.recursion |
| 279 | else: |
| 280 | maxlevels = args.maxlevels |
| 281 | |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 282 | # if flist is provided then load it |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 283 | if args.flist: |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 284 | try: |
| 285 | with (sys.stdin if args.flist=='-' else open(args.flist)) as f: |
| 286 | for line in f: |
| 287 | compile_dests.append(line.strip()) |
Andrew Svetlov | 3438fa4 | 2012-12-17 23:35:18 +0200 | [diff] [blame] | 288 | except OSError: |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 289 | if args.quiet < 2: |
| 290 | print("Error reading file list {}".format(args.flist)) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 291 | return False |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 292 | |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 293 | if args.invalidation_mode: |
| 294 | ivl_mode = args.invalidation_mode.replace('-', '_').upper() |
| 295 | invalidation_mode = py_compile.PycInvalidationMode[ivl_mode] |
| 296 | else: |
| 297 | invalidation_mode = None |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 298 | |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 299 | success = True |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 300 | try: |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 301 | if compile_dests: |
| 302 | for dest in compile_dests: |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 303 | if os.path.isfile(dest): |
| 304 | if not compile_file(dest, args.ddir, args.force, args.rx, |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 305 | args.quiet, args.legacy, |
| 306 | invalidation_mode=invalidation_mode): |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 307 | success = False |
| 308 | else: |
Benjamin Peterson | 344ff4a | 2014-08-19 16:13:26 -0500 | [diff] [blame] | 309 | if not compile_dir(dest, maxlevels, args.ddir, |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 310 | args.force, args.rx, args.quiet, |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 311 | args.legacy, workers=args.workers, |
| 312 | invalidation_mode=invalidation_mode): |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 313 | success = False |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 314 | return success |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 315 | else: |
R David Murray | 8a1d1e6 | 2013-12-15 20:49:38 -0500 | [diff] [blame] | 316 | return compile_path(legacy=args.legacy, force=args.force, |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 317 | quiet=args.quiet, |
| 318 | invalidation_mode=invalidation_mode) |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 319 | except KeyboardInterrupt: |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 320 | if args.quiet < 2: |
| 321 | print("\n[interrupted]") |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 322 | return False |
| 323 | return True |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 324 | |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 325 | |
| 326 | if __name__ == '__main__': |
Raymond Hettinger | 7b4b788 | 2004-12-20 00:29:29 +0000 | [diff] [blame] | 327 | exit_status = int(not main()) |
Jeremy Hylton | 12b6457 | 2001-04-18 01:20:21 +0000 | [diff] [blame] | 328 | sys.exit(exit_status) |