Issue #6857: Fix Decimal formatting to be consistent with existing float
[python.git] / Lib / compileall.py
blob49537fded0b9aa13fd4c6fc2fdd779542edd1717
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.
13 """
14 import os
15 import sys
16 import py_compile
17 import struct
18 import imp
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
35 """
36 if not quiet:
37 print 'Listing', dir, '...'
38 try:
39 names = os.listdir(dir)
40 except os.error:
41 print "Can't list", dir
42 names = []
43 names.sort()
44 success = 1
45 for name in names:
46 fullname = os.path.join(dir, name)
47 if ddir is not None:
48 dfile = os.path.join(ddir, name)
49 else:
50 dfile = None
51 if rx is not None:
52 mo = rx.search(fullname)
53 if mo:
54 continue
55 if os.path.isfile(fullname):
56 head, tail = name[:-3], name[-3:]
57 if tail == '.py':
58 if not force:
59 try:
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)
65 if expect == actual:
66 continue
67 except IOError:
68 pass
69 if not quiet:
70 print 'Compiling', fullname, '...'
71 try:
72 ok = py_compile.compile(fullname, None, dfile, True)
73 except KeyboardInterrupt:
74 raise KeyboardInterrupt
75 except py_compile.PyCompileError,err:
76 if quiet:
77 print 'Compiling', fullname, '...'
78 print err.msg
79 success = 0
80 except IOError, e:
81 print "Sorry", e
82 success = 0
83 else:
84 if ok == 0:
85 success = 0
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,
91 quiet):
92 success = 0
93 return success
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)
106 success = 1
107 for dir in sys.path:
108 if (not dir or dir == os.curdir) and skip_curdir:
109 print 'Skipping current directory'
110 else:
111 success = success and compile_dir(dir, maxlevels, None,
112 force, quiet=quiet)
113 return success
115 def main():
116 """Script main program."""
117 import getopt
118 try:
119 opts, args = getopt.getopt(sys.argv[1:], 'lfqd:x:')
120 except getopt.error, msg:
121 print 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"
131 sys.exit(2)
132 maxlevels = 10
133 ddir = None
134 force = 0
135 quiet = 0
136 rx = None
137 for o, a in opts:
138 if o == '-l': maxlevels = 0
139 if o == '-d': ddir = a
140 if o == '-f': force = 1
141 if o == '-q': quiet = 1
142 if o == '-x':
143 import re
144 rx = re.compile(a)
145 if ddir:
146 if len(args) != 1:
147 print "-d destdir require exactly one directory argument"
148 sys.exit(2)
149 success = 1
150 try:
151 if args:
152 for dir in args:
153 if not compile_dir(dir, maxlevels, ddir,
154 force, rx, quiet):
155 success = 0
156 else:
157 success = compile_path()
158 except KeyboardInterrupt:
159 print "\n[interrupt]"
160 success = 0
161 return success
163 if __name__ == '__main__':
164 exit_status = int(not main())
165 sys.exit(exit_status)