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.
19 def compile_dir(dir, maxlevels
=10, ddir
=None):
20 """Byte-compile all modules in the given directory tree.
22 Arguments (only dir is required):
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)
30 print 'Listing', dir, '...'
32 names
= os
.listdir(dir)
34 print "Can't list", dir
38 fullname
= os
.path
.join(dir, name
)
40 dfile
= os
.path
.join(ddir
, name
)
43 if os
.path
.isfile(fullname
):
44 head
, tail
= name
[:-3], name
[-3:]
46 print 'Compiling', fullname
, '...'
48 py_compile
.compile(fullname
, None, dfile
)
49 except KeyboardInterrupt:
50 raise KeyboardInterrupt
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
+ ':',
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
)
63 def compile_path(skip_curdir
=1, maxlevels
=0):
64 """Byte-compile all module on sys.path.
66 Arguments (all optional):
68 skip_curdir: if true, skip current directory (default true)
69 maxlevels: max recursion level (default 0)
73 if (not dir or dir == os
.curdir
) and skip_curdir
:
74 print 'Skipping current directory'
76 compile_dir(dir, maxlevels
)
79 """Script main program."""
82 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'ld:')
83 except getopt
.error
, 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"
93 if o
== '-l': maxlevels
= 0
94 if o
== '-d': ddir
= a
97 print "-d destdir require exactly one directory argument"
102 compile_dir(dir, maxlevels
, ddir
)
105 except KeyboardInterrupt:
106 print "\n[interrupt]"
108 if __name__
== '__main__':