blob: f564abbd03629b67f69d294f5af1352069032168 [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 Cannon28d10882009-02-10 02:07:38 +000017import struct
18import imp
Guido van Rossum3bb54481994-08-29 10:52:58 +000019
Matthias Klosefae23dc2010-03-15 18:00:01 +000020__all__ = ["compile_dir","compile_file","compile_path"]
Skip Montanaroe99d5ea2001-01-20 19:54:20 +000021
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:
37 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 Rossum45e2fbc1998-03-26 21:13:24 +000041 print "Can't list", dir
42 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
Matthias Kloseb13d04c2010-03-15 17:44:12 +000051 if not os.path.isdir(fullname):
52 if not compile_file(fullname, ddir, force, rx, quiet):
53 success = 0
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000054 elif maxlevels > 0 and \
55 name != os.curdir and name != os.pardir and \
56 os.path.isdir(fullname) and \
57 not os.path.islink(fullname):
Brett Cannon28d10882009-02-10 02:07:38 +000058 if not compile_dir(fullname, maxlevels - 1, dfile, force, rx,
59 quiet):
Jeremy Hylton12b64572001-04-18 01:20:21 +000060 success = 0
Fred Drake9065ea31999-03-29 20:25:40 +000061 return success
Guido van Rossumc567b811998-01-19 23:07:55 +000062
Matthias Kloseb13d04c2010-03-15 17:44:12 +000063def compile_file(fullname, ddir=None, force=0, rx=None, quiet=0):
64 """Byte-compile file.
65 file: the file to byte-compile
66 ddir: if given, purported directory name (this is the
67 directory name that will show up in error messages)
68 force: if 1, force compilation, even if timestamps are up-to-date
69 quiet: if 1, be quiet during compilation
70
71 """
72
73 success = 1
74 name = os.path.basename(fullname)
75 if ddir is not None:
76 dfile = os.path.join(ddir, name)
77 else:
78 dfile = None
79 if rx is not None:
80 mo = rx.search(fullname)
81 if mo:
82 return success
83 if os.path.isfile(fullname):
84 head, tail = name[:-3], name[-3:]
85 if tail == '.py':
86 if not force:
87 try:
88 mtime = int(os.stat(fullname).st_mtime)
89 expect = struct.pack('<4sl', imp.get_magic(), mtime)
90 cfile = fullname + (__debug__ and 'c' or 'o')
91 with open(cfile, 'rb') as chandle:
92 actual = chandle.read(8)
93 if expect == actual:
94 return success
95 except IOError:
96 pass
97 if not quiet:
98 print 'Compiling', fullname, '...'
99 try:
100 ok = py_compile.compile(fullname, None, dfile, True)
101 except py_compile.PyCompileError,err:
102 if quiet:
103 print 'Compiling', fullname, '...'
104 print err.msg
105 success = 0
106 except IOError, e:
107 print "Sorry", e
108 success = 0
109 else:
110 if ok == 0:
111 success = 0
112 return success
113
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000114def compile_path(skip_curdir=1, maxlevels=0, force=0, quiet=0):
Guido van Rossumc567b811998-01-19 23:07:55 +0000115 """Byte-compile all module on sys.path.
116
117 Arguments (all optional):
118
119 skip_curdir: if true, skip current directory (default true)
120 maxlevels: max recursion level (default 0)
Guido van Rossum0b56a3e1998-12-21 18:23:38 +0000121 force: as for compile_dir() (default 0)
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000122 quiet: as for compile_dir() (default 0)
Guido van Rossumc567b811998-01-19 23:07:55 +0000123
124 """
Fred Drake9065ea31999-03-29 20:25:40 +0000125 success = 1
Guido van Rossumc567b811998-01-19 23:07:55 +0000126 for dir in sys.path:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000127 if (not dir or dir == os.curdir) and skip_curdir:
128 print 'Skipping current directory'
129 else:
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000130 success = success and compile_dir(dir, maxlevels, None,
131 force, quiet=quiet)
Fred Drake9065ea31999-03-29 20:25:40 +0000132 return success
Guido van Rossum3bb54481994-08-29 10:52:58 +0000133
Matthias Kloseb13d04c2010-03-15 17:44:12 +0000134def expand_args(args, flist):
135 """read names in flist and append to args"""
136 expanded = args[:]
137 if flist:
138 try:
139 if flist == '-':
140 fd = sys.stdin
141 else:
142 fd = open(flist)
143 while 1:
144 line = fd.readline()
145 if not line:
146 break
147 expanded.append(line[:-1])
148 except IOError:
149 print "Error reading file list %s" % flist
150 raise
151 return expanded
152
Guido van Rossum3bb54481994-08-29 10:52:58 +0000153def main():
Guido van Rossumc567b811998-01-19 23:07:55 +0000154 """Script main program."""
155 import getopt
156 try:
Matthias Kloseb13d04c2010-03-15 17:44:12 +0000157 opts, args = getopt.getopt(sys.argv[1:], 'lfqd:x:i:')
Guido van Rossumc567b811998-01-19 23:07:55 +0000158 except getopt.error, msg:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000159 print msg
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000160 print "usage: python compileall.py [-l] [-f] [-q] [-d destdir] " \
Matthias Kloseb13d04c2010-03-15 17:44:12 +0000161 "[-x regexp] [-i list] [directory|file ...]"
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000162 print "-l: don't recurse down"
Guido van Rossum0b56a3e1998-12-21 18:23:38 +0000163 print "-f: force rebuild even if timestamps are up-to-date"
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000164 print "-q: quiet operation"
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000165 print "-d destdir: purported directory name for error messages"
Jeremy Hylton12b64572001-04-18 01:20:21 +0000166 print " if no directory arguments, -l sys.path is assumed"
167 print "-x regexp: skip files matching the regular expression regexp"
Fred Drakea28df132008-03-04 21:14:04 +0000168 print " the regexp is searched for in the full path of the file"
Matthias Kloseb13d04c2010-03-15 17:44:12 +0000169 print "-i list: expand list with its content (file and directory names)"
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000170 sys.exit(2)
Guido van Rossumc567b811998-01-19 23:07:55 +0000171 maxlevels = 10
172 ddir = None
Guido van Rossum0b56a3e1998-12-21 18:23:38 +0000173 force = 0
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000174 quiet = 0
Jeremy Hylton12b64572001-04-18 01:20:21 +0000175 rx = None
Matthias Kloseb13d04c2010-03-15 17:44:12 +0000176 flist = None
Guido van Rossumc567b811998-01-19 23:07:55 +0000177 for o, a in opts:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000178 if o == '-l': maxlevels = 0
179 if o == '-d': ddir = a
Guido van Rossum0b56a3e1998-12-21 18:23:38 +0000180 if o == '-f': force = 1
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000181 if o == '-q': quiet = 1
Jeremy Hylton12b64572001-04-18 01:20:21 +0000182 if o == '-x':
183 import re
184 rx = re.compile(a)
Matthias Kloseb13d04c2010-03-15 17:44:12 +0000185 if o == '-i': flist = a
Guido van Rossumc567b811998-01-19 23:07:55 +0000186 if ddir:
Matthias Kloseb13d04c2010-03-15 17:44:12 +0000187 if len(args) != 1 and not os.path.isdir(args[0]):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000188 print "-d destdir require exactly one directory argument"
189 sys.exit(2)
Fred Drake9065ea31999-03-29 20:25:40 +0000190 success = 1
Guido van Rossumc567b811998-01-19 23:07:55 +0000191 try:
Matthias Kloseb13d04c2010-03-15 17:44:12 +0000192 if args or flist:
193 try:
194 if flist:
195 args = expand_args(args, flist)
196 except IOError:
197 success = 0
198 if success:
199 for arg in args:
200 if os.path.isdir(arg):
201 if not compile_dir(arg, maxlevels, ddir,
202 force, rx, quiet):
203 success = 0
204 else:
205 if not compile_file(arg, ddir, force, rx, quiet):
206 success = 0
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000207 else:
Fred Drake9065ea31999-03-29 20:25:40 +0000208 success = compile_path()
Guido van Rossumc567b811998-01-19 23:07:55 +0000209 except KeyboardInterrupt:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000210 print "\n[interrupt]"
Fred Drake9065ea31999-03-29 20:25:40 +0000211 success = 0
212 return success
Guido van Rossum3bb54481994-08-29 10:52:58 +0000213
214if __name__ == '__main__':
Raymond Hettinger7b4b7882004-12-20 00:29:29 +0000215 exit_status = int(not main())
Jeremy Hylton12b64572001-04-18 01:20:21 +0000216 sys.exit(exit_status)