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_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
)
52 mo
= rx
.search(fullname
)
55 if os
.path
.isfile(fullname
):
56 head
, tail
= name
[:-3], name
[-3:]
60 mtime
= int(os
.stat(fullname
).st_mtime
)
61 expect
= struct
.pack('<4sl', imp
.get_magic(), mtime
)
62 cfile
= fullname
+ (__debug__
and 'c' or 'o')
63 with
open(cfile
, 'rb') as chandle
:
64 actual
= chandle
.read(8)
70 print 'Compiling', fullname
, '...'
72 ok
= py_compile
.compile(fullname
, None, dfile
, True)
73 except KeyboardInterrupt:
74 raise KeyboardInterrupt
75 except py_compile
.PyCompileError
,err
:
77 print 'Compiling', fullname
, '...'
86 elif maxlevels
> 0 and \
87 name
!= os
.curdir
and name
!= os
.pardir
and \
88 os
.path
.isdir(fullname
) and \
89 not os
.path
.islink(fullname
):
90 if not compile_dir(fullname
, maxlevels
- 1, dfile
, force
, rx
,
95 def compile_path(skip_curdir
=1, maxlevels
=0, force
=0, quiet
=0):
96 """Byte-compile all module on sys.path.
98 Arguments (all optional):
100 skip_curdir: if true, skip current directory (default true)
101 maxlevels: max recursion level (default 0)
102 force: as for compile_dir() (default 0)
103 quiet: as for compile_dir() (default 0)
108 if (not dir or dir == os
.curdir
) and skip_curdir
:
109 print 'Skipping current directory'
111 success
= success
and compile_dir(dir, maxlevels
, None,
116 """Script main program."""
119 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'lfqd:x:')
120 except getopt
.error
, msg
:
122 print "usage: python compileall.py [-l] [-f] [-q] [-d destdir] " \
123 "[-x regexp] [directory ...]"
124 print "-l: don't recurse down"
125 print "-f: force rebuild even if timestamps are up-to-date"
126 print "-q: quiet operation"
127 print "-d destdir: purported directory name for error messages"
128 print " if no directory arguments, -l sys.path is assumed"
129 print "-x regexp: skip files matching the regular expression regexp"
130 print " the regexp is searched for in the full path of the file"
138 if o
== '-l': maxlevels
= 0
139 if o
== '-d': ddir
= a
140 if o
== '-f': force
= 1
141 if o
== '-q': quiet
= 1
147 print "-d destdir require exactly one directory argument"
153 if not compile_dir(dir, maxlevels
, ddir
,
157 success
= compile_path()
158 except KeyboardInterrupt:
159 print "\n[interrupt]"
163 if __name__
== '__main__':
164 exit_status
= int(not main())
165 sys
.exit(exit_status
)