blob: 8e1569c58e275b859b823c03d949db4aa470c46f [file] [log] [blame]
Éric Araujo2e579f02010-11-20 21:53:02 +00001"""Module/script to byte-compile all .py files to .pyc (or .pyo) 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
Matthias Klosec33b9022010-03-16 00:36:26 +000019__all__ = ["compile_dir","compile_file","compile_path"]
Skip Montanaroe99d5ea2001-01-20 19:54:20 +000020
Georg Brandl8334fd92010-12-04 10:26:46 +000021def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None,
22 quiet=False, legacy=False, optimize=-1):
Guido van Rossumc567b811998-01-19 23:07:55 +000023 """Byte-compile all modules in the given directory tree.
Guido van Rossum3bb54481994-08-29 10:52:58 +000024
Guido van Rossumc567b811998-01-19 23:07:55 +000025 Arguments (only dir is required):
26
27 dir: the directory to byte-compile
28 maxlevels: maximum recursion level (default 10)
R. David Murray94f58c32010-12-17 16:29:07 +000029 ddir: the directory that will be prepended to the path to the
30 file as it is compiled into each byte-code file.
Barry Warsaw28a691b2010-04-17 00:19:56 +000031 force: if True, force compilation, even if timestamps are up-to-date
32 quiet: if True, be quiet during compilation
33 legacy: if True, produce legacy pyc paths instead of PEP 3147 paths
Georg Brandl8334fd92010-12-04 10:26:46 +000034 optimize: optimization level or -1 for level of the interpreter
Guido van Rossumc567b811998-01-19 23:07:55 +000035 """
Martin v. Löwis5c137c22002-03-18 12:44:08 +000036 if not quiet:
Victor Stinner53071262011-05-11 00:36:28 +020037 print('Listing {!r}...'.format(dir))
Guido van Rossumc567b811998-01-19 23:07:55 +000038 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000039 names = os.listdir(dir)
Andrew Svetlovad28c7f2012-12-18 22:02:39 +020040 except OSError:
Victor Stinner53071262011-05-11 00:36:28 +020041 print("Can't list {!r}".format(dir))
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000042 names = []
Guido van Rossumc567b811998-01-19 23:07:55 +000043 names.sort()
Fred Drake9065ea31999-03-29 20:25:40 +000044 success = 1
Guido van Rossumc567b811998-01-19 23:07:55 +000045 for name in names:
Barry Warsawc04317f2010-04-26 15:59:03 +000046 if name == '__pycache__':
47 continue
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000048 fullname = os.path.join(dir, name)
Raymond Hettinger8989ea62002-06-01 00:06:20 +000049 if ddir is not None:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000050 dfile = os.path.join(ddir, name)
51 else:
52 dfile = None
Matthias Klosec33b9022010-03-16 00:36:26 +000053 if not os.path.isdir(fullname):
Georg Brandl8334fd92010-12-04 10:26:46 +000054 if not compile_file(fullname, ddir, force, rx, quiet,
55 legacy, optimize):
Matthias Klosec33b9022010-03-16 00:36:26 +000056 success = 0
Éric Araujo2e579f02010-11-20 21:53:02 +000057 elif (maxlevels > 0 and name != os.curdir and name != os.pardir and
58 os.path.isdir(fullname) and not os.path.islink(fullname)):
Brett Cannonbefb14f2009-02-10 02:10:16 +000059 if not compile_dir(fullname, maxlevels - 1, dfile, force, rx,
Georg Brandl45438462011-02-07 12:36:54 +000060 quiet, legacy, optimize):
Jeremy Hylton12b64572001-04-18 01:20:21 +000061 success = 0
Fred Drake9065ea31999-03-29 20:25:40 +000062 return success
Guido van Rossumc567b811998-01-19 23:07:55 +000063
Éric Araujo413d7b42010-12-23 18:44:31 +000064def compile_file(fullname, ddir=None, force=False, rx=None, quiet=False,
Georg Brandl8334fd92010-12-04 10:26:46 +000065 legacy=False, optimize=-1):
Éric Araujo413d7b42010-12-23 18:44:31 +000066 """Byte-compile one file.
67
68 Arguments (only fullname is required):
69
Barry Warsaw28a691b2010-04-17 00:19:56 +000070 fullname: the file to byte-compile
R. David Murray94f58c32010-12-17 16:29:07 +000071 ddir: if given, the directory name compiled in to the
72 byte-code file.
Barry Warsaw28a691b2010-04-17 00:19:56 +000073 force: if True, force compilation, even if timestamps are up-to-date
74 quiet: if True, be quiet during compilation
75 legacy: if True, produce legacy pyc paths instead of PEP 3147 paths
Georg Brandl8334fd92010-12-04 10:26:46 +000076 optimize: optimization level or -1 for level of the interpreter
Matthias Klosec33b9022010-03-16 00:36:26 +000077 """
78 success = 1
79 name = os.path.basename(fullname)
80 if ddir is not None:
81 dfile = os.path.join(ddir, name)
82 else:
83 dfile = None
84 if rx is not None:
85 mo = rx.search(fullname)
86 if mo:
87 return success
88 if os.path.isfile(fullname):
Barry Warsaw28a691b2010-04-17 00:19:56 +000089 if legacy:
90 cfile = fullname + ('c' if __debug__ else 'o')
91 else:
Georg Brandl8334fd92010-12-04 10:26:46 +000092 if optimize >= 0:
Brett Cannon7822e122013-06-14 23:04:02 -040093 cfile = importlib.util.cache_from_source(
94 fullname, debug_override=not optimize)
Georg Brandl8334fd92010-12-04 10:26:46 +000095 else:
Brett Cannon7822e122013-06-14 23:04:02 -040096 cfile = importlib.util.cache_from_source(fullname)
Barry Warsaw28a691b2010-04-17 00:19:56 +000097 cache_dir = os.path.dirname(cfile)
Matthias Klosec33b9022010-03-16 00:36:26 +000098 head, tail = name[:-3], name[-3:]
99 if tail == '.py':
100 if not force:
101 try:
102 mtime = int(os.stat(fullname).st_mtime)
Brett Cannon7822e122013-06-14 23:04:02 -0400103 expect = struct.pack('<4sl', importlib.util.MAGIC_NUMBER,
104 mtime)
Matthias Klosec33b9022010-03-16 00:36:26 +0000105 with open(cfile, 'rb') as chandle:
106 actual = chandle.read(8)
107 if expect == actual:
108 return success
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200109 except OSError:
Matthias Klosec33b9022010-03-16 00:36:26 +0000110 pass
111 if not quiet:
Victor Stinner53071262011-05-11 00:36:28 +0200112 print('Compiling {!r}...'.format(fullname))
Matthias Klosec33b9022010-03-16 00:36:26 +0000113 try:
Georg Brandl8334fd92010-12-04 10:26:46 +0000114 ok = py_compile.compile(fullname, cfile, dfile, True,
115 optimize=optimize)
Matthias Klosec33b9022010-03-16 00:36:26 +0000116 except py_compile.PyCompileError as err:
117 if quiet:
Victor Stinner53071262011-05-11 00:36:28 +0200118 print('*** Error compiling {!r}...'.format(fullname))
Matthias Klosec33b9022010-03-16 00:36:26 +0000119 else:
120 print('*** ', end='')
Martin v. Löwis4b003072010-03-16 13:19:21 +0000121 # escape non-printable characters in msg
Barry Warsaw28a691b2010-04-17 00:19:56 +0000122 msg = err.msg.encode(sys.stdout.encoding,
123 errors='backslashreplace')
Martin v. Löwis4b003072010-03-16 13:19:21 +0000124 msg = msg.decode(sys.stdout.encoding)
125 print(msg)
Matthias Klosec33b9022010-03-16 00:36:26 +0000126 success = 0
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200127 except (SyntaxError, UnicodeError, OSError) as e:
Matthias Klosec33b9022010-03-16 00:36:26 +0000128 if quiet:
Victor Stinner53071262011-05-11 00:36:28 +0200129 print('*** Error compiling {!r}...'.format(fullname))
Matthias Klosec33b9022010-03-16 00:36:26 +0000130 else:
131 print('*** ', end='')
132 print(e.__class__.__name__ + ':', e)
133 success = 0
134 else:
135 if ok == 0:
136 success = 0
137 return success
138
Barry Warsaw28a691b2010-04-17 00:19:56 +0000139def compile_path(skip_curdir=1, maxlevels=0, force=False, quiet=False,
Georg Brandl8334fd92010-12-04 10:26:46 +0000140 legacy=False, optimize=-1):
Guido van Rossumc567b811998-01-19 23:07:55 +0000141 """Byte-compile all module on sys.path.
142
143 Arguments (all optional):
144
Éric Araujo3b371cf2011-09-01 20:00:33 +0200145 skip_curdir: if true, skip current directory (default True)
Guido van Rossumc567b811998-01-19 23:07:55 +0000146 maxlevels: max recursion level (default 0)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000147 force: as for compile_dir() (default False)
148 quiet: as for compile_dir() (default False)
149 legacy: as for compile_dir() (default False)
Georg Brandl8334fd92010-12-04 10:26:46 +0000150 optimize: as for compile_dir() (default -1)
Guido van Rossumc567b811998-01-19 23:07:55 +0000151 """
Fred Drake9065ea31999-03-29 20:25:40 +0000152 success = 1
Guido van Rossumc567b811998-01-19 23:07:55 +0000153 for dir in sys.path:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000154 if (not dir or dir == os.curdir) and skip_curdir:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000155 print('Skipping current directory')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000156 else:
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000157 success = success and compile_dir(dir, maxlevels, None,
Barry Warsaw28a691b2010-04-17 00:19:56 +0000158 force, quiet=quiet,
Georg Brandl8334fd92010-12-04 10:26:46 +0000159 legacy=legacy, optimize=optimize)
Fred Drake9065ea31999-03-29 20:25:40 +0000160 return success
Guido van Rossum3bb54481994-08-29 10:52:58 +0000161
Matthias Klosec33b9022010-03-16 00:36:26 +0000162
Guido van Rossum3bb54481994-08-29 10:52:58 +0000163def main():
Guido van Rossumc567b811998-01-19 23:07:55 +0000164 """Script main program."""
R. David Murray650f1472010-11-20 21:18:51 +0000165 import argparse
166
167 parser = argparse.ArgumentParser(
168 description='Utilities to support installing Python libraries.')
R. David Murray94f58c32010-12-17 16:29:07 +0000169 parser.add_argument('-l', action='store_const', const=0,
170 default=10, dest='maxlevels',
171 help="don't recurse into subdirectories")
R. David Murray650f1472010-11-20 21:18:51 +0000172 parser.add_argument('-f', action='store_true', dest='force',
173 help='force rebuild even if timestamps are up to date')
174 parser.add_argument('-q', action='store_true', dest='quiet',
R. David Murray94f58c32010-12-17 16:29:07 +0000175 help='output only error messages')
R. David Murray650f1472010-11-20 21:18:51 +0000176 parser.add_argument('-b', action='store_true', dest='legacy',
R. David Murray94f58c32010-12-17 16:29:07 +0000177 help='use legacy (pre-PEP3147) compiled file locations')
R. David Murray650f1472010-11-20 21:18:51 +0000178 parser.add_argument('-d', metavar='DESTDIR', dest='ddir', default=None,
R. David Murray94f58c32010-12-17 16:29:07 +0000179 help=('directory to prepend to file paths for use in '
Éric Araujo3b371cf2011-09-01 20:00:33 +0200180 'compile-time tracebacks and in runtime '
R. David Murray94f58c32010-12-17 16:29:07 +0000181 'tracebacks in cases where the source file is '
182 'unavailable'))
R. David Murray650f1472010-11-20 21:18:51 +0000183 parser.add_argument('-x', metavar='REGEXP', dest='rx', default=None,
Éric Araujo3b371cf2011-09-01 20:00:33 +0200184 help=('skip files matching the regular expression; '
185 'the regexp is searched for in the full path '
186 'of each file considered for compilation'))
R. David Murray650f1472010-11-20 21:18:51 +0000187 parser.add_argument('-i', metavar='FILE', dest='flist',
R. David Murray94f58c32010-12-17 16:29:07 +0000188 help=('add all the files and directories listed in '
Éric Araujo3b371cf2011-09-01 20:00:33 +0200189 'FILE to the list considered for compilation; '
190 'if "-", names are read from stdin'))
R. David Murray94f58c32010-12-17 16:29:07 +0000191 parser.add_argument('compile_dest', metavar='FILE|DIR', nargs='*',
192 help=('zero or more file and directory names '
193 'to compile; if no arguments given, defaults '
194 'to the equivalent of -l sys.path'))
R. David Murray650f1472010-11-20 21:18:51 +0000195 args = parser.parse_args()
196
R. David Murray95333e32010-12-14 22:32:50 +0000197 compile_dests = args.compile_dest
198
R. David Murray650f1472010-11-20 21:18:51 +0000199 if args.rx:
200 import re
201 args.rx = re.compile(args.rx)
202
203 # if flist is provided then load it
R. David Murray650f1472010-11-20 21:18:51 +0000204 if args.flist:
R. David Murray95333e32010-12-14 22:32:50 +0000205 try:
206 with (sys.stdin if args.flist=='-' else open(args.flist)) as f:
207 for line in f:
208 compile_dests.append(line.strip())
Andrew Svetlov3438fa42012-12-17 23:35:18 +0200209 except OSError:
R. David Murray95333e32010-12-14 22:32:50 +0000210 print("Error reading file list {}".format(args.flist))
211 return False
R. David Murray650f1472010-11-20 21:18:51 +0000212
R. David Murray95333e32010-12-14 22:32:50 +0000213 success = True
Guido van Rossumc567b811998-01-19 23:07:55 +0000214 try:
R. David Murray650f1472010-11-20 21:18:51 +0000215 if compile_dests:
216 for dest in compile_dests:
R. David Murray5317e9c2010-12-16 19:08:51 +0000217 if os.path.isfile(dest):
218 if not compile_file(dest, args.ddir, args.force, args.rx,
219 args.quiet, args.legacy):
220 success = False
221 else:
R. David Murray650f1472010-11-20 21:18:51 +0000222 if not compile_dir(dest, args.maxlevels, args.ddir,
223 args.force, args.rx, args.quiet,
224 args.legacy):
R. David Murray95333e32010-12-14 22:32:50 +0000225 success = False
R. David Murray95333e32010-12-14 22:32:50 +0000226 return success
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000227 else:
R David Murray8a1d1e62013-12-15 20:49:38 -0500228 return compile_path(legacy=args.legacy, force=args.force,
229 quiet=args.quiet)
Guido van Rossumc567b811998-01-19 23:07:55 +0000230 except KeyboardInterrupt:
Éric Araujo2e579f02010-11-20 21:53:02 +0000231 print("\n[interrupted]")
R. David Murray95333e32010-12-14 22:32:50 +0000232 return False
233 return True
R. David Murray650f1472010-11-20 21:18:51 +0000234
Guido van Rossum3bb54481994-08-29 10:52:58 +0000235
236if __name__ == '__main__':
Raymond Hettinger7b4b7882004-12-20 00:29:29 +0000237 exit_status = int(not main())
Jeremy Hylton12b64572001-04-18 01:20:21 +0000238 sys.exit(exit_status)