]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.10/Lib/compileall.py
1 """Module/script to byte-compile all .py files to .pyc (or .pyo) files.
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_file","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: the directory that will be prepended to the path to the
30 file as it is compiled into each byte-code file.
31 force: if 1, force compilation, even if timestamps are up-to-date
32 quiet: if 1, be quiet during compilation
35 print 'Listing', dir, '...'
37 names
= os
.listdir(dir)
39 print "Can't list", dir
44 fullname
= os
.path
.join(dir, name
)
46 dfile
= os
.path
.join(ddir
, name
)
49 if not os
.path
.isdir(fullname
):
50 if not compile_file(fullname
, ddir
, force
, rx
, quiet
):
52 elif maxlevels
> 0 and \
53 name
!= os
.curdir
and name
!= os
.pardir
and \
54 os
.path
.isdir(fullname
) and \
55 not os
.path
.islink(fullname
):
56 if not compile_dir(fullname
, maxlevels
- 1, dfile
, force
, rx
,
61 def compile_file(fullname
, ddir
=None, force
=0, rx
=None, quiet
=0):
62 """Byte-compile one file.
64 Arguments (only fullname is required):
66 fullname: the file to byte-compile
67 ddir: if given, the directory name compiled in to the
69 force: if 1, force compilation, even if timestamps are up-to-date
70 quiet: if 1, be quiet during compilation
73 name
= os
.path
.basename(fullname
)
75 dfile
= os
.path
.join(ddir
, name
)
79 mo
= rx
.search(fullname
)
82 if os
.path
.isfile(fullname
):
83 head
, tail
= name
[:-3], name
[-3:]
87 mtime
= int(os
.stat(fullname
).st_mtime
)
88 expect
= struct
.pack('<4sl', imp
.get_magic(), mtime
)
89 cfile
= fullname
+ (__debug__
and 'c' or 'o')
90 with
open(cfile
, 'rb') as chandle
:
91 actual
= chandle
.read(8)
97 print 'Compiling', fullname
, '...'
99 ok
= py_compile
.compile(fullname
, None, dfile
, True)
100 except py_compile
.PyCompileError
,err
:
102 print 'Compiling', fullname
, '...'
113 def compile_path(skip_curdir
=1, maxlevels
=0, force
=0, quiet
=0):
114 """Byte-compile all module on sys.path.
116 Arguments (all optional):
118 skip_curdir: if true, skip current directory (default true)
119 maxlevels: max recursion level (default 0)
120 force: as for compile_dir() (default 0)
121 quiet: as for compile_dir() (default 0)
125 if (not dir or dir == os
.curdir
) and skip_curdir
:
126 print 'Skipping current directory'
128 success
= success
and compile_dir(dir, maxlevels
, None,
132 def expand_args(args
, flist
):
133 """read names in flist and append to args"""
145 expanded
.append(line
[:-1])
147 print "Error reading file list %s" % flist
152 """Script main program."""
155 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'lfqd:x:i:')
156 except getopt
.error
, msg
:
158 print "usage: python compileall.py [-l] [-f] [-q] [-d destdir] " \
159 "[-x regexp] [-i list] [directory|file ...]"
161 print "arguments: zero or more file and directory names to compile; " \
162 "if no arguments given, "
163 print " defaults to the equivalent of -l sys.path"
166 print "-l: don't recurse into subdirectories"
167 print "-f: force rebuild even if timestamps are up-to-date"
168 print "-q: output only error messages"
169 print "-d destdir: directory to prepend to file paths for use in " \
170 "compile-time tracebacks and in"
171 print " runtime tracebacks in cases where the source " \
172 "file is unavailable"
173 print "-x regexp: skip files matching the regular expression regexp; " \
174 "the regexp is searched for"
175 print " in the full path of each file considered for " \
177 print "-i file: add all the files and directories listed in file to " \
178 "the list considered for"
179 print ' compilation; if "-", names are read from stdin'
189 if o
== '-l': maxlevels
= 0
190 if o
== '-d': ddir
= a
191 if o
== '-f': force
= 1
192 if o
== '-q': quiet
= 1
196 if o
== '-i': flist
= a
198 if len(args
) != 1 and not os
.path
.isdir(args
[0]):
199 print "-d destdir require exactly one directory argument"
206 args
= expand_args(args
, flist
)
211 if os
.path
.isdir(arg
):
212 if not compile_dir(arg
, maxlevels
, ddir
,
216 if not compile_file(arg
, ddir
, force
, rx
, quiet
):
219 success
= compile_path()
220 except KeyboardInterrupt:
221 print "\n[interrupted]"
225 if __name__
== '__main__':
226 exit_status
= int(not main())
227 sys
.exit(exit_status
)