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.
20 __all__
= ["compile_dir","compile_file","compile_path"]
22 def compile_dir(dir, maxlevels
=10, ddir
=None,
23 force
=0, rx
=None, quiet
=0):
24 """Byte-compile all modules in the given directory tree.
26 Arguments (only dir is required):
28 dir: the directory to byte-compile
29 maxlevels: maximum recursion level (default 10)
30 ddir: if given, purported directory name (this is the
31 directory name that will show up in error messages)
32 force: if 1, force compilation, even if timestamps are up-to-date
33 quiet: if 1, be quiet during compilation
37 print 'Listing', dir, '...'
39 names
= os
.listdir(dir)
41 print "Can't list", dir
46 fullname
= os
.path
.join(dir, name
)
48 dfile
= os
.path
.join(ddir
, name
)
51 if not os
.path
.isdir(fullname
):
52 if not compile_file(fullname
, ddir
, force
, rx
, quiet
):
54 elif maxlevels
> 0 and \
55 name
!= os
.curdir
and name
!= os
.pardir
and \
56 os
.path
.isdir(fullname
) and \
57 not os
.path
.islink(fullname
):
58 if not compile_dir(fullname
, maxlevels
- 1, dfile
, force
, rx
,
63 def compile_file(fullname
, ddir
=None, force
=0, rx
=None, quiet
=0):
65 file: the file to byte-compile
66 ddir: if given, purported directory name (this is the
67 directory name that will show up in error messages)
68 force: if 1, force compilation, even if timestamps are up-to-date
69 quiet: if 1, be quiet during compilation
74 name
= os
.path
.basename(fullname
)
76 dfile
= os
.path
.join(ddir
, name
)
80 mo
= rx
.search(fullname
)
83 if os
.path
.isfile(fullname
):
84 head
, tail
= name
[:-3], name
[-3:]
88 mtime
= int(os
.stat(fullname
).st_mtime
)
89 expect
= struct
.pack('<4sl', imp
.get_magic(), mtime
)
90 cfile
= fullname
+ (__debug__
and 'c' or 'o')
91 with
open(cfile
, 'rb') as chandle
:
92 actual
= chandle
.read(8)
98 print 'Compiling', fullname
, '...'
100 ok
= py_compile
.compile(fullname
, None, dfile
, True)
101 except py_compile
.PyCompileError
,err
:
103 print 'Compiling', fullname
, '...'
114 def compile_path(skip_curdir
=1, maxlevels
=0, force
=0, quiet
=0):
115 """Byte-compile all module on sys.path.
117 Arguments (all optional):
119 skip_curdir: if true, skip current directory (default true)
120 maxlevels: max recursion level (default 0)
121 force: as for compile_dir() (default 0)
122 quiet: as for compile_dir() (default 0)
127 if (not dir or dir == os
.curdir
) and skip_curdir
:
128 print 'Skipping current directory'
130 success
= success
and compile_dir(dir, maxlevels
, None,
134 def expand_args(args
, flist
):
135 """read names in flist and append to args"""
147 expanded
.append(line
[:-1])
149 print "Error reading file list %s" % flist
154 """Script main program."""
157 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'lfqd:x:i:')
158 except getopt
.error
, msg
:
160 print "usage: python compileall.py [-l] [-f] [-q] [-d destdir] " \
161 "[-x regexp] [-i list] [directory|file ...]"
162 print "-l: don't recurse down"
163 print "-f: force rebuild even if timestamps are up-to-date"
164 print "-q: quiet operation"
165 print "-d destdir: purported directory name for error messages"
166 print " if no directory arguments, -l sys.path is assumed"
167 print "-x regexp: skip files matching the regular expression regexp"
168 print " the regexp is searched for in the full path of the file"
169 print "-i list: expand list with its content (file and directory names)"
178 if o
== '-l': maxlevels
= 0
179 if o
== '-d': ddir
= a
180 if o
== '-f': force
= 1
181 if o
== '-q': quiet
= 1
185 if o
== '-i': flist
= a
187 if len(args
) != 1 and not os
.path
.isdir(args
[0]):
188 print "-d destdir require exactly one directory argument"
195 args
= expand_args(args
, flist
)
200 if os
.path
.isdir(arg
):
201 if not compile_dir(arg
, maxlevels
, ddir
,
205 if not compile_file(arg
, ddir
, force
, rx
, quiet
):
208 success
= compile_path()
209 except KeyboardInterrupt:
210 print "\n[interrupt]"
214 if __name__
== '__main__':
215 exit_status
= int(not main())
216 sys
.exit(exit_status
)