blob: 2432842db5a0ecf7af5ebfa98a94b5b41a0ae110 [file] [log] [blame]
Guido van Rossumc567b811998-01-19 23:07:55 +00001"""Module/script to "compile" all .py files to .pyc (or .pyo) file.
2
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.
12
13"""
Guido van Rossum3bb54481994-08-29 10:52:58 +000014import os
15import sys
16import py_compile
Brett Cannonbefb14f2009-02-10 02:10:16 +000017import struct
18import imp
Guido van Rossum3bb54481994-08-29 10:52:58 +000019
Skip Montanaroe99d5ea2001-01-20 19:54:20 +000020__all__ = ["compile_dir","compile_path"]
21
Martin v. Löwis5c137c22002-03-18 12:44:08 +000022def compile_dir(dir, maxlevels=10, ddir=None,
23 force=0, rx=None, quiet=0):
Guido van Rossumc567b811998-01-19 23:07:55 +000024 """Byte-compile all modules in the given directory tree.
Guido van Rossum3bb54481994-08-29 10:52:58 +000025
Guido van Rossumc567b811998-01-19 23:07:55 +000026 Arguments (only dir is required):
27
28 dir: the directory to byte-compile
29 maxlevels: maximum recursion level (default 10)
30 ddir: if given, purported directory name (this is the
31 directory name that will show up in error messages)
Guido van Rossum0b56a3e1998-12-21 18:23:38 +000032 force: if 1, force compilation, even if timestamps are up-to-date
Martin v. Löwis5c137c22002-03-18 12:44:08 +000033 quiet: if 1, be quiet during compilation
Guido van Rossumc567b811998-01-19 23:07:55 +000034
35 """
Martin v. Löwis5c137c22002-03-18 12:44:08 +000036 if not quiet:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000037 print('Listing', dir, '...')
Guido van Rossumc567b811998-01-19 23:07:55 +000038 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000039 names = os.listdir(dir)
Guido van Rossumc567b811998-01-19 23:07:55 +000040 except os.error:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000041 print("Can't list", 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:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000046 fullname = os.path.join(dir, name)
Raymond Hettinger8989ea62002-06-01 00:06:20 +000047 if ddir is not None:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000048 dfile = os.path.join(ddir, name)
49 else:
50 dfile = None
Raymond Hettinger8989ea62002-06-01 00:06:20 +000051 if rx is not None:
Jeremy Hylton12b64572001-04-18 01:20:21 +000052 mo = rx.search(fullname)
53 if mo:
54 continue
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000055 if os.path.isfile(fullname):
56 head, tail = name[:-3], name[-3:]
57 if tail == '.py':
Brett Cannonbefb14f2009-02-10 02:10:16 +000058 if not force:
59 try:
Mark Dickinson769ba472009-04-19 17:14:11 +000060 mtime = int(os.stat(fullname).st_mtime)
Brett Cannonbefb14f2009-02-10 02:10:16 +000061 expect = struct.pack('<4sl', imp.get_magic(), mtime)
62 cfile = fullname + (__debug__ and 'c' or 'o')
63 with open(cfile, 'rb') as chandle:
64 actual = chandle.read(8)
65 if expect == actual:
66 continue
67 except IOError:
68 pass
Martin v. Löwis5c137c22002-03-18 12:44:08 +000069 if not quiet:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000070 print('Compiling', fullname, '...')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000071 try:
Martin v. Löwis0c6774d2003-01-15 11:51:06 +000072 ok = py_compile.compile(fullname, None, dfile, True)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000073 except KeyboardInterrupt:
74 raise KeyboardInterrupt
Guido van Rossumb940e112007-01-10 16:19:56 +000075 except py_compile.PyCompileError as err:
Martin v. Löwis873a2772004-06-20 20:59:56 +000076 if quiet:
Guido van Rossumb7d3e652007-07-15 13:01:48 +000077 print('*** Error compiling', fullname, '...')
78 else:
79 print('*** ', end='')
Martin v. Löwise96ac3a2010-03-21 22:02:42 +000080 # escape non-printable characters in msg
81 msg = err.msg.encode(sys.stdout.encoding, 'backslashreplace')
82 msg = msg.decode(sys.stdout.encoding)
83 print(msg)
Fred Drake9065ea31999-03-29 20:25:40 +000084 success = 0
Guido van Rossumb7d3e652007-07-15 13:01:48 +000085 except (SyntaxError, UnicodeError, IOError) as e:
86 if quiet:
87 print('*** Error compiling', fullname, '...')
88 else:
89 print('*** ', end='')
90 print(e.__class__.__name__ + ':', e)
Martin v. Löwis6f2adc72003-01-16 11:02:43 +000091 success = 0
Jeremy Hylton12b64572001-04-18 01:20:21 +000092 else:
93 if ok == 0:
94 success = 0
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000095 elif maxlevels > 0 and \
96 name != os.curdir and name != os.pardir and \
97 os.path.isdir(fullname) and \
98 not os.path.islink(fullname):
Brett Cannonbefb14f2009-02-10 02:10:16 +000099 if not compile_dir(fullname, maxlevels - 1, dfile, force, rx,
100 quiet):
Jeremy Hylton12b64572001-04-18 01:20:21 +0000101 success = 0
Fred Drake9065ea31999-03-29 20:25:40 +0000102 return success
Guido van Rossumc567b811998-01-19 23:07:55 +0000103
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000104def compile_path(skip_curdir=1, maxlevels=0, force=0, quiet=0):
Guido van Rossumc567b811998-01-19 23:07:55 +0000105 """Byte-compile all module on sys.path.
106
107 Arguments (all optional):
108
109 skip_curdir: if true, skip current directory (default true)
110 maxlevels: max recursion level (default 0)
Guido van Rossum0b56a3e1998-12-21 18:23:38 +0000111 force: as for compile_dir() (default 0)
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000112 quiet: as for compile_dir() (default 0)
Guido van Rossumc567b811998-01-19 23:07:55 +0000113
114 """
Fred Drake9065ea31999-03-29 20:25:40 +0000115 success = 1
Guido van Rossumc567b811998-01-19 23:07:55 +0000116 for dir in sys.path:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000117 if (not dir or dir == os.curdir) and skip_curdir:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000118 print('Skipping current directory')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000119 else:
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000120 success = success and compile_dir(dir, maxlevels, None,
121 force, quiet=quiet)
Fred Drake9065ea31999-03-29 20:25:40 +0000122 return success
Guido van Rossum3bb54481994-08-29 10:52:58 +0000123
124def main():
Guido van Rossumc567b811998-01-19 23:07:55 +0000125 """Script main program."""
126 import getopt
127 try:
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000128 opts, args = getopt.getopt(sys.argv[1:], 'lfqd:x:')
Guido van Rossumb940e112007-01-10 16:19:56 +0000129 except getopt.error as msg:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000130 print(msg)
131 print("usage: python compileall.py [-l] [-f] [-q] [-d destdir] " \
132 "[-x regexp] [directory ...]")
133 print("-l: don't recurse down")
134 print("-f: force rebuild even if timestamps are up-to-date")
135 print("-q: quiet operation")
136 print("-d destdir: purported directory name for error messages")
137 print(" if no directory arguments, -l sys.path is assumed")
138 print("-x regexp: skip files matching the regular expression regexp")
Christian Heimes78644762008-03-04 23:39:23 +0000139 print(" the regexp is searched for in the full path of the file")
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000140 sys.exit(2)
Guido van Rossumc567b811998-01-19 23:07:55 +0000141 maxlevels = 10
142 ddir = None
Guido van Rossum0b56a3e1998-12-21 18:23:38 +0000143 force = 0
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000144 quiet = 0
Jeremy Hylton12b64572001-04-18 01:20:21 +0000145 rx = None
Guido van Rossumc567b811998-01-19 23:07:55 +0000146 for o, a in opts:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000147 if o == '-l': maxlevels = 0
148 if o == '-d': ddir = a
Guido van Rossum0b56a3e1998-12-21 18:23:38 +0000149 if o == '-f': force = 1
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000150 if o == '-q': quiet = 1
Jeremy Hylton12b64572001-04-18 01:20:21 +0000151 if o == '-x':
152 import re
153 rx = re.compile(a)
Guido van Rossumc567b811998-01-19 23:07:55 +0000154 if ddir:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000155 if len(args) != 1:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000156 print("-d destdir require exactly one directory argument")
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000157 sys.exit(2)
Fred Drake9065ea31999-03-29 20:25:40 +0000158 success = 1
Guido van Rossumc567b811998-01-19 23:07:55 +0000159 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000160 if args:
161 for dir in args:
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000162 if not compile_dir(dir, maxlevels, ddir,
163 force, rx, quiet):
Jeremy Hylton12b64572001-04-18 01:20:21 +0000164 success = 0
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000165 else:
Fred Drake9065ea31999-03-29 20:25:40 +0000166 success = compile_path()
Guido van Rossumc567b811998-01-19 23:07:55 +0000167 except KeyboardInterrupt:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000168 print("\n[interrupt]")
Fred Drake9065ea31999-03-29 20:25:40 +0000169 success = 0
170 return success
Guido van Rossum3bb54481994-08-29 10:52:58 +0000171
172if __name__ == '__main__':
Raymond Hettinger7b4b7882004-12-20 00:29:29 +0000173 exit_status = int(not main())
Jeremy Hylton12b64572001-04-18 01:20:21 +0000174 sys.exit(exit_status)