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