blob: f695b3aa8415d2a1888ba511ea2cf92a64acd06b [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 +000014
15import os
16import sys
17import py_compile
18
Guido van Rossumc567b811998-01-19 23:07:55 +000019def compile_dir(dir, maxlevels=10, ddir=None):
20 """Byte-compile all modules in the given directory tree.
Guido van Rossum3bb54481994-08-29 10:52:58 +000021
Guido van Rossumc567b811998-01-19 23:07:55 +000022 Arguments (only dir is required):
23
24 dir: the directory to byte-compile
25 maxlevels: maximum recursion level (default 10)
26 ddir: if given, purported directory name (this is the
27 directory name that will show up in error messages)
28
29 """
30 print 'Listing', dir, '...'
31 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000032 names = os.listdir(dir)
Guido van Rossumc567b811998-01-19 23:07:55 +000033 except os.error:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000034 print "Can't list", dir
35 names = []
Guido van Rossumc567b811998-01-19 23:07:55 +000036 names.sort()
37 for name in names:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000038 fullname = os.path.join(dir, name)
39 if ddir:
40 dfile = os.path.join(ddir, name)
41 else:
42 dfile = None
43 if os.path.isfile(fullname):
44 head, tail = name[:-3], name[-3:]
45 if tail == '.py':
46 print 'Compiling', fullname, '...'
47 try:
48 py_compile.compile(fullname, None, dfile)
49 except KeyboardInterrupt:
50 raise KeyboardInterrupt
51 except:
52 if type(sys.exc_type) == type(''):
53 exc_type_name = sys.exc_type
54 else: exc_type_name = sys.exc_type.__name__
55 print 'Sorry:', exc_type_name + ':',
56 print sys.exc_value
57 elif maxlevels > 0 and \
58 name != os.curdir and name != os.pardir and \
59 os.path.isdir(fullname) and \
60 not os.path.islink(fullname):
61 compile_dir(fullname, maxlevels - 1, dfile)
Guido van Rossumc567b811998-01-19 23:07:55 +000062
63def compile_path(skip_curdir=1, maxlevels=0):
64 """Byte-compile all module on sys.path.
65
66 Arguments (all optional):
67
68 skip_curdir: if true, skip current directory (default true)
69 maxlevels: max recursion level (default 0)
70
71 """
72 for dir in sys.path:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000073 if (not dir or dir == os.curdir) and skip_curdir:
74 print 'Skipping current directory'
75 else:
76 compile_dir(dir, maxlevels)
Guido van Rossum3bb54481994-08-29 10:52:58 +000077
78def main():
Guido van Rossumc567b811998-01-19 23:07:55 +000079 """Script main program."""
80 import getopt
81 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000082 opts, args = getopt.getopt(sys.argv[1:], 'ld:')
Guido van Rossumc567b811998-01-19 23:07:55 +000083 except getopt.error, msg:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000084 print msg
85 print "usage: compileall [-l] [-d destdir] [directory ...]"
86 print "-l: don't recurse down"
87 print "-d destdir: purported directory name for error messages"
88 print "if no arguments, -l sys.path is assumed"
89 sys.exit(2)
Guido van Rossumc567b811998-01-19 23:07:55 +000090 maxlevels = 10
91 ddir = None
92 for o, a in opts:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000093 if o == '-l': maxlevels = 0
94 if o == '-d': ddir = a
Guido van Rossumc567b811998-01-19 23:07:55 +000095 if ddir:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000096 if len(args) != 1:
97 print "-d destdir require exactly one directory argument"
98 sys.exit(2)
Guido van Rossumc567b811998-01-19 23:07:55 +000099 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000100 if args:
101 for dir in args:
102 compile_dir(dir, maxlevels, ddir)
103 else:
104 compile_path()
Guido van Rossumc567b811998-01-19 23:07:55 +0000105 except KeyboardInterrupt:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000106 print "\n[interrupt]"
Guido van Rossum3bb54481994-08-29 10:52:58 +0000107
108if __name__ == '__main__':
Guido van Rossumc567b811998-01-19 23:07:55 +0000109 main()