1 # Routines to force "compilation" of all .py files in a directory
2 # tree or on sys.path. By default recursion is pruned at a depth of
3 # 10 and the current directory, if it occurs in sys.path, is skipped.
4 # When called as a script, compiles argument directories, or sys.path
6 # After a similar module by Sjoerd Mullender.
12 def compile_dir(dir, maxlevels
= 10):
13 print 'Listing', dir, '...'
15 names
= os
.listdir(dir)
17 print "Can't list", dir
21 fullname
= os
.path
.join(dir, name
)
22 if os
.path
.isfile(fullname
):
23 head
, tail
= name
[:-3], name
[-3:]
25 print 'Compiling', fullname
, '...'
27 py_compile
.compile(fullname
)
28 except KeyboardInterrupt:
33 if type(sys
.exc_type
) == type(''):
34 exc_type_name
= sys
.exc_type
35 else: exc_type_name
= sys
.exc_type
.__name
__
36 print 'Sorry:', exc_type_name
+ ':',
38 elif maxlevels
> 0 and \
39 name
!= os
.curdir
and name
!= os
.pardir
and \
40 os
.path
.isdir(fullname
) and \
41 not os
.path
.islink(fullname
):
42 compile_dir(fullname
, maxlevels
- 1)
44 def compile_path(skip_curdir
= 1):
46 if dir == os
.curdir
and skip_curdir
:
47 print 'Skipping current directory'
54 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'l')
55 except getopt
.error
, msg
:
57 print "usage: compileall [-l] [directory ...]"
58 print "-l: don't recurse down"
59 print "if no arguments, -l sys.path is assumed"
62 if o
== '-l': maxlevels
= 0
64 for dir in sys
.argv
[1:]:
65 compile_dir(dir, maxlevels
)
69 if __name__
== '__main__':