blob: 8cfde5b7b3e0aa9a8537f27a49df39868dc6a1db [file] [log] [blame]
Brett Cannonf299abd2015-04-13 14:21:02 -04001"""Module/script to byte-compile all .py files to .pyc files.
Guido van Rossumc567b811998-01-19 23:07:55 +00002
3When called as a script with arguments, this compiles the directories
4given as arguments recursively; the -l option prevents it from
5recursing into directories.
6
7Without arguments, if compiles all modules on sys.path, without
8recursing into subdirectories. (Even though it should do so for
9packages -- for now, you'll have to deal with packages separately.)
10
11See module py_compile for details of the actual byte-compilation.
Guido van Rossumc567b811998-01-19 23:07:55 +000012"""
Guido van Rossum3bb54481994-08-29 10:52:58 +000013import os
14import sys
Brett Cannon7822e122013-06-14 23:04:02 -040015import importlib.util
Guido van Rossum3bb54481994-08-29 10:52:58 +000016import py_compile
Brett Cannonbefb14f2009-02-10 02:10:16 +000017import struct
Guido van Rossum3bb54481994-08-29 10:52:58 +000018
Brett Cannonf1a8df02014-09-12 10:39:48 -040019from functools import partial
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +020020from pathlib import Path
21
Matthias Klosec33b9022010-03-16 00:36:26 +000022__all__ = ["compile_dir","compile_file","compile_path"]
Skip Montanaroe99d5ea2001-01-20 19:54:20 +000023
Victor Stinnereb1dda22019-10-15 11:26:13 +020024def _walk_dir(dir, maxlevels, quiet=0):
Berker Peksag812a2b62016-10-01 00:54:18 +030025 if quiet < 2 and isinstance(dir, os.PathLike):
26 dir = os.fspath(dir)
Brett Cannonf1a8df02014-09-12 10:39:48 -040027 if not quiet:
28 print('Listing {!r}...'.format(dir))
29 try:
30 names = os.listdir(dir)
31 except OSError:
Berker Peksag6554b862014-10-15 11:10:57 +030032 if quiet < 2:
33 print("Can't list {!r}".format(dir))
Brett Cannonf1a8df02014-09-12 10:39:48 -040034 names = []
35 names.sort()
36 for name in names:
37 if name == '__pycache__':
38 continue
39 fullname = os.path.join(dir, name)
Brett Cannonf1a8df02014-09-12 10:39:48 -040040 if not os.path.isdir(fullname):
41 yield fullname
42 elif (maxlevels > 0 and name != os.curdir and name != os.pardir and
43 os.path.isdir(fullname) and not os.path.islink(fullname)):
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +020044 yield from _walk_dir(fullname, maxlevels=maxlevels - 1,
45 quiet=quiet)
Brett Cannonf1a8df02014-09-12 10:39:48 -040046
Victor Stinnereb1dda22019-10-15 11:26:13 +020047def compile_dir(dir, maxlevels=None, ddir=None, force=False,
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +020048 rx=None, quiet=0, legacy=False, optimize=-1, workers=1,
49 invalidation_mode=None, stripdir=None,
50 prependdir=None, limit_sl_dest=None):
Guido van Rossumc567b811998-01-19 23:07:55 +000051 """Byte-compile all modules in the given directory tree.
Guido van Rossum3bb54481994-08-29 10:52:58 +000052
Guido van Rossumc567b811998-01-19 23:07:55 +000053 Arguments (only dir is required):
54
55 dir: the directory to byte-compile
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +020056 maxlevels: maximum recursion level (default `sys.getrecursionlimit()`)
R. David Murray94f58c32010-12-17 16:29:07 +000057 ddir: the directory that will be prepended to the path to the
58 file as it is compiled into each byte-code file.
Barry Warsaw28a691b2010-04-17 00:19:56 +000059 force: if True, force compilation, even if timestamps are up-to-date
Berker Peksag6554b862014-10-15 11:10:57 +030060 quiet: full output with False or 0, errors only with 1,
61 no output with 2
Barry Warsaw28a691b2010-04-17 00:19:56 +000062 legacy: if True, produce legacy pyc paths instead of PEP 3147 paths
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +020063 optimize: int or list of optimization levels or -1 for level of
64 the interpreter. Multiple levels leads to multiple compiled
65 files each with one optimization level.
Brett Cannonf1a8df02014-09-12 10:39:48 -040066 workers: maximum number of parallel workers
Benjamin Peterson42aa93b2017-12-09 10:26:52 -080067 invalidation_mode: how the up-to-dateness of the pyc will be checked
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +020068 stripdir: part of path to left-strip from source file path
69 prependdir: path to prepend to beggining of original file path, applied
70 after stripdir
71 limit_sl_dest: ignore symlinks if they are pointing outside of
72 the defined path
Guido van Rossumc567b811998-01-19 23:07:55 +000073 """
Dustin Spicuzza1d817e42018-11-23 12:06:55 -050074 ProcessPoolExecutor = None
Antoine Pitrou1a2dd822019-05-15 23:45:18 +020075 if workers < 0:
76 raise ValueError('workers must be greater or equal to 0')
77 if workers != 1:
78 try:
79 # Only import when needed, as low resource platforms may
80 # fail to import it
81 from concurrent.futures import ProcessPoolExecutor
82 except ImportError:
83 workers = 1
Victor Stinnereb1dda22019-10-15 11:26:13 +020084 if maxlevels is None:
85 maxlevels = sys.getrecursionlimit()
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +020086 files = _walk_dir(dir, quiet=quiet, maxlevels=maxlevels)
Brett Cannon1e3c3e92015-12-27 13:17:04 -080087 success = True
Antoine Pitrou1a2dd822019-05-15 23:45:18 +020088 if workers != 1 and ProcessPoolExecutor is not None:
89 # If workers == 0, let ProcessPoolExecutor choose
Brett Cannonf1a8df02014-09-12 10:39:48 -040090 workers = workers or None
91 with ProcessPoolExecutor(max_workers=workers) as executor:
92 results = executor.map(partial(compile_file,
93 ddir=ddir, force=force,
94 rx=rx, quiet=quiet,
95 legacy=legacy,
Benjamin Peterson42aa93b2017-12-09 10:26:52 -080096 optimize=optimize,
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +020097 invalidation_mode=invalidation_mode,
98 stripdir=stripdir,
99 prependdir=prependdir,
100 limit_sl_dest=limit_sl_dest),
Brett Cannonf1a8df02014-09-12 10:39:48 -0400101 files)
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800102 success = min(results, default=True)
Brett Cannonf1a8df02014-09-12 10:39:48 -0400103 else:
104 for file in files:
105 if not compile_file(file, ddir, force, rx, quiet,
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200106 legacy, optimize, invalidation_mode,
107 stripdir=stripdir, prependdir=prependdir,
108 limit_sl_dest=limit_sl_dest):
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800109 success = False
Fred Drake9065ea31999-03-29 20:25:40 +0000110 return success
Guido van Rossumc567b811998-01-19 23:07:55 +0000111
Berker Peksag6554b862014-10-15 11:10:57 +0300112def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800113 legacy=False, optimize=-1,
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200114 invalidation_mode=None, stripdir=None, prependdir=None,
115 limit_sl_dest=None):
Éric Araujo413d7b42010-12-23 18:44:31 +0000116 """Byte-compile one file.
117
118 Arguments (only fullname is required):
119
Barry Warsaw28a691b2010-04-17 00:19:56 +0000120 fullname: the file to byte-compile
R. David Murray94f58c32010-12-17 16:29:07 +0000121 ddir: if given, the directory name compiled in to the
122 byte-code file.
Barry Warsaw28a691b2010-04-17 00:19:56 +0000123 force: if True, force compilation, even if timestamps are up-to-date
Berker Peksag6554b862014-10-15 11:10:57 +0300124 quiet: full output with False or 0, errors only with 1,
125 no output with 2
Barry Warsaw28a691b2010-04-17 00:19:56 +0000126 legacy: if True, produce legacy pyc paths instead of PEP 3147 paths
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200127 optimize: int or list of optimization levels or -1 for level of
128 the interpreter. Multiple levels leads to multiple compiled
129 files each with one optimization level.
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800130 invalidation_mode: how the up-to-dateness of the pyc will be checked
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200131 stripdir: part of path to left-strip from source file path
132 prependdir: path to prepend to beggining of original file path, applied
133 after stripdir
134 limit_sl_dest: ignore symlinks if they are pointing outside of
135 the defined path.
Matthias Klosec33b9022010-03-16 00:36:26 +0000136 """
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200137
138 if ddir is not None and (stripdir is not None or prependdir is not None):
139 raise ValueError(("Destination dir (ddir) cannot be used "
140 "in combination with stripdir or prependdir"))
141
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800142 success = True
Berker Peksag812a2b62016-10-01 00:54:18 +0300143 if quiet < 2 and isinstance(fullname, os.PathLike):
144 fullname = os.fspath(fullname)
Matthias Klosec33b9022010-03-16 00:36:26 +0000145 name = os.path.basename(fullname)
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200146
147 dfile = None
148
Matthias Klosec33b9022010-03-16 00:36:26 +0000149 if ddir is not None:
150 dfile = os.path.join(ddir, name)
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200151
152 if stripdir is not None:
153 fullname_parts = fullname.split(os.path.sep)
154 stripdir_parts = stripdir.split(os.path.sep)
155 ddir_parts = list(fullname_parts)
156
157 for spart, opart in zip(stripdir_parts, fullname_parts):
158 if spart == opart:
159 ddir_parts.remove(spart)
160
161 dfile = os.path.join(*ddir_parts)
162
163 if prependdir is not None:
164 if dfile is None:
165 dfile = os.path.join(prependdir, fullname)
166 else:
167 dfile = os.path.join(prependdir, dfile)
168
169 if isinstance(optimize, int):
170 optimize = [optimize]
171
Matthias Klosec33b9022010-03-16 00:36:26 +0000172 if rx is not None:
173 mo = rx.search(fullname)
174 if mo:
175 return success
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200176
177 if limit_sl_dest is not None and os.path.islink(fullname):
178 if Path(limit_sl_dest).resolve() not in Path(fullname).resolve().parents:
179 return success
180
181 opt_cfiles = {}
182
Matthias Klosec33b9022010-03-16 00:36:26 +0000183 if os.path.isfile(fullname):
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200184 for opt_level in optimize:
185 if legacy:
186 opt_cfiles[opt_level] = fullname + 'c'
Georg Brandl8334fd92010-12-04 10:26:46 +0000187 else:
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200188 if opt_level >= 0:
189 opt = opt_level if opt_level >= 1 else ''
190 cfile = (importlib.util.cache_from_source(
191 fullname, optimization=opt))
192 opt_cfiles[opt_level] = cfile
193 else:
194 cfile = importlib.util.cache_from_source(fullname)
195 opt_cfiles[opt_level] = cfile
196
Matthias Klosec33b9022010-03-16 00:36:26 +0000197 head, tail = name[:-3], name[-3:]
198 if tail == '.py':
199 if not force:
200 try:
201 mtime = int(os.stat(fullname).st_mtime)
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800202 expect = struct.pack('<4sll', importlib.util.MAGIC_NUMBER,
203 0, mtime)
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200204 for cfile in opt_cfiles.values():
205 with open(cfile, 'rb') as chandle:
206 actual = chandle.read(12)
207 if expect != actual:
208 break
209 else:
Matthias Klosec33b9022010-03-16 00:36:26 +0000210 return success
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200211 except OSError:
Matthias Klosec33b9022010-03-16 00:36:26 +0000212 pass
213 if not quiet:
Victor Stinner53071262011-05-11 00:36:28 +0200214 print('Compiling {!r}...'.format(fullname))
Matthias Klosec33b9022010-03-16 00:36:26 +0000215 try:
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200216 for opt_level, cfile in opt_cfiles.items():
217 ok = py_compile.compile(fullname, cfile, dfile, True,
218 optimize=opt_level,
219 invalidation_mode=invalidation_mode)
Matthias Klosec33b9022010-03-16 00:36:26 +0000220 except py_compile.PyCompileError as err:
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800221 success = False
Berker Peksag6554b862014-10-15 11:10:57 +0300222 if quiet >= 2:
223 return success
224 elif quiet:
Victor Stinner53071262011-05-11 00:36:28 +0200225 print('*** Error compiling {!r}...'.format(fullname))
Matthias Klosec33b9022010-03-16 00:36:26 +0000226 else:
227 print('*** ', end='')
Martin v. Löwis4b003072010-03-16 13:19:21 +0000228 # escape non-printable characters in msg
Barry Warsaw28a691b2010-04-17 00:19:56 +0000229 msg = err.msg.encode(sys.stdout.encoding,
230 errors='backslashreplace')
Martin v. Löwis4b003072010-03-16 13:19:21 +0000231 msg = msg.decode(sys.stdout.encoding)
232 print(msg)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200233 except (SyntaxError, UnicodeError, OSError) as e:
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800234 success = False
Berker Peksag6554b862014-10-15 11:10:57 +0300235 if quiet >= 2:
236 return success
237 elif quiet:
Victor Stinner53071262011-05-11 00:36:28 +0200238 print('*** Error compiling {!r}...'.format(fullname))
Matthias Klosec33b9022010-03-16 00:36:26 +0000239 else:
240 print('*** ', end='')
241 print(e.__class__.__name__ + ':', e)
Matthias Klosec33b9022010-03-16 00:36:26 +0000242 else:
243 if ok == 0:
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800244 success = False
Matthias Klosec33b9022010-03-16 00:36:26 +0000245 return success
246
Berker Peksag6554b862014-10-15 11:10:57 +0300247def compile_path(skip_curdir=1, maxlevels=0, force=False, quiet=0,
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800248 legacy=False, optimize=-1,
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -0400249 invalidation_mode=None):
Guido van Rossumc567b811998-01-19 23:07:55 +0000250 """Byte-compile all module on sys.path.
251
252 Arguments (all optional):
253
Éric Araujo3b371cf2011-09-01 20:00:33 +0200254 skip_curdir: if true, skip current directory (default True)
Guido van Rossumc567b811998-01-19 23:07:55 +0000255 maxlevels: max recursion level (default 0)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000256 force: as for compile_dir() (default False)
Berker Peksag6554b862014-10-15 11:10:57 +0300257 quiet: as for compile_dir() (default 0)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000258 legacy: as for compile_dir() (default False)
Georg Brandl8334fd92010-12-04 10:26:46 +0000259 optimize: as for compile_dir() (default -1)
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800260 invalidation_mode: as for compiler_dir()
Guido van Rossumc567b811998-01-19 23:07:55 +0000261 """
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800262 success = True
Guido van Rossumc567b811998-01-19 23:07:55 +0000263 for dir in sys.path:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000264 if (not dir or dir == os.curdir) and skip_curdir:
Berker Peksag6554b862014-10-15 11:10:57 +0300265 if quiet < 2:
266 print('Skipping current directory')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000267 else:
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800268 success = success and compile_dir(
269 dir,
270 maxlevels,
271 None,
272 force,
273 quiet=quiet,
274 legacy=legacy,
275 optimize=optimize,
276 invalidation_mode=invalidation_mode,
277 )
Fred Drake9065ea31999-03-29 20:25:40 +0000278 return success
Guido van Rossum3bb54481994-08-29 10:52:58 +0000279
Matthias Klosec33b9022010-03-16 00:36:26 +0000280
Guido van Rossum3bb54481994-08-29 10:52:58 +0000281def main():
Guido van Rossumc567b811998-01-19 23:07:55 +0000282 """Script main program."""
R. David Murray650f1472010-11-20 21:18:51 +0000283 import argparse
284
285 parser = argparse.ArgumentParser(
286 description='Utilities to support installing Python libraries.')
R. David Murray94f58c32010-12-17 16:29:07 +0000287 parser.add_argument('-l', action='store_const', const=0,
Victor Stinnereb1dda22019-10-15 11:26:13 +0200288 default=None, dest='maxlevels',
R. David Murray94f58c32010-12-17 16:29:07 +0000289 help="don't recurse into subdirectories")
Benjamin Peterson344ff4a2014-08-19 16:13:26 -0500290 parser.add_argument('-r', type=int, dest='recursion',
291 help=('control the maximum recursion level. '
292 'if `-l` and `-r` options are specified, '
293 'then `-r` takes precedence.'))
R. David Murray650f1472010-11-20 21:18:51 +0000294 parser.add_argument('-f', action='store_true', dest='force',
295 help='force rebuild even if timestamps are up to date')
Berker Peksag6554b862014-10-15 11:10:57 +0300296 parser.add_argument('-q', action='count', dest='quiet', default=0,
297 help='output only error messages; -qq will suppress '
298 'the error messages as well.')
R. David Murray650f1472010-11-20 21:18:51 +0000299 parser.add_argument('-b', action='store_true', dest='legacy',
R. David Murray94f58c32010-12-17 16:29:07 +0000300 help='use legacy (pre-PEP3147) compiled file locations')
R. David Murray650f1472010-11-20 21:18:51 +0000301 parser.add_argument('-d', metavar='DESTDIR', dest='ddir', default=None,
R. David Murray94f58c32010-12-17 16:29:07 +0000302 help=('directory to prepend to file paths for use in '
Éric Araujo3b371cf2011-09-01 20:00:33 +0200303 'compile-time tracebacks and in runtime '
R. David Murray94f58c32010-12-17 16:29:07 +0000304 'tracebacks in cases where the source file is '
305 'unavailable'))
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200306 parser.add_argument('-s', metavar='STRIPDIR', dest='stripdir',
307 default=None,
308 help=('part of path to left-strip from path '
309 'to source file - for example buildroot. '
310 '`-d` and `-s` options cannot be '
311 'specified together.'))
312 parser.add_argument('-p', metavar='PREPENDDIR', dest='prependdir',
313 default=None,
314 help=('path to add as prefix to path '
315 'to source file - for example / to make '
316 'it absolute when some part is removed '
317 'by `-s` option. '
318 '`-d` and `-p` options cannot be '
319 'specified together.'))
R. David Murray650f1472010-11-20 21:18:51 +0000320 parser.add_argument('-x', metavar='REGEXP', dest='rx', default=None,
Éric Araujo3b371cf2011-09-01 20:00:33 +0200321 help=('skip files matching the regular expression; '
322 'the regexp is searched for in the full path '
323 'of each file considered for compilation'))
R. David Murray650f1472010-11-20 21:18:51 +0000324 parser.add_argument('-i', metavar='FILE', dest='flist',
R. David Murray94f58c32010-12-17 16:29:07 +0000325 help=('add all the files and directories listed in '
Éric Araujo3b371cf2011-09-01 20:00:33 +0200326 'FILE to the list considered for compilation; '
327 'if "-", names are read from stdin'))
R. David Murray94f58c32010-12-17 16:29:07 +0000328 parser.add_argument('compile_dest', metavar='FILE|DIR', nargs='*',
329 help=('zero or more file and directory names '
330 'to compile; if no arguments given, defaults '
331 'to the equivalent of -l sys.path'))
Brett Cannonf1a8df02014-09-12 10:39:48 -0400332 parser.add_argument('-j', '--workers', default=1,
333 type=int, help='Run compileall concurrently')
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800334 invalidation_modes = [mode.name.lower().replace('_', '-')
335 for mode in py_compile.PycInvalidationMode]
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -0400336 parser.add_argument('--invalidation-mode',
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800337 choices=sorted(invalidation_modes),
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -0400338 help=('set .pyc invalidation mode; defaults to '
339 '"checked-hash" if the SOURCE_DATE_EPOCH '
340 'environment variable is set, and '
341 '"timestamp" otherwise.'))
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200342 parser.add_argument('-o', action='append', type=int, dest='opt_levels',
343 help=('Optimization levels to run compilation with.'
344 'Default is -1 which uses optimization level of'
345 'Python interpreter itself (specified by -O).'))
346 parser.add_argument('-e', metavar='DIR', dest='limit_sl_dest',
347 help='Ignore symlinks pointing outsite of the DIR')
R. David Murray650f1472010-11-20 21:18:51 +0000348
Brett Cannonf1a8df02014-09-12 10:39:48 -0400349 args = parser.parse_args()
R. David Murray95333e32010-12-14 22:32:50 +0000350 compile_dests = args.compile_dest
351
R. David Murray650f1472010-11-20 21:18:51 +0000352 if args.rx:
353 import re
354 args.rx = re.compile(args.rx)
355
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200356 if args.limit_sl_dest == "":
357 args.limit_sl_dest = None
Benjamin Peterson344ff4a2014-08-19 16:13:26 -0500358
359 if args.recursion is not None:
360 maxlevels = args.recursion
361 else:
362 maxlevels = args.maxlevels
363
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200364 if args.opt_levels is None:
365 args.opt_levels = [-1]
366
367 if args.ddir is not None and (
368 args.stripdir is not None or args.prependdir is not None
369 ):
370 parser.error("-d cannot be used in combination with -s or -p")
371
R. David Murray650f1472010-11-20 21:18:51 +0000372 # if flist is provided then load it
R. David Murray650f1472010-11-20 21:18:51 +0000373 if args.flist:
R. David Murray95333e32010-12-14 22:32:50 +0000374 try:
375 with (sys.stdin if args.flist=='-' else open(args.flist)) as f:
376 for line in f:
377 compile_dests.append(line.strip())
Andrew Svetlov3438fa42012-12-17 23:35:18 +0200378 except OSError:
Berker Peksag6554b862014-10-15 11:10:57 +0300379 if args.quiet < 2:
380 print("Error reading file list {}".format(args.flist))
R. David Murray95333e32010-12-14 22:32:50 +0000381 return False
R. David Murray650f1472010-11-20 21:18:51 +0000382
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -0400383 if args.invalidation_mode:
384 ivl_mode = args.invalidation_mode.replace('-', '_').upper()
385 invalidation_mode = py_compile.PycInvalidationMode[ivl_mode]
386 else:
387 invalidation_mode = None
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800388
R. David Murray95333e32010-12-14 22:32:50 +0000389 success = True
Guido van Rossumc567b811998-01-19 23:07:55 +0000390 try:
R. David Murray650f1472010-11-20 21:18:51 +0000391 if compile_dests:
392 for dest in compile_dests:
R. David Murray5317e9c2010-12-16 19:08:51 +0000393 if os.path.isfile(dest):
394 if not compile_file(dest, args.ddir, args.force, args.rx,
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800395 args.quiet, args.legacy,
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200396 invalidation_mode=invalidation_mode,
397 stripdir=args.stripdir,
398 prependdir=args.prependdir,
399 optimize=args.opt_levels,
400 limit_sl_dest=args.limit_sl_dest):
R. David Murray5317e9c2010-12-16 19:08:51 +0000401 success = False
402 else:
Benjamin Peterson344ff4a2014-08-19 16:13:26 -0500403 if not compile_dir(dest, maxlevels, args.ddir,
R. David Murray650f1472010-11-20 21:18:51 +0000404 args.force, args.rx, args.quiet,
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800405 args.legacy, workers=args.workers,
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200406 invalidation_mode=invalidation_mode,
407 stripdir=args.stripdir,
408 prependdir=args.prependdir,
409 optimize=args.opt_levels,
410 limit_sl_dest=args.limit_sl_dest):
R. David Murray95333e32010-12-14 22:32:50 +0000411 success = False
R. David Murray95333e32010-12-14 22:32:50 +0000412 return success
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000413 else:
R David Murray8a1d1e62013-12-15 20:49:38 -0500414 return compile_path(legacy=args.legacy, force=args.force,
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800415 quiet=args.quiet,
416 invalidation_mode=invalidation_mode)
Guido van Rossumc567b811998-01-19 23:07:55 +0000417 except KeyboardInterrupt:
Berker Peksag6554b862014-10-15 11:10:57 +0300418 if args.quiet < 2:
419 print("\n[interrupted]")
R. David Murray95333e32010-12-14 22:32:50 +0000420 return False
421 return True
R. David Murray650f1472010-11-20 21:18:51 +0000422
Guido van Rossum3bb54481994-08-29 10:52:58 +0000423
424if __name__ == '__main__':
Raymond Hettinger7b4b7882004-12-20 00:29:29 +0000425 exit_status = int(not main())
Jeremy Hylton12b64572001-04-18 01:20:21 +0000426 sys.exit(exit_status)