blob: 910f3057f4affd765772cd8fc8c47272c17e9c07 [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.
Guido van Rossumc567b811998-01-19 23:07:55 +000012"""
Guido van Rossum3bb54481994-08-29 10:52:58 +000013import os
14import sys
15import py_compile
Brett Cannon28d10882009-02-10 02:07:38 +000016import struct
17import imp
Guido van Rossum3bb54481994-08-29 10:52:58 +000018
Matthias Klosefae23dc2010-03-15 18:00:01 +000019__all__ = ["compile_dir","compile_file","compile_path"]
Skip Montanaroe99d5ea2001-01-20 19:54:20 +000020
Martin v. Löwis5c137c22002-03-18 12:44:08 +000021def compile_dir(dir, maxlevels=10, ddir=None,
22 force=0, rx=None, quiet=0):
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)
29 ddir: if given, purported directory name (this is the
30 directory name that will show up in error messages)
Guido van Rossum0b56a3e1998-12-21 18:23:38 +000031 force: if 1, force compilation, even if timestamps are up-to-date
Martin v. Löwis5c137c22002-03-18 12:44:08 +000032 quiet: if 1, be quiet during compilation
Guido van Rossumc567b811998-01-19 23:07:55 +000033 """
Martin v. Löwis5c137c22002-03-18 12:44:08 +000034 if not quiet:
35 print 'Listing', dir, '...'
Guido van Rossumc567b811998-01-19 23:07:55 +000036 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000037 names = os.listdir(dir)
Guido van Rossumc567b811998-01-19 23:07:55 +000038 except os.error:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000039 print "Can't list", dir
40 names = []
Guido van Rossumc567b811998-01-19 23:07:55 +000041 names.sort()
Fred Drake9065ea31999-03-29 20:25:40 +000042 success = 1
Guido van Rossumc567b811998-01-19 23:07:55 +000043 for name in names:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000044 fullname = os.path.join(dir, name)
Raymond Hettinger8989ea62002-06-01 00:06:20 +000045 if ddir is not None:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000046 dfile = os.path.join(ddir, name)
47 else:
48 dfile = None
Matthias Kloseb13d04c2010-03-15 17:44:12 +000049 if not os.path.isdir(fullname):
50 if not compile_file(fullname, ddir, force, rx, quiet):
51 success = 0
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000052 elif maxlevels > 0 and \
53 name != os.curdir and name != os.pardir and \
54 os.path.isdir(fullname) and \
55 not os.path.islink(fullname):
Brett Cannon28d10882009-02-10 02:07:38 +000056 if not compile_dir(fullname, maxlevels - 1, dfile, force, rx,
57 quiet):
Jeremy Hylton12b64572001-04-18 01:20:21 +000058 success = 0
Fred Drake9065ea31999-03-29 20:25:40 +000059 return success
Guido van Rossumc567b811998-01-19 23:07:55 +000060
Matthias Kloseb13d04c2010-03-15 17:44:12 +000061def compile_file(fullname, ddir=None, force=0, rx=None, quiet=0):
Éric Araujoc11ba762010-12-16 06:15:02 +000062 """Byte-compile one file.
63
64 Arguments (only fullname is required):
65
66 fullname: the file to byte-compile
Matthias Kloseb13d04c2010-03-15 17:44:12 +000067 ddir: if given, purported directory name (this is the
68 directory name that will show up in error messages)
69 force: if 1, force compilation, even if timestamps are up-to-date
70 quiet: if 1, be quiet during compilation
Matthias Kloseb13d04c2010-03-15 17:44:12 +000071 """
Matthias Kloseb13d04c2010-03-15 17:44:12 +000072 success = 1
73 name = os.path.basename(fullname)
74 if ddir is not None:
75 dfile = os.path.join(ddir, name)
76 else:
77 dfile = None
78 if rx is not None:
79 mo = rx.search(fullname)
80 if mo:
81 return success
82 if os.path.isfile(fullname):
83 head, tail = name[:-3], name[-3:]
84 if tail == '.py':
85 if not force:
86 try:
87 mtime = int(os.stat(fullname).st_mtime)
88 expect = struct.pack('<4sl', imp.get_magic(), mtime)
89 cfile = fullname + (__debug__ and 'c' or 'o')
90 with open(cfile, 'rb') as chandle:
91 actual = chandle.read(8)
92 if expect == actual:
93 return success
94 except IOError:
95 pass
96 if not quiet:
97 print 'Compiling', fullname, '...'
98 try:
99 ok = py_compile.compile(fullname, None, dfile, True)
100 except py_compile.PyCompileError,err:
101 if quiet:
102 print 'Compiling', fullname, '...'
103 print err.msg
104 success = 0
105 except IOError, e:
106 print "Sorry", e
107 success = 0
108 else:
109 if ok == 0:
110 success = 0
111 return success
112
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000113def compile_path(skip_curdir=1, maxlevels=0, force=0, quiet=0):
Guido van Rossumc567b811998-01-19 23:07:55 +0000114 """Byte-compile all module on sys.path.
115
116 Arguments (all optional):
117
118 skip_curdir: if true, skip current directory (default true)
119 maxlevels: max recursion level (default 0)
Guido van Rossum0b56a3e1998-12-21 18:23:38 +0000120 force: as for compile_dir() (default 0)
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000121 quiet: as for compile_dir() (default 0)
Guido van Rossumc567b811998-01-19 23:07:55 +0000122 """
Fred Drake9065ea31999-03-29 20:25:40 +0000123 success = 1
Guido van Rossumc567b811998-01-19 23:07:55 +0000124 for dir in sys.path:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000125 if (not dir or dir == os.curdir) and skip_curdir:
126 print 'Skipping current directory'
127 else:
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000128 success = success and compile_dir(dir, maxlevels, None,
129 force, quiet=quiet)
Fred Drake9065ea31999-03-29 20:25:40 +0000130 return success
Guido van Rossum3bb54481994-08-29 10:52:58 +0000131
Matthias Kloseb13d04c2010-03-15 17:44:12 +0000132def expand_args(args, flist):
133 """read names in flist and append to args"""
134 expanded = args[:]
135 if flist:
136 try:
137 if flist == '-':
138 fd = sys.stdin
139 else:
140 fd = open(flist)
141 while 1:
142 line = fd.readline()
143 if not line:
144 break
145 expanded.append(line[:-1])
146 except IOError:
147 print "Error reading file list %s" % flist
148 raise
149 return expanded
150
Guido van Rossum3bb54481994-08-29 10:52:58 +0000151def main():
Guido van Rossumc567b811998-01-19 23:07:55 +0000152 """Script main program."""
153 import getopt
154 try:
Matthias Kloseb13d04c2010-03-15 17:44:12 +0000155 opts, args = getopt.getopt(sys.argv[1:], 'lfqd:x:i:')
Guido van Rossumc567b811998-01-19 23:07:55 +0000156 except getopt.error, msg:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000157 print msg
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000158 print "usage: python compileall.py [-l] [-f] [-q] [-d destdir] " \
Matthias Kloseb13d04c2010-03-15 17:44:12 +0000159 "[-x regexp] [-i list] [directory|file ...]"
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000160 print "-l: don't recurse down"
Guido van Rossum0b56a3e1998-12-21 18:23:38 +0000161 print "-f: force rebuild even if timestamps are up-to-date"
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000162 print "-q: quiet operation"
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000163 print "-d destdir: purported directory name for error messages"
Jeremy Hylton12b64572001-04-18 01:20:21 +0000164 print " if no directory arguments, -l sys.path is assumed"
165 print "-x regexp: skip files matching the regular expression regexp"
Fred Drakea28df132008-03-04 21:14:04 +0000166 print " the regexp is searched for in the full path of the file"
Matthias Kloseb13d04c2010-03-15 17:44:12 +0000167 print "-i list: expand list with its content (file and directory names)"
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000168 sys.exit(2)
Guido van Rossumc567b811998-01-19 23:07:55 +0000169 maxlevels = 10
170 ddir = None
Guido van Rossum0b56a3e1998-12-21 18:23:38 +0000171 force = 0
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000172 quiet = 0
Jeremy Hylton12b64572001-04-18 01:20:21 +0000173 rx = None
Matthias Kloseb13d04c2010-03-15 17:44:12 +0000174 flist = None
Guido van Rossumc567b811998-01-19 23:07:55 +0000175 for o, a in opts:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000176 if o == '-l': maxlevels = 0
177 if o == '-d': ddir = a
Guido van Rossum0b56a3e1998-12-21 18:23:38 +0000178 if o == '-f': force = 1
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000179 if o == '-q': quiet = 1
Jeremy Hylton12b64572001-04-18 01:20:21 +0000180 if o == '-x':
181 import re
182 rx = re.compile(a)
Matthias Kloseb13d04c2010-03-15 17:44:12 +0000183 if o == '-i': flist = a
Guido van Rossumc567b811998-01-19 23:07:55 +0000184 if ddir:
Matthias Kloseb13d04c2010-03-15 17:44:12 +0000185 if len(args) != 1 and not os.path.isdir(args[0]):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000186 print "-d destdir require exactly one directory argument"
187 sys.exit(2)
Fred Drake9065ea31999-03-29 20:25:40 +0000188 success = 1
Guido van Rossumc567b811998-01-19 23:07:55 +0000189 try:
Matthias Kloseb13d04c2010-03-15 17:44:12 +0000190 if args or flist:
191 try:
192 if flist:
193 args = expand_args(args, flist)
194 except IOError:
195 success = 0
196 if success:
197 for arg in args:
198 if os.path.isdir(arg):
199 if not compile_dir(arg, maxlevels, ddir,
200 force, rx, quiet):
201 success = 0
202 else:
203 if not compile_file(arg, ddir, force, rx, quiet):
204 success = 0
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000205 else:
Fred Drake9065ea31999-03-29 20:25:40 +0000206 success = compile_path()
Guido van Rossumc567b811998-01-19 23:07:55 +0000207 except KeyboardInterrupt:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000208 print "\n[interrupt]"
Fred Drake9065ea31999-03-29 20:25:40 +0000209 success = 0
210 return success
Guido van Rossum3bb54481994-08-29 10:52:58 +0000211
212if __name__ == '__main__':
Raymond Hettinger7b4b7882004-12-20 00:29:29 +0000213 exit_status = int(not main())
Jeremy Hylton12b64572001-04-18 01:20:21 +0000214 sys.exit(exit_status)