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 __all__
= ["compile_dir","compile_path"]
21 def compile_dir(dir, maxlevels
=10, ddir
=None,
22 force
=0, rx
=None, quiet
=0):
23 """Byte-compile all modules in the given directory tree.
25 Arguments (only dir is required):
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
36 print 'Listing', dir, '...'
38 names
= os
.listdir(dir)
40 print "Can't list", dir
45 fullname
= os
.path
.join(dir, name
)
47 dfile
= os
.path
.join(ddir
, name
)
51 mo
= rx
.search(fullname
)
54 if os
.path
.isfile(fullname
):
55 head
, tail
= name
[:-3], name
[-3:]
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
63 print 'Compiling', fullname
, '...'
65 ok
= py_compile
.compile(fullname
, None, dfile
, True)
66 except KeyboardInterrupt:
67 raise KeyboardInterrupt
68 except py_compile
.PyCompileError
,err
:
70 print 'Compiling', fullname
, '...'
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
):
87 def compile_path(skip_curdir
=1, maxlevels
=0, force
=0, quiet
=0):
88 """Byte-compile all module on sys.path.
90 Arguments (all optional):
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)
100 if (not dir or dir == os
.curdir
) and skip_curdir
:
101 print 'Skipping current directory'
103 success
= success
and compile_dir(dir, maxlevels
, None,
108 """Script main program."""
111 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'lfqd:x:')
112 except getopt
.error
, 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 searched for in the full path of the file"
130 if o
== '-l': maxlevels
= 0
131 if o
== '-d': ddir
= a
132 if o
== '-f': force
= 1
133 if o
== '-q': quiet
= 1
139 print "-d destdir require exactly one directory argument"
145 if not compile_dir(dir, maxlevels
, ddir
,
149 success
= compile_path()
150 except KeyboardInterrupt:
151 print "\n[interrupt]"
155 if __name__
== '__main__':
156 exit_status
= int(not main())
157 sys
.exit(exit_status
)