Fix an amazing number of typos & malformed sentences reported by Detlef
[python/dscho.git] / Lib / compileall.py
blob69a59b3a9123ce493477a16bc856082f5133538d
1 """Module/script to "compile" all .py files to .pyc (or .pyo) file.
3 When called as a script with arguments, this compiles the directories
4 given as arguments recursively; the -l option prevents it from
5 recursing into directories.
7 Without arguments, if compiles all modules on sys.path, without
8 recursing into subdirectories. (Even though it should do so for
9 packages -- for now, you'll have to deal with packages separately.)
11 See module py_compile for details of the actual byte-compilation.
13 """
15 import os
16 import stat
17 import sys
18 import py_compile
20 def compile_dir(dir, maxlevels=10, ddir=None, force=0):
21 """Byte-compile all modules in the given directory tree.
23 Arguments (only dir is required):
25 dir: the directory to byte-compile
26 maxlevels: maximum recursion level (default 10)
27 ddir: if given, purported directory name (this is the
28 directory name that will show up in error messages)
29 force: if 1, force compilation, even if timestamps are up-to-date
31 """
32 print 'Listing', dir, '...'
33 try:
34 names = os.listdir(dir)
35 except os.error:
36 print "Can't list", dir
37 names = []
38 names.sort()
39 for name in names:
40 fullname = os.path.join(dir, name)
41 if ddir:
42 dfile = os.path.join(ddir, name)
43 else:
44 dfile = None
45 if os.path.isfile(fullname):
46 head, tail = name[:-3], name[-3:]
47 if tail == '.py':
48 cfile = fullname + (__debug__ and 'c' or 'o')
49 ftime = os.stat(fullname)[stat.ST_MTIME]
50 try: ctime = os.stat(cfile)[stat.ST_MTIME]
51 except os.error: ctime = 0
52 if (ctime > ftime) and not force: continue
53 print 'Compiling', fullname, '...'
54 try:
55 py_compile.compile(fullname, None, dfile)
56 except KeyboardInterrupt:
57 raise KeyboardInterrupt
58 except:
59 if type(sys.exc_type) == type(''):
60 exc_type_name = sys.exc_type
61 else: exc_type_name = sys.exc_type.__name__
62 print 'Sorry:', exc_type_name + ':',
63 print sys.exc_value
64 elif maxlevels > 0 and \
65 name != os.curdir and name != os.pardir and \
66 os.path.isdir(fullname) and \
67 not os.path.islink(fullname):
68 compile_dir(fullname, maxlevels - 1, dfile, force)
70 def compile_path(skip_curdir=1, maxlevels=0, force=0):
71 """Byte-compile all module on sys.path.
73 Arguments (all optional):
75 skip_curdir: if true, skip current directory (default true)
76 maxlevels: max recursion level (default 0)
77 force: as for compile_dir() (default 0)
79 """
80 for dir in sys.path:
81 if (not dir or dir == os.curdir) and skip_curdir:
82 print 'Skipping current directory'
83 else:
84 compile_dir(dir, maxlevels, None, force)
86 def main():
87 """Script main program."""
88 import getopt
89 try:
90 opts, args = getopt.getopt(sys.argv[1:], 'lfd:')
91 except getopt.error, msg:
92 print msg
93 print "usage: compileall [-l] [-f] [-d destdir] [directory ...]"
94 print "-l: don't recurse down"
95 print "-f: force rebuild even if timestamps are up-to-date"
96 print "-d destdir: purported directory name for error messages"
97 print "if no directory arguments, -l sys.path is assumed"
98 sys.exit(2)
99 maxlevels = 10
100 ddir = None
101 force = 0
102 for o, a in opts:
103 if o == '-l': maxlevels = 0
104 if o == '-d': ddir = a
105 if o == '-f': force = 1
106 if ddir:
107 if len(args) != 1:
108 print "-d destdir require exactly one directory argument"
109 sys.exit(2)
110 try:
111 if args:
112 for dir in args:
113 compile_dir(dir, maxlevels, ddir, force)
114 else:
115 compile_path()
116 except KeyboardInterrupt:
117 print "\n[interrupt]"
119 if __name__ == '__main__':
120 main()