blob: b21d95f82c94ac092444ce6e930a753d1b81aebd [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001"""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"""
14
15import os
16import sys
17import py_compile
18
19__all__ = ["compile_dir","compile_path"]
20
21def compile_dir(dir, maxlevels=10, ddir=None,
22 force=0, rx=None, quiet=0):
23 """Byte-compile all modules in the given directory tree.
24
25 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)
31 force: if 1, force compilation, even if timestamps are up-to-date
32 quiet: if 1, be quiet during compilation
33
34 """
35 if not quiet:
36 print 'Listing', dir, '...'
37 try:
38 names = os.listdir(dir)
39 except os.error:
40 print "Can't list", dir
41 names = []
42 names.sort()
43 success = 1
44 for name in names:
45 fullname = os.path.join(dir, name)
46 if ddir is not None:
47 dfile = os.path.join(ddir, name)
48 else:
49 dfile = None
50 if rx is not None:
51 mo = rx.search(fullname)
52 if mo:
53 continue
54 if os.path.isfile(fullname):
55 head, tail = name[:-3], name[-3:]
56 if tail == '.py':
57 cfile = fullname + (__debug__ and 'c' or 'o')
58 ftime = os.stat(fullname).st_mtime
59 try: ctime = os.stat(cfile).st_mtime
60 except os.error: ctime = 0
61 if (ctime > ftime) and not force: continue
62 if not quiet:
63 print 'Compiling', fullname, '...'
64 try:
65 ok = py_compile.compile(fullname, None, dfile, True)
66 except KeyboardInterrupt:
67 raise KeyboardInterrupt
68 except py_compile.PyCompileError,err:
69 if quiet:
70 print 'Compiling', fullname, '...'
71 print err.msg
72 success = 0
73 except IOError, e:
74 print "Sorry", e
75 success = 0
76 else:
77 if ok == 0:
78 success = 0
79 elif maxlevels > 0 and \
80 name != os.curdir and name != os.pardir and \
81 os.path.isdir(fullname) and \
82 not os.path.islink(fullname):
83 if not compile_dir(fullname, maxlevels - 1, dfile, force, rx, quiet):
84 success = 0
85 return success
86
87def compile_path(skip_curdir=1, maxlevels=0, force=0, quiet=0):
88 """Byte-compile all module on sys.path.
89
90 Arguments (all optional):
91
92 skip_curdir: if true, skip current directory (default true)
93 maxlevels: max recursion level (default 0)
94 force: as for compile_dir() (default 0)
95 quiet: as for compile_dir() (default 0)
96
97 """
98 success = 1
99 for dir in sys.path:
100 if (not dir or dir == os.curdir) and skip_curdir:
101 print 'Skipping current directory'
102 else:
103 success = success and compile_dir(dir, maxlevels, None,
104 force, quiet=quiet)
105 return success
106
107def main():
108 """Script main program."""
109 import getopt
110 try:
111 opts, args = getopt.getopt(sys.argv[1:], 'lfqd:x:')
112 except getopt.error, msg:
113 print msg
114 print "usage: python compileall.py [-l] [-f] [-q] [-d destdir] " \
115 "[-x regexp] [directory ...]"
116 print "-l: don't recurse down"
117 print "-f: force rebuild even if timestamps are up-to-date"
118 print "-q: quiet operation"
119 print "-d destdir: purported directory name for error messages"
120 print " if no directory arguments, -l sys.path is assumed"
121 print "-x regexp: skip files matching the regular expression regexp"
122 print " the regexp is search for in the full path of the file"
123 sys.exit(2)
124 maxlevels = 10
125 ddir = None
126 force = 0
127 quiet = 0
128 rx = None
129 for o, a in opts:
130 if o == '-l': maxlevels = 0
131 if o == '-d': ddir = a
132 if o == '-f': force = 1
133 if o == '-q': quiet = 1
134 if o == '-x':
135 import re
136 rx = re.compile(a)
137 if ddir:
138 if len(args) != 1:
139 print "-d destdir require exactly one directory argument"
140 sys.exit(2)
141 success = 1
142 try:
143 if args:
144 for dir in args:
145 if not compile_dir(dir, maxlevels, ddir,
146 force, rx, quiet):
147 success = 0
148 else:
149 success = compile_path()
150 except KeyboardInterrupt:
151 print "\n[interrupt]"
152 success = 0
153 return success
154
155if __name__ == '__main__':
156 exit_status = int(not main())
157 sys.exit(exit_status)