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 |
Lumír 'Frenzy' Balhar | e77d428 | 2020-05-14 16:17:22 +0200 | [diff] [blame] | 18 | import filecmp |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 19 | |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 20 | from functools import partial |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 21 | from pathlib import Path |
| 22 | |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 23 | __all__ = ["compile_dir","compile_file","compile_path"] |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 24 | |
Victor Stinner | eb1dda2 | 2019-10-15 11:26:13 +0200 | [diff] [blame] | 25 | def _walk_dir(dir, maxlevels, quiet=0): |
Berker Peksag | 812a2b6 | 2016-10-01 00:54:18 +0300 | [diff] [blame] | 26 | if quiet < 2 and isinstance(dir, os.PathLike): |
| 27 | dir = os.fspath(dir) |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 28 | if not quiet: |
| 29 | print('Listing {!r}...'.format(dir)) |
| 30 | try: |
| 31 | names = os.listdir(dir) |
| 32 | except OSError: |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 33 | if quiet < 2: |
| 34 | print("Can't list {!r}".format(dir)) |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 35 | names = [] |
| 36 | names.sort() |
| 37 | for name in names: |
| 38 | if name == '__pycache__': |
| 39 | continue |
| 40 | fullname = os.path.join(dir, name) |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 41 | if not os.path.isdir(fullname): |
| 42 | yield fullname |
| 43 | elif (maxlevels > 0 and name != os.curdir and name != os.pardir and |
| 44 | os.path.isdir(fullname) and not os.path.islink(fullname)): |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 45 | yield from _walk_dir(fullname, maxlevels=maxlevels - 1, |
| 46 | quiet=quiet) |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 47 | |
Victor Stinner | eb1dda2 | 2019-10-15 11:26:13 +0200 | [diff] [blame] | 48 | def compile_dir(dir, maxlevels=None, ddir=None, force=False, |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 49 | rx=None, quiet=0, legacy=False, optimize=-1, workers=1, |
Gregory P. Smith | 0267335 | 2020-02-28 17:28:37 -0800 | [diff] [blame] | 50 | invalidation_mode=None, *, stripdir=None, |
Lumír 'Frenzy' Balhar | e77d428 | 2020-05-14 16:17:22 +0200 | [diff] [blame] | 51 | prependdir=None, limit_sl_dest=None, hardlink_dupes=False): |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 52 | """Byte-compile all modules in the given directory tree. |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 54 | Arguments (only dir is required): |
| 55 | |
| 56 | dir: the directory to byte-compile |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 57 | maxlevels: maximum recursion level (default `sys.getrecursionlimit()`) |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 58 | ddir: the directory that will be prepended to the path to the |
| 59 | file as it is compiled into each byte-code file. |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 60 | force: if True, force compilation, even if timestamps are up-to-date |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 61 | quiet: full output with False or 0, errors only with 1, |
| 62 | no output with 2 |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 63 | legacy: if True, produce legacy pyc paths instead of PEP 3147 paths |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 64 | optimize: int or list of optimization levels or -1 for level of |
| 65 | the interpreter. Multiple levels leads to multiple compiled |
| 66 | files each with one optimization level. |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 67 | workers: maximum number of parallel workers |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 68 | invalidation_mode: how the up-to-dateness of the pyc will be checked |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 69 | stripdir: part of path to left-strip from source file path |
Jelle Zijlstra | efb8dd5 | 2020-04-30 03:39:11 -0700 | [diff] [blame] | 70 | prependdir: path to prepend to beginning of original file path, applied |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 71 | after stripdir |
| 72 | limit_sl_dest: ignore symlinks if they are pointing outside of |
| 73 | the defined path |
Lumír 'Frenzy' Balhar | e77d428 | 2020-05-14 16:17:22 +0200 | [diff] [blame] | 74 | hardlink_dupes: hardlink duplicated pyc files |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 75 | """ |
Dustin Spicuzza | 1d817e4 | 2018-11-23 12:06:55 -0500 | [diff] [blame] | 76 | ProcessPoolExecutor = None |
Gregory P. Smith | 0267335 | 2020-02-28 17:28:37 -0800 | [diff] [blame] | 77 | if ddir is not None and (stripdir is not None or prependdir is not None): |
| 78 | raise ValueError(("Destination dir (ddir) cannot be used " |
| 79 | "in combination with stripdir or prependdir")) |
| 80 | if ddir is not None: |
| 81 | stripdir = dir |
| 82 | prependdir = ddir |
| 83 | ddir = None |
Antoine Pitrou | 1a2dd82 | 2019-05-15 23:45:18 +0200 | [diff] [blame] | 84 | if workers < 0: |
| 85 | raise ValueError('workers must be greater or equal to 0') |
| 86 | if workers != 1: |
Asheesh Laroia | bf2e7e5 | 2021-02-07 19:15:51 -0800 | [diff] [blame] | 87 | # Check if this is a system where ProcessPoolExecutor can function. |
| 88 | from concurrent.futures.process import _check_system_limits |
Antoine Pitrou | 1a2dd82 | 2019-05-15 23:45:18 +0200 | [diff] [blame] | 89 | try: |
Asheesh Laroia | bf2e7e5 | 2021-02-07 19:15:51 -0800 | [diff] [blame] | 90 | _check_system_limits() |
| 91 | except NotImplementedError: |
Antoine Pitrou | 1a2dd82 | 2019-05-15 23:45:18 +0200 | [diff] [blame] | 92 | workers = 1 |
Asheesh Laroia | bf2e7e5 | 2021-02-07 19:15:51 -0800 | [diff] [blame] | 93 | else: |
| 94 | from concurrent.futures import ProcessPoolExecutor |
Victor Stinner | eb1dda2 | 2019-10-15 11:26:13 +0200 | [diff] [blame] | 95 | if maxlevels is None: |
| 96 | maxlevels = sys.getrecursionlimit() |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 97 | files = _walk_dir(dir, quiet=quiet, maxlevels=maxlevels) |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 98 | success = True |
Antoine Pitrou | 1a2dd82 | 2019-05-15 23:45:18 +0200 | [diff] [blame] | 99 | if workers != 1 and ProcessPoolExecutor is not None: |
| 100 | # If workers == 0, let ProcessPoolExecutor choose |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 101 | workers = workers or None |
| 102 | with ProcessPoolExecutor(max_workers=workers) as executor: |
| 103 | results = executor.map(partial(compile_file, |
| 104 | ddir=ddir, force=force, |
| 105 | rx=rx, quiet=quiet, |
| 106 | legacy=legacy, |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 107 | optimize=optimize, |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 108 | invalidation_mode=invalidation_mode, |
| 109 | stripdir=stripdir, |
| 110 | prependdir=prependdir, |
Lumír 'Frenzy' Balhar | e77d428 | 2020-05-14 16:17:22 +0200 | [diff] [blame] | 111 | limit_sl_dest=limit_sl_dest, |
| 112 | hardlink_dupes=hardlink_dupes), |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 113 | files) |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 114 | success = min(results, default=True) |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 115 | else: |
| 116 | for file in files: |
| 117 | if not compile_file(file, ddir, force, rx, quiet, |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 118 | legacy, optimize, invalidation_mode, |
| 119 | stripdir=stripdir, prependdir=prependdir, |
Lumír 'Frenzy' Balhar | e77d428 | 2020-05-14 16:17:22 +0200 | [diff] [blame] | 120 | limit_sl_dest=limit_sl_dest, |
| 121 | hardlink_dupes=hardlink_dupes): |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 122 | success = False |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 123 | return success |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 124 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 125 | def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 126 | legacy=False, optimize=-1, |
Gregory P. Smith | 0267335 | 2020-02-28 17:28:37 -0800 | [diff] [blame] | 127 | invalidation_mode=None, *, stripdir=None, prependdir=None, |
Lumír 'Frenzy' Balhar | e77d428 | 2020-05-14 16:17:22 +0200 | [diff] [blame] | 128 | limit_sl_dest=None, hardlink_dupes=False): |
Éric Araujo | 413d7b4 | 2010-12-23 18:44:31 +0000 | [diff] [blame] | 129 | """Byte-compile one file. |
| 130 | |
| 131 | Arguments (only fullname is required): |
| 132 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 133 | fullname: the file to byte-compile |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 134 | ddir: if given, the directory name compiled in to the |
| 135 | byte-code file. |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 136 | force: if True, force compilation, even if timestamps are up-to-date |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 137 | quiet: full output with False or 0, errors only with 1, |
| 138 | no output with 2 |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 139 | legacy: if True, produce legacy pyc paths instead of PEP 3147 paths |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 140 | optimize: int or list of optimization levels or -1 for level of |
| 141 | the interpreter. Multiple levels leads to multiple compiled |
| 142 | files each with one optimization level. |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 143 | invalidation_mode: how the up-to-dateness of the pyc will be checked |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 144 | stripdir: part of path to left-strip from source file path |
Jelle Zijlstra | efb8dd5 | 2020-04-30 03:39:11 -0700 | [diff] [blame] | 145 | prependdir: path to prepend to beginning of original file path, applied |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 146 | after stripdir |
| 147 | limit_sl_dest: ignore symlinks if they are pointing outside of |
| 148 | the defined path. |
Lumír 'Frenzy' Balhar | e77d428 | 2020-05-14 16:17:22 +0200 | [diff] [blame] | 149 | hardlink_dupes: hardlink duplicated pyc files |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 150 | """ |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 151 | |
| 152 | if ddir is not None and (stripdir is not None or prependdir is not None): |
| 153 | raise ValueError(("Destination dir (ddir) cannot be used " |
| 154 | "in combination with stripdir or prependdir")) |
| 155 | |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 156 | success = True |
Berker Peksag | 812a2b6 | 2016-10-01 00:54:18 +0300 | [diff] [blame] | 157 | if quiet < 2 and isinstance(fullname, os.PathLike): |
| 158 | fullname = os.fspath(fullname) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 159 | name = os.path.basename(fullname) |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 160 | |
| 161 | dfile = None |
| 162 | |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 163 | if ddir is not None: |
| 164 | dfile = os.path.join(ddir, name) |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 165 | |
| 166 | if stripdir is not None: |
| 167 | fullname_parts = fullname.split(os.path.sep) |
| 168 | stripdir_parts = stripdir.split(os.path.sep) |
| 169 | ddir_parts = list(fullname_parts) |
| 170 | |
| 171 | for spart, opart in zip(stripdir_parts, fullname_parts): |
| 172 | if spart == opart: |
| 173 | ddir_parts.remove(spart) |
| 174 | |
| 175 | dfile = os.path.join(*ddir_parts) |
| 176 | |
| 177 | if prependdir is not None: |
| 178 | if dfile is None: |
| 179 | dfile = os.path.join(prependdir, fullname) |
| 180 | else: |
| 181 | dfile = os.path.join(prependdir, dfile) |
| 182 | |
| 183 | if isinstance(optimize, int): |
| 184 | optimize = [optimize] |
| 185 | |
Lumír 'Frenzy' Balhar | e77d428 | 2020-05-14 16:17:22 +0200 | [diff] [blame] | 186 | # Use set() to remove duplicates. |
| 187 | # Use sorted() to create pyc files in a deterministic order. |
| 188 | optimize = sorted(set(optimize)) |
| 189 | |
| 190 | if hardlink_dupes and len(optimize) < 2: |
| 191 | raise ValueError("Hardlinking of duplicated bytecode makes sense " |
| 192 | "only for more than one optimization level") |
| 193 | |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 194 | if rx is not None: |
| 195 | mo = rx.search(fullname) |
| 196 | if mo: |
| 197 | return success |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 198 | |
| 199 | if limit_sl_dest is not None and os.path.islink(fullname): |
| 200 | if Path(limit_sl_dest).resolve() not in Path(fullname).resolve().parents: |
| 201 | return success |
| 202 | |
| 203 | opt_cfiles = {} |
| 204 | |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 205 | if os.path.isfile(fullname): |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 206 | for opt_level in optimize: |
| 207 | if legacy: |
| 208 | opt_cfiles[opt_level] = fullname + 'c' |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 209 | else: |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 210 | if opt_level >= 0: |
| 211 | opt = opt_level if opt_level >= 1 else '' |
| 212 | cfile = (importlib.util.cache_from_source( |
| 213 | fullname, optimization=opt)) |
| 214 | opt_cfiles[opt_level] = cfile |
| 215 | else: |
| 216 | cfile = importlib.util.cache_from_source(fullname) |
| 217 | opt_cfiles[opt_level] = cfile |
| 218 | |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 219 | head, tail = name[:-3], name[-3:] |
| 220 | if tail == '.py': |
| 221 | if not force: |
| 222 | try: |
| 223 | mtime = int(os.stat(fullname).st_mtime) |
Miss Islington (bot) | 0af681b | 2021-08-24 08:09:14 -0700 | [diff] [blame] | 224 | expect = struct.pack('<4sLL', importlib.util.MAGIC_NUMBER, |
| 225 | 0, mtime & 0xFFFF_FFFF) |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 226 | for cfile in opt_cfiles.values(): |
| 227 | with open(cfile, 'rb') as chandle: |
| 228 | actual = chandle.read(12) |
| 229 | if expect != actual: |
| 230 | break |
| 231 | else: |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 232 | return success |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 233 | except OSError: |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 234 | pass |
| 235 | if not quiet: |
Victor Stinner | 5307126 | 2011-05-11 00:36:28 +0200 | [diff] [blame] | 236 | print('Compiling {!r}...'.format(fullname)) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 237 | try: |
Lumír 'Frenzy' Balhar | e77d428 | 2020-05-14 16:17:22 +0200 | [diff] [blame] | 238 | for index, opt_level in enumerate(optimize): |
| 239 | cfile = opt_cfiles[opt_level] |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 240 | ok = py_compile.compile(fullname, cfile, dfile, True, |
| 241 | optimize=opt_level, |
| 242 | invalidation_mode=invalidation_mode) |
Lumír 'Frenzy' Balhar | e77d428 | 2020-05-14 16:17:22 +0200 | [diff] [blame] | 243 | if index > 0 and hardlink_dupes: |
| 244 | previous_cfile = opt_cfiles[optimize[index - 1]] |
| 245 | if filecmp.cmp(cfile, previous_cfile, shallow=False): |
| 246 | os.unlink(cfile) |
| 247 | os.link(previous_cfile, cfile) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 248 | except py_compile.PyCompileError as err: |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 249 | success = False |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 250 | if quiet >= 2: |
| 251 | return success |
| 252 | elif quiet: |
Victor Stinner | 5307126 | 2011-05-11 00:36:28 +0200 | [diff] [blame] | 253 | print('*** Error compiling {!r}...'.format(fullname)) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 254 | else: |
| 255 | print('*** ', end='') |
Martin v. Löwis | 4b00307 | 2010-03-16 13:19:21 +0000 | [diff] [blame] | 256 | # escape non-printable characters in msg |
Miss Islington (bot) | 0db6c14 | 2021-07-30 10:12:05 -0700 | [diff] [blame] | 257 | encoding = sys.stdout.encoding or sys.getdefaultencoding() |
| 258 | msg = err.msg.encode(encoding, errors='backslashreplace').decode(encoding) |
Martin v. Löwis | 4b00307 | 2010-03-16 13:19:21 +0000 | [diff] [blame] | 259 | print(msg) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 260 | except (SyntaxError, UnicodeError, OSError) as e: |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 261 | success = False |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 262 | if quiet >= 2: |
| 263 | return success |
| 264 | elif quiet: |
Victor Stinner | 5307126 | 2011-05-11 00:36:28 +0200 | [diff] [blame] | 265 | print('*** Error compiling {!r}...'.format(fullname)) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 266 | else: |
| 267 | print('*** ', end='') |
| 268 | print(e.__class__.__name__ + ':', e) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 269 | else: |
| 270 | if ok == 0: |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 271 | success = False |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 272 | return success |
| 273 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 274 | def compile_path(skip_curdir=1, maxlevels=0, force=False, quiet=0, |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 275 | legacy=False, optimize=-1, |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 276 | invalidation_mode=None): |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 277 | """Byte-compile all module on sys.path. |
| 278 | |
| 279 | Arguments (all optional): |
| 280 | |
Éric Araujo | 3b371cf | 2011-09-01 20:00:33 +0200 | [diff] [blame] | 281 | skip_curdir: if true, skip current directory (default True) |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 282 | maxlevels: max recursion level (default 0) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 283 | force: as for compile_dir() (default False) |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 284 | quiet: as for compile_dir() (default 0) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 285 | legacy: as for compile_dir() (default False) |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 286 | optimize: as for compile_dir() (default -1) |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 287 | invalidation_mode: as for compiler_dir() |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 288 | """ |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 289 | success = True |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 290 | for dir in sys.path: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 291 | if (not dir or dir == os.curdir) and skip_curdir: |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 292 | if quiet < 2: |
| 293 | print('Skipping current directory') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 294 | else: |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 295 | success = success and compile_dir( |
| 296 | dir, |
| 297 | maxlevels, |
| 298 | None, |
| 299 | force, |
| 300 | quiet=quiet, |
| 301 | legacy=legacy, |
| 302 | optimize=optimize, |
| 303 | invalidation_mode=invalidation_mode, |
| 304 | ) |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 305 | return success |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 306 | |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 307 | |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 308 | def main(): |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 309 | """Script main program.""" |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 310 | import argparse |
| 311 | |
| 312 | parser = argparse.ArgumentParser( |
| 313 | description='Utilities to support installing Python libraries.') |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 314 | parser.add_argument('-l', action='store_const', const=0, |
Victor Stinner | eb1dda2 | 2019-10-15 11:26:13 +0200 | [diff] [blame] | 315 | default=None, dest='maxlevels', |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 316 | help="don't recurse into subdirectories") |
Benjamin Peterson | 344ff4a | 2014-08-19 16:13:26 -0500 | [diff] [blame] | 317 | parser.add_argument('-r', type=int, dest='recursion', |
| 318 | help=('control the maximum recursion level. ' |
| 319 | 'if `-l` and `-r` options are specified, ' |
| 320 | 'then `-r` takes precedence.')) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 321 | parser.add_argument('-f', action='store_true', dest='force', |
| 322 | help='force rebuild even if timestamps are up to date') |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 323 | parser.add_argument('-q', action='count', dest='quiet', default=0, |
| 324 | help='output only error messages; -qq will suppress ' |
| 325 | 'the error messages as well.') |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 326 | parser.add_argument('-b', action='store_true', dest='legacy', |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 327 | help='use legacy (pre-PEP3147) compiled file locations') |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 328 | parser.add_argument('-d', metavar='DESTDIR', dest='ddir', default=None, |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 329 | help=('directory to prepend to file paths for use in ' |
Éric Araujo | 3b371cf | 2011-09-01 20:00:33 +0200 | [diff] [blame] | 330 | 'compile-time tracebacks and in runtime ' |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 331 | 'tracebacks in cases where the source file is ' |
| 332 | 'unavailable')) |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 333 | parser.add_argument('-s', metavar='STRIPDIR', dest='stripdir', |
| 334 | default=None, |
| 335 | help=('part of path to left-strip from path ' |
| 336 | 'to source file - for example buildroot. ' |
| 337 | '`-d` and `-s` options cannot be ' |
| 338 | 'specified together.')) |
| 339 | parser.add_argument('-p', metavar='PREPENDDIR', dest='prependdir', |
| 340 | default=None, |
| 341 | help=('path to add as prefix to path ' |
| 342 | 'to source file - for example / to make ' |
| 343 | 'it absolute when some part is removed ' |
| 344 | 'by `-s` option. ' |
| 345 | '`-d` and `-p` options cannot be ' |
| 346 | 'specified together.')) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 347 | parser.add_argument('-x', metavar='REGEXP', dest='rx', default=None, |
Éric Araujo | 3b371cf | 2011-09-01 20:00:33 +0200 | [diff] [blame] | 348 | help=('skip files matching the regular expression; ' |
| 349 | 'the regexp is searched for in the full path ' |
| 350 | 'of each file considered for compilation')) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 351 | parser.add_argument('-i', metavar='FILE', dest='flist', |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 352 | help=('add all the files and directories listed in ' |
Éric Araujo | 3b371cf | 2011-09-01 20:00:33 +0200 | [diff] [blame] | 353 | 'FILE to the list considered for compilation; ' |
| 354 | 'if "-", names are read from stdin')) |
R. David Murray | 94f58c3 | 2010-12-17 16:29:07 +0000 | [diff] [blame] | 355 | parser.add_argument('compile_dest', metavar='FILE|DIR', nargs='*', |
| 356 | help=('zero or more file and directory names ' |
| 357 | 'to compile; if no arguments given, defaults ' |
| 358 | 'to the equivalent of -l sys.path')) |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 359 | parser.add_argument('-j', '--workers', default=1, |
| 360 | type=int, help='Run compileall concurrently') |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 361 | invalidation_modes = [mode.name.lower().replace('_', '-') |
| 362 | for mode in py_compile.PycInvalidationMode] |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 363 | parser.add_argument('--invalidation-mode', |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 364 | choices=sorted(invalidation_modes), |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 365 | help=('set .pyc invalidation mode; defaults to ' |
| 366 | '"checked-hash" if the SOURCE_DATE_EPOCH ' |
| 367 | 'environment variable is set, and ' |
| 368 | '"timestamp" otherwise.')) |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 369 | parser.add_argument('-o', action='append', type=int, dest='opt_levels', |
Miss Islington (bot) | 8a5f14e | 2021-09-17 16:02:32 -0700 | [diff] [blame] | 370 | help=('Optimization levels to run compilation with. ' |
| 371 | 'Default is -1 which uses the optimization level ' |
| 372 | 'of the Python interpreter itself (see -O).')) |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 373 | parser.add_argument('-e', metavar='DIR', dest='limit_sl_dest', |
| 374 | help='Ignore symlinks pointing outsite of the DIR') |
Lumír 'Frenzy' Balhar | e77d428 | 2020-05-14 16:17:22 +0200 | [diff] [blame] | 375 | parser.add_argument('--hardlink-dupes', action='store_true', |
| 376 | dest='hardlink_dupes', |
| 377 | help='Hardlink duplicated pyc files') |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 378 | |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 379 | args = parser.parse_args() |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 380 | compile_dests = args.compile_dest |
| 381 | |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 382 | if args.rx: |
| 383 | import re |
| 384 | args.rx = re.compile(args.rx) |
| 385 | |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 386 | if args.limit_sl_dest == "": |
| 387 | args.limit_sl_dest = None |
Benjamin Peterson | 344ff4a | 2014-08-19 16:13:26 -0500 | [diff] [blame] | 388 | |
| 389 | if args.recursion is not None: |
| 390 | maxlevels = args.recursion |
| 391 | else: |
| 392 | maxlevels = args.maxlevels |
| 393 | |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 394 | if args.opt_levels is None: |
| 395 | args.opt_levels = [-1] |
| 396 | |
Lumír 'Frenzy' Balhar | e77d428 | 2020-05-14 16:17:22 +0200 | [diff] [blame] | 397 | if len(args.opt_levels) == 1 and args.hardlink_dupes: |
| 398 | parser.error(("Hardlinking of duplicated bytecode makes sense " |
| 399 | "only for more than one optimization level.")) |
| 400 | |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 401 | if args.ddir is not None and ( |
| 402 | args.stripdir is not None or args.prependdir is not None |
| 403 | ): |
| 404 | parser.error("-d cannot be used in combination with -s or -p") |
| 405 | |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 406 | # if flist is provided then load it |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 407 | if args.flist: |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 408 | try: |
Inada Naoki | 8001775 | 2021-04-02 09:01:57 +0900 | [diff] [blame] | 409 | with (sys.stdin if args.flist=='-' else |
| 410 | open(args.flist, encoding="utf-8")) as f: |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 411 | for line in f: |
| 412 | compile_dests.append(line.strip()) |
Andrew Svetlov | 3438fa4 | 2012-12-17 23:35:18 +0200 | [diff] [blame] | 413 | except OSError: |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 414 | if args.quiet < 2: |
| 415 | print("Error reading file list {}".format(args.flist)) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 416 | return False |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 417 | |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 418 | if args.invalidation_mode: |
| 419 | ivl_mode = args.invalidation_mode.replace('-', '_').upper() |
| 420 | invalidation_mode = py_compile.PycInvalidationMode[ivl_mode] |
| 421 | else: |
| 422 | invalidation_mode = None |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 423 | |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 424 | success = True |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 425 | try: |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 426 | if compile_dests: |
| 427 | for dest in compile_dests: |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 428 | if os.path.isfile(dest): |
| 429 | if not compile_file(dest, args.ddir, args.force, args.rx, |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 430 | args.quiet, args.legacy, |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 431 | invalidation_mode=invalidation_mode, |
| 432 | stripdir=args.stripdir, |
| 433 | prependdir=args.prependdir, |
| 434 | optimize=args.opt_levels, |
Lumír 'Frenzy' Balhar | e77d428 | 2020-05-14 16:17:22 +0200 | [diff] [blame] | 435 | limit_sl_dest=args.limit_sl_dest, |
| 436 | hardlink_dupes=args.hardlink_dupes): |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 437 | success = False |
| 438 | else: |
Benjamin Peterson | 344ff4a | 2014-08-19 16:13:26 -0500 | [diff] [blame] | 439 | if not compile_dir(dest, maxlevels, args.ddir, |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 440 | args.force, args.rx, args.quiet, |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 441 | args.legacy, workers=args.workers, |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 442 | invalidation_mode=invalidation_mode, |
| 443 | stripdir=args.stripdir, |
| 444 | prependdir=args.prependdir, |
| 445 | optimize=args.opt_levels, |
Lumír 'Frenzy' Balhar | e77d428 | 2020-05-14 16:17:22 +0200 | [diff] [blame] | 446 | limit_sl_dest=args.limit_sl_dest, |
| 447 | hardlink_dupes=args.hardlink_dupes): |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 448 | success = False |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 449 | return success |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 450 | else: |
R David Murray | 8a1d1e6 | 2013-12-15 20:49:38 -0500 | [diff] [blame] | 451 | return compile_path(legacy=args.legacy, force=args.force, |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 452 | quiet=args.quiet, |
| 453 | invalidation_mode=invalidation_mode) |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 454 | except KeyboardInterrupt: |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 455 | if args.quiet < 2: |
| 456 | print("\n[interrupted]") |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 457 | return False |
| 458 | return True |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 459 | |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 460 | |
| 461 | if __name__ == '__main__': |
Raymond Hettinger | 7b4b788 | 2004-12-20 00:29:29 +0000 | [diff] [blame] | 462 | exit_status = int(not main()) |
Jeremy Hylton | 12b6457 | 2001-04-18 01:20:21 +0000 | [diff] [blame] | 463 | sys.exit(exit_status) |