blob: 1831ad749f2b17882b357580d3c582c6571434c5 [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,
Gregory P. Smith02673352020-02-28 17:28:37 -080049 invalidation_mode=None, *, stripdir=None,
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +020050 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
Gregory P. Smith02673352020-02-28 17:28:37 -080075 if ddir is not None and (stripdir is not None or prependdir is not None):
76 raise ValueError(("Destination dir (ddir) cannot be used "
77 "in combination with stripdir or prependdir"))
78 if ddir is not None:
79 stripdir = dir
80 prependdir = ddir
81 ddir = None
Antoine Pitrou1a2dd822019-05-15 23:45:18 +020082 if workers < 0:
83 raise ValueError('workers must be greater or equal to 0')
84 if workers != 1:
85 try:
86 # Only import when needed, as low resource platforms may
87 # fail to import it
88 from concurrent.futures import ProcessPoolExecutor
89 except ImportError:
90 workers = 1
Victor Stinnereb1dda22019-10-15 11:26:13 +020091 if maxlevels is None:
92 maxlevels = sys.getrecursionlimit()
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +020093 files = _walk_dir(dir, quiet=quiet, maxlevels=maxlevels)
Brett Cannon1e3c3e92015-12-27 13:17:04 -080094 success = True
Antoine Pitrou1a2dd822019-05-15 23:45:18 +020095 if workers != 1 and ProcessPoolExecutor is not None:
96 # If workers == 0, let ProcessPoolExecutor choose
Brett Cannonf1a8df02014-09-12 10:39:48 -040097 workers = workers or None
98 with ProcessPoolExecutor(max_workers=workers) as executor:
99 results = executor.map(partial(compile_file,
100 ddir=ddir, force=force,
101 rx=rx, quiet=quiet,
102 legacy=legacy,
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800103 optimize=optimize,
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200104 invalidation_mode=invalidation_mode,
105 stripdir=stripdir,
106 prependdir=prependdir,
107 limit_sl_dest=limit_sl_dest),
Brett Cannonf1a8df02014-09-12 10:39:48 -0400108 files)
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800109 success = min(results, default=True)
Brett Cannonf1a8df02014-09-12 10:39:48 -0400110 else:
111 for file in files:
112 if not compile_file(file, ddir, force, rx, quiet,
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200113 legacy, optimize, invalidation_mode,
114 stripdir=stripdir, prependdir=prependdir,
115 limit_sl_dest=limit_sl_dest):
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800116 success = False
Fred Drake9065ea31999-03-29 20:25:40 +0000117 return success
Guido van Rossumc567b811998-01-19 23:07:55 +0000118
Berker Peksag6554b862014-10-15 11:10:57 +0300119def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800120 legacy=False, optimize=-1,
Gregory P. Smith02673352020-02-28 17:28:37 -0800121 invalidation_mode=None, *, stripdir=None, prependdir=None,
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200122 limit_sl_dest=None):
Éric Araujo413d7b42010-12-23 18:44:31 +0000123 """Byte-compile one file.
124
125 Arguments (only fullname is required):
126
Barry Warsaw28a691b2010-04-17 00:19:56 +0000127 fullname: the file to byte-compile
R. David Murray94f58c32010-12-17 16:29:07 +0000128 ddir: if given, the directory name compiled in to the
129 byte-code file.
Barry Warsaw28a691b2010-04-17 00:19:56 +0000130 force: if True, force compilation, even if timestamps are up-to-date
Berker Peksag6554b862014-10-15 11:10:57 +0300131 quiet: full output with False or 0, errors only with 1,
132 no output with 2
Barry Warsaw28a691b2010-04-17 00:19:56 +0000133 legacy: if True, produce legacy pyc paths instead of PEP 3147 paths
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200134 optimize: int or list of optimization levels or -1 for level of
135 the interpreter. Multiple levels leads to multiple compiled
136 files each with one optimization level.
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800137 invalidation_mode: how the up-to-dateness of the pyc will be checked
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200138 stripdir: part of path to left-strip from source file path
139 prependdir: path to prepend to beggining of original file path, applied
140 after stripdir
141 limit_sl_dest: ignore symlinks if they are pointing outside of
142 the defined path.
Matthias Klosec33b9022010-03-16 00:36:26 +0000143 """
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200144
145 if ddir is not None and (stripdir is not None or prependdir is not None):
146 raise ValueError(("Destination dir (ddir) cannot be used "
147 "in combination with stripdir or prependdir"))
148
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800149 success = True
Berker Peksag812a2b62016-10-01 00:54:18 +0300150 if quiet < 2 and isinstance(fullname, os.PathLike):
151 fullname = os.fspath(fullname)
Matthias Klosec33b9022010-03-16 00:36:26 +0000152 name = os.path.basename(fullname)
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200153
154 dfile = None
155
Matthias Klosec33b9022010-03-16 00:36:26 +0000156 if ddir is not None:
157 dfile = os.path.join(ddir, name)
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200158
159 if stripdir is not None:
160 fullname_parts = fullname.split(os.path.sep)
161 stripdir_parts = stripdir.split(os.path.sep)
162 ddir_parts = list(fullname_parts)
163
164 for spart, opart in zip(stripdir_parts, fullname_parts):
165 if spart == opart:
166 ddir_parts.remove(spart)
167
168 dfile = os.path.join(*ddir_parts)
169
170 if prependdir is not None:
171 if dfile is None:
172 dfile = os.path.join(prependdir, fullname)
173 else:
174 dfile = os.path.join(prependdir, dfile)
175
176 if isinstance(optimize, int):
177 optimize = [optimize]
178
Matthias Klosec33b9022010-03-16 00:36:26 +0000179 if rx is not None:
180 mo = rx.search(fullname)
181 if mo:
182 return success
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200183
184 if limit_sl_dest is not None and os.path.islink(fullname):
185 if Path(limit_sl_dest).resolve() not in Path(fullname).resolve().parents:
186 return success
187
188 opt_cfiles = {}
189
Matthias Klosec33b9022010-03-16 00:36:26 +0000190 if os.path.isfile(fullname):
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200191 for opt_level in optimize:
192 if legacy:
193 opt_cfiles[opt_level] = fullname + 'c'
Georg Brandl8334fd92010-12-04 10:26:46 +0000194 else:
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200195 if opt_level >= 0:
196 opt = opt_level if opt_level >= 1 else ''
197 cfile = (importlib.util.cache_from_source(
198 fullname, optimization=opt))
199 opt_cfiles[opt_level] = cfile
200 else:
201 cfile = importlib.util.cache_from_source(fullname)
202 opt_cfiles[opt_level] = cfile
203
Matthias Klosec33b9022010-03-16 00:36:26 +0000204 head, tail = name[:-3], name[-3:]
205 if tail == '.py':
206 if not force:
207 try:
208 mtime = int(os.stat(fullname).st_mtime)
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800209 expect = struct.pack('<4sll', importlib.util.MAGIC_NUMBER,
210 0, mtime)
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200211 for cfile in opt_cfiles.values():
212 with open(cfile, 'rb') as chandle:
213 actual = chandle.read(12)
214 if expect != actual:
215 break
216 else:
Matthias Klosec33b9022010-03-16 00:36:26 +0000217 return success
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200218 except OSError:
Matthias Klosec33b9022010-03-16 00:36:26 +0000219 pass
220 if not quiet:
Victor Stinner53071262011-05-11 00:36:28 +0200221 print('Compiling {!r}...'.format(fullname))
Matthias Klosec33b9022010-03-16 00:36:26 +0000222 try:
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200223 for opt_level, cfile in opt_cfiles.items():
224 ok = py_compile.compile(fullname, cfile, dfile, True,
225 optimize=opt_level,
226 invalidation_mode=invalidation_mode)
Matthias Klosec33b9022010-03-16 00:36:26 +0000227 except py_compile.PyCompileError as err:
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800228 success = False
Berker Peksag6554b862014-10-15 11:10:57 +0300229 if quiet >= 2:
230 return success
231 elif quiet:
Victor Stinner53071262011-05-11 00:36:28 +0200232 print('*** Error compiling {!r}...'.format(fullname))
Matthias Klosec33b9022010-03-16 00:36:26 +0000233 else:
234 print('*** ', end='')
Martin v. Löwis4b003072010-03-16 13:19:21 +0000235 # escape non-printable characters in msg
Barry Warsaw28a691b2010-04-17 00:19:56 +0000236 msg = err.msg.encode(sys.stdout.encoding,
237 errors='backslashreplace')
Martin v. Löwis4b003072010-03-16 13:19:21 +0000238 msg = msg.decode(sys.stdout.encoding)
239 print(msg)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200240 except (SyntaxError, UnicodeError, OSError) as e:
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800241 success = False
Berker Peksag6554b862014-10-15 11:10:57 +0300242 if quiet >= 2:
243 return success
244 elif quiet:
Victor Stinner53071262011-05-11 00:36:28 +0200245 print('*** Error compiling {!r}...'.format(fullname))
Matthias Klosec33b9022010-03-16 00:36:26 +0000246 else:
247 print('*** ', end='')
248 print(e.__class__.__name__ + ':', e)
Matthias Klosec33b9022010-03-16 00:36:26 +0000249 else:
250 if ok == 0:
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800251 success = False
Matthias Klosec33b9022010-03-16 00:36:26 +0000252 return success
253
Berker Peksag6554b862014-10-15 11:10:57 +0300254def compile_path(skip_curdir=1, maxlevels=0, force=False, quiet=0,
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800255 legacy=False, optimize=-1,
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -0400256 invalidation_mode=None):
Guido van Rossumc567b811998-01-19 23:07:55 +0000257 """Byte-compile all module on sys.path.
258
259 Arguments (all optional):
260
Éric Araujo3b371cf2011-09-01 20:00:33 +0200261 skip_curdir: if true, skip current directory (default True)
Guido van Rossumc567b811998-01-19 23:07:55 +0000262 maxlevels: max recursion level (default 0)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000263 force: as for compile_dir() (default False)
Berker Peksag6554b862014-10-15 11:10:57 +0300264 quiet: as for compile_dir() (default 0)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000265 legacy: as for compile_dir() (default False)
Georg Brandl8334fd92010-12-04 10:26:46 +0000266 optimize: as for compile_dir() (default -1)
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800267 invalidation_mode: as for compiler_dir()
Guido van Rossumc567b811998-01-19 23:07:55 +0000268 """
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800269 success = True
Guido van Rossumc567b811998-01-19 23:07:55 +0000270 for dir in sys.path:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000271 if (not dir or dir == os.curdir) and skip_curdir:
Berker Peksag6554b862014-10-15 11:10:57 +0300272 if quiet < 2:
273 print('Skipping current directory')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000274 else:
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800275 success = success and compile_dir(
276 dir,
277 maxlevels,
278 None,
279 force,
280 quiet=quiet,
281 legacy=legacy,
282 optimize=optimize,
283 invalidation_mode=invalidation_mode,
284 )
Fred Drake9065ea31999-03-29 20:25:40 +0000285 return success
Guido van Rossum3bb54481994-08-29 10:52:58 +0000286
Matthias Klosec33b9022010-03-16 00:36:26 +0000287
Guido van Rossum3bb54481994-08-29 10:52:58 +0000288def main():
Guido van Rossumc567b811998-01-19 23:07:55 +0000289 """Script main program."""
R. David Murray650f1472010-11-20 21:18:51 +0000290 import argparse
291
292 parser = argparse.ArgumentParser(
293 description='Utilities to support installing Python libraries.')
R. David Murray94f58c32010-12-17 16:29:07 +0000294 parser.add_argument('-l', action='store_const', const=0,
Victor Stinnereb1dda22019-10-15 11:26:13 +0200295 default=None, dest='maxlevels',
R. David Murray94f58c32010-12-17 16:29:07 +0000296 help="don't recurse into subdirectories")
Benjamin Peterson344ff4a2014-08-19 16:13:26 -0500297 parser.add_argument('-r', type=int, dest='recursion',
298 help=('control the maximum recursion level. '
299 'if `-l` and `-r` options are specified, '
300 'then `-r` takes precedence.'))
R. David Murray650f1472010-11-20 21:18:51 +0000301 parser.add_argument('-f', action='store_true', dest='force',
302 help='force rebuild even if timestamps are up to date')
Berker Peksag6554b862014-10-15 11:10:57 +0300303 parser.add_argument('-q', action='count', dest='quiet', default=0,
304 help='output only error messages; -qq will suppress '
305 'the error messages as well.')
R. David Murray650f1472010-11-20 21:18:51 +0000306 parser.add_argument('-b', action='store_true', dest='legacy',
R. David Murray94f58c32010-12-17 16:29:07 +0000307 help='use legacy (pre-PEP3147) compiled file locations')
R. David Murray650f1472010-11-20 21:18:51 +0000308 parser.add_argument('-d', metavar='DESTDIR', dest='ddir', default=None,
R. David Murray94f58c32010-12-17 16:29:07 +0000309 help=('directory to prepend to file paths for use in '
Éric Araujo3b371cf2011-09-01 20:00:33 +0200310 'compile-time tracebacks and in runtime '
R. David Murray94f58c32010-12-17 16:29:07 +0000311 'tracebacks in cases where the source file is '
312 'unavailable'))
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200313 parser.add_argument('-s', metavar='STRIPDIR', dest='stripdir',
314 default=None,
315 help=('part of path to left-strip from path '
316 'to source file - for example buildroot. '
317 '`-d` and `-s` options cannot be '
318 'specified together.'))
319 parser.add_argument('-p', metavar='PREPENDDIR', dest='prependdir',
320 default=None,
321 help=('path to add as prefix to path '
322 'to source file - for example / to make '
323 'it absolute when some part is removed '
324 'by `-s` option. '
325 '`-d` and `-p` options cannot be '
326 'specified together.'))
R. David Murray650f1472010-11-20 21:18:51 +0000327 parser.add_argument('-x', metavar='REGEXP', dest='rx', default=None,
Éric Araujo3b371cf2011-09-01 20:00:33 +0200328 help=('skip files matching the regular expression; '
329 'the regexp is searched for in the full path '
330 'of each file considered for compilation'))
R. David Murray650f1472010-11-20 21:18:51 +0000331 parser.add_argument('-i', metavar='FILE', dest='flist',
R. David Murray94f58c32010-12-17 16:29:07 +0000332 help=('add all the files and directories listed in '
Éric Araujo3b371cf2011-09-01 20:00:33 +0200333 'FILE to the list considered for compilation; '
334 'if "-", names are read from stdin'))
R. David Murray94f58c32010-12-17 16:29:07 +0000335 parser.add_argument('compile_dest', metavar='FILE|DIR', nargs='*',
336 help=('zero or more file and directory names '
337 'to compile; if no arguments given, defaults '
338 'to the equivalent of -l sys.path'))
Brett Cannonf1a8df02014-09-12 10:39:48 -0400339 parser.add_argument('-j', '--workers', default=1,
340 type=int, help='Run compileall concurrently')
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800341 invalidation_modes = [mode.name.lower().replace('_', '-')
342 for mode in py_compile.PycInvalidationMode]
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -0400343 parser.add_argument('--invalidation-mode',
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800344 choices=sorted(invalidation_modes),
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -0400345 help=('set .pyc invalidation mode; defaults to '
346 '"checked-hash" if the SOURCE_DATE_EPOCH '
347 'environment variable is set, and '
348 '"timestamp" otherwise.'))
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200349 parser.add_argument('-o', action='append', type=int, dest='opt_levels',
350 help=('Optimization levels to run compilation with.'
351 'Default is -1 which uses optimization level of'
352 'Python interpreter itself (specified by -O).'))
353 parser.add_argument('-e', metavar='DIR', dest='limit_sl_dest',
354 help='Ignore symlinks pointing outsite of the DIR')
R. David Murray650f1472010-11-20 21:18:51 +0000355
Brett Cannonf1a8df02014-09-12 10:39:48 -0400356 args = parser.parse_args()
R. David Murray95333e32010-12-14 22:32:50 +0000357 compile_dests = args.compile_dest
358
R. David Murray650f1472010-11-20 21:18:51 +0000359 if args.rx:
360 import re
361 args.rx = re.compile(args.rx)
362
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200363 if args.limit_sl_dest == "":
364 args.limit_sl_dest = None
Benjamin Peterson344ff4a2014-08-19 16:13:26 -0500365
366 if args.recursion is not None:
367 maxlevels = args.recursion
368 else:
369 maxlevels = args.maxlevels
370
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200371 if args.opt_levels is None:
372 args.opt_levels = [-1]
373
374 if args.ddir is not None and (
375 args.stripdir is not None or args.prependdir is not None
376 ):
377 parser.error("-d cannot be used in combination with -s or -p")
378
R. David Murray650f1472010-11-20 21:18:51 +0000379 # if flist is provided then load it
R. David Murray650f1472010-11-20 21:18:51 +0000380 if args.flist:
R. David Murray95333e32010-12-14 22:32:50 +0000381 try:
382 with (sys.stdin if args.flist=='-' else open(args.flist)) as f:
383 for line in f:
384 compile_dests.append(line.strip())
Andrew Svetlov3438fa42012-12-17 23:35:18 +0200385 except OSError:
Berker Peksag6554b862014-10-15 11:10:57 +0300386 if args.quiet < 2:
387 print("Error reading file list {}".format(args.flist))
R. David Murray95333e32010-12-14 22:32:50 +0000388 return False
R. David Murray650f1472010-11-20 21:18:51 +0000389
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -0400390 if args.invalidation_mode:
391 ivl_mode = args.invalidation_mode.replace('-', '_').upper()
392 invalidation_mode = py_compile.PycInvalidationMode[ivl_mode]
393 else:
394 invalidation_mode = None
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800395
R. David Murray95333e32010-12-14 22:32:50 +0000396 success = True
Guido van Rossumc567b811998-01-19 23:07:55 +0000397 try:
R. David Murray650f1472010-11-20 21:18:51 +0000398 if compile_dests:
399 for dest in compile_dests:
R. David Murray5317e9c2010-12-16 19:08:51 +0000400 if os.path.isfile(dest):
401 if not compile_file(dest, args.ddir, args.force, args.rx,
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800402 args.quiet, args.legacy,
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200403 invalidation_mode=invalidation_mode,
404 stripdir=args.stripdir,
405 prependdir=args.prependdir,
406 optimize=args.opt_levels,
407 limit_sl_dest=args.limit_sl_dest):
R. David Murray5317e9c2010-12-16 19:08:51 +0000408 success = False
409 else:
Benjamin Peterson344ff4a2014-08-19 16:13:26 -0500410 if not compile_dir(dest, maxlevels, args.ddir,
R. David Murray650f1472010-11-20 21:18:51 +0000411 args.force, args.rx, args.quiet,
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800412 args.legacy, workers=args.workers,
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200413 invalidation_mode=invalidation_mode,
414 stripdir=args.stripdir,
415 prependdir=args.prependdir,
416 optimize=args.opt_levels,
417 limit_sl_dest=args.limit_sl_dest):
R. David Murray95333e32010-12-14 22:32:50 +0000418 success = False
R. David Murray95333e32010-12-14 22:32:50 +0000419 return success
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000420 else:
R David Murray8a1d1e62013-12-15 20:49:38 -0500421 return compile_path(legacy=args.legacy, force=args.force,
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800422 quiet=args.quiet,
423 invalidation_mode=invalidation_mode)
Guido van Rossumc567b811998-01-19 23:07:55 +0000424 except KeyboardInterrupt:
Berker Peksag6554b862014-10-15 11:10:57 +0300425 if args.quiet < 2:
426 print("\n[interrupted]")
R. David Murray95333e32010-12-14 22:32:50 +0000427 return False
428 return True
R. David Murray650f1472010-11-20 21:18:51 +0000429
Guido van Rossum3bb54481994-08-29 10:52:58 +0000430
431if __name__ == '__main__':
Raymond Hettinger7b4b7882004-12-20 00:29:29 +0000432 exit_status = int(not main())
Jeremy Hylton12b64572001-04-18 01:20:21 +0000433 sys.exit(exit_status)