Mercurial > cpython-lru_cache-weakref
annotate Lib/compileall.py @ 78069:a8aa77458e12 3.2
Add c_size_t and c_ssize_t to the ctypes summary table.
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Thu, 12 Jul 2012 20:31:50 +0200 |
| parents | 755a57f987ca |
| children | 892e0ee4ca32 |
| rev | line source |
|---|---|
|
66328
7af19e99d2f8
Fix typos and style in compileall.
Éric Araujo <merwok@netwok.org>
parents:
66322
diff
changeset
|
1 """Module/script to byte-compile all .py files to .pyc (or .pyo) files. |
|
7126
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
2 |
|
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
3 When called as a script with arguments, this compiles the directories |
|
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
4 given as arguments recursively; the -l option prevents it from |
|
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
5 recursing into directories. |
|
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
6 |
|
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
7 Without arguments, if compiles all modules on sys.path, without |
|
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
8 recursing into subdirectories. (Even though it should do so for |
|
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
9 packages -- for now, you'll have to deal with packages separately.) |
|
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
10 |
|
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
11 See module py_compile for details of the actual byte-compilation. |
|
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
12 """ |
|
1828
b464e1d0b2fb
New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 import os |
|
b464e1d0b2fb
New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 import sys |
| 60648 | 15 import errno |
|
66328
7af19e99d2f8
Fix typos and style in compileall.
Éric Araujo <merwok@netwok.org>
parents:
66322
diff
changeset
|
16 import imp |
|
1828
b464e1d0b2fb
New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 import py_compile |
|
51501
5e7d7fd1da5a
Merged revisions 69481 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
45659
diff
changeset
|
18 import struct |
|
1828
b464e1d0b2fb
New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 |
|
59644
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
20 __all__ = ["compile_dir","compile_file","compile_path"] |
|
1828
b464e1d0b2fb
New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 |
|
66685
713c6b6ca5ce
Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents:
66328
diff
changeset
|
22 def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None, |
|
713c6b6ca5ce
Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents:
66328
diff
changeset
|
23 quiet=False, legacy=False, optimize=-1): |
|
7126
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
24 """Byte-compile all modules in the given directory tree. |
|
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
25 |
|
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
26 Arguments (only dir is required): |
|
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
27 |
|
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
28 dir: the directory to byte-compile |
|
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
29 maxlevels: maximum recursion level (default 10) |
|
66986
61935c8dc09e
#10454: clarify the compileall docs and help messages.
R. David Murray <rdmurray@bitdance.com>
parents:
66977
diff
changeset
|
30 ddir: the directory that will be prepended to the path to the |
|
61935c8dc09e
#10454: clarify the compileall docs and help messages.
R. David Murray <rdmurray@bitdance.com>
parents:
66977
diff
changeset
|
31 file as it is compiled into each byte-code file. |
| 60648 | 32 force: if True, force compilation, even if timestamps are up-to-date |
| 33 quiet: if True, be quiet during compilation | |
| 34 legacy: if True, produce legacy pyc paths instead of PEP 3147 paths | |
|
66685
713c6b6ca5ce
Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents:
66328
diff
changeset
|
35 optimize: optimization level or -1 for level of the interpreter |
|
7126
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
36 """ |
|
22374
2417694324ef
Patch #495598: add an -q (quiet) option to pycompile.
Martin v. Löwis <martin@v.loewis.de>
parents:
17559
diff
changeset
|
37 if not quiet: |
|
70026
85cfbbc7da60
Issue #11169: compileall module uses repr() to format filenames and paths to
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68257
diff
changeset
|
38 print('Listing {!r}...'.format(dir)) |
|
7126
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
39 try: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7126
diff
changeset
|
40 names = os.listdir(dir) |
|
7126
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
41 except os.error: |
|
70026
85cfbbc7da60
Issue #11169: compileall module uses repr() to format filenames and paths to
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68257
diff
changeset
|
42 print("Can't list {!r}".format(dir)) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7126
diff
changeset
|
43 names = [] |
|
7126
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
44 names.sort() |
|
10498
fe8645e49314
When run as a script, report failures in the exit code as well.
Fred Drake <fdrake@acm.org>
parents:
9664
diff
changeset
|
45 success = 1 |
|
7126
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
46 for name in names: |
|
60979
efd70553a098
Bug 8527 - multiple compileall calls produce cascading __pycache__ directories.
Barry Warsaw <barry@python.org>
parents:
60648
diff
changeset
|
47 if name == '__pycache__': |
|
efd70553a098
Bug 8527 - multiple compileall calls produce cascading __pycache__ directories.
Barry Warsaw <barry@python.org>
parents:
60648
diff
changeset
|
48 continue |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7126
diff
changeset
|
49 fullname = os.path.join(dir, name) |
|
23610
0a2818cc342c
Use is None rather than general boolean
Raymond Hettinger <python@rcn.com>
parents:
22374
diff
changeset
|
50 if ddir is not None: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7126
diff
changeset
|
51 dfile = os.path.join(ddir, name) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7126
diff
changeset
|
52 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7126
diff
changeset
|
53 dfile = None |
|
59644
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
54 if not os.path.isdir(fullname): |
|
66685
713c6b6ca5ce
Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents:
66328
diff
changeset
|
55 if not compile_file(fullname, ddir, force, rx, quiet, |
|
713c6b6ca5ce
Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents:
66328
diff
changeset
|
56 legacy, optimize): |
|
59644
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
57 success = 0 |
|
66328
7af19e99d2f8
Fix typos and style in compileall.
Éric Araujo <merwok@netwok.org>
parents:
66322
diff
changeset
|
58 elif (maxlevels > 0 and name != os.curdir and name != os.pardir and |
|
7af19e99d2f8
Fix typos and style in compileall.
Éric Araujo <merwok@netwok.org>
parents:
66322
diff
changeset
|
59 os.path.isdir(fullname) and not os.path.islink(fullname)): |
|
51501
5e7d7fd1da5a
Merged revisions 69481 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
45659
diff
changeset
|
60 if not compile_dir(fullname, maxlevels - 1, dfile, force, rx, |
|
67892
f1faec49350d
#11132: pass optimize parameter to recursive call in compileall.compile_dir(). Reviewed by Eric A.
Georg Brandl <georg@python.org>
parents:
67092
diff
changeset
|
61 quiet, legacy, optimize): |
|
17559
28c14a79ad22
Fix compileall.py so that it fails on SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16070
diff
changeset
|
62 success = 0 |
|
10498
fe8645e49314
When run as a script, report failures in the exit code as well.
Fred Drake <fdrake@acm.org>
parents:
9664
diff
changeset
|
63 return success |
|
7126
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
64 |
|
67092
36ac3d9d74c1
Nits: use a real boolean, make one docstring more similar to the other ones
Éric Araujo <merwok@netwok.org>
parents:
66986
diff
changeset
|
65 def compile_file(fullname, ddir=None, force=False, rx=None, quiet=False, |
|
66685
713c6b6ca5ce
Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents:
66328
diff
changeset
|
66 legacy=False, optimize=-1): |
|
67092
36ac3d9d74c1
Nits: use a real boolean, make one docstring more similar to the other ones
Éric Araujo <merwok@netwok.org>
parents:
66986
diff
changeset
|
67 """Byte-compile one file. |
|
36ac3d9d74c1
Nits: use a real boolean, make one docstring more similar to the other ones
Éric Araujo <merwok@netwok.org>
parents:
66986
diff
changeset
|
68 |
|
36ac3d9d74c1
Nits: use a real boolean, make one docstring more similar to the other ones
Éric Araujo <merwok@netwok.org>
parents:
66986
diff
changeset
|
69 Arguments (only fullname is required): |
|
36ac3d9d74c1
Nits: use a real boolean, make one docstring more similar to the other ones
Éric Araujo <merwok@netwok.org>
parents:
66986
diff
changeset
|
70 |
| 60648 | 71 fullname: the file to byte-compile |
|
66986
61935c8dc09e
#10454: clarify the compileall docs and help messages.
R. David Murray <rdmurray@bitdance.com>
parents:
66977
diff
changeset
|
72 ddir: if given, the directory name compiled in to the |
|
61935c8dc09e
#10454: clarify the compileall docs and help messages.
R. David Murray <rdmurray@bitdance.com>
parents:
66977
diff
changeset
|
73 byte-code file. |
| 60648 | 74 force: if True, force compilation, even if timestamps are up-to-date |
| 75 quiet: if True, be quiet during compilation | |
| 76 legacy: if True, produce legacy pyc paths instead of PEP 3147 paths | |
|
66685
713c6b6ca5ce
Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents:
66328
diff
changeset
|
77 optimize: optimization level or -1 for level of the interpreter |
|
59644
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
78 """ |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
79 success = 1 |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
80 name = os.path.basename(fullname) |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
81 if ddir is not None: |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
82 dfile = os.path.join(ddir, name) |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
83 else: |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
84 dfile = None |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
85 if rx is not None: |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
86 mo = rx.search(fullname) |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
87 if mo: |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
88 return success |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
89 if os.path.isfile(fullname): |
| 60648 | 90 if legacy: |
| 91 cfile = fullname + ('c' if __debug__ else 'o') | |
| 92 else: | |
|
66685
713c6b6ca5ce
Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents:
66328
diff
changeset
|
93 if optimize >= 0: |
|
713c6b6ca5ce
Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents:
66328
diff
changeset
|
94 cfile = imp.cache_from_source(fullname, |
|
713c6b6ca5ce
Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents:
66328
diff
changeset
|
95 debug_override=not optimize) |
|
713c6b6ca5ce
Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents:
66328
diff
changeset
|
96 else: |
|
713c6b6ca5ce
Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents:
66328
diff
changeset
|
97 cfile = imp.cache_from_source(fullname) |
| 60648 | 98 cache_dir = os.path.dirname(cfile) |
|
59644
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
99 head, tail = name[:-3], name[-3:] |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
100 if tail == '.py': |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
101 if not force: |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
102 try: |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
103 mtime = int(os.stat(fullname).st_mtime) |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
104 expect = struct.pack('<4sl', imp.get_magic(), mtime) |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
105 with open(cfile, 'rb') as chandle: |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
106 actual = chandle.read(8) |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
107 if expect == actual: |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
108 return success |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
109 except IOError: |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
110 pass |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
111 if not quiet: |
|
70026
85cfbbc7da60
Issue #11169: compileall module uses repr() to format filenames and paths to
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68257
diff
changeset
|
112 print('Compiling {!r}...'.format(fullname)) |
|
59644
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
113 try: |
|
66685
713c6b6ca5ce
Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents:
66328
diff
changeset
|
114 ok = py_compile.compile(fullname, cfile, dfile, True, |
|
713c6b6ca5ce
Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents:
66328
diff
changeset
|
115 optimize=optimize) |
|
59644
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
116 except py_compile.PyCompileError as err: |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
117 if quiet: |
|
70026
85cfbbc7da60
Issue #11169: compileall module uses repr() to format filenames and paths to
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68257
diff
changeset
|
118 print('*** Error compiling {!r}...'.format(fullname)) |
|
59644
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
119 else: |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
120 print('*** ', end='') |
|
59649
42572e347108
Issue #6716/2: Backslash-replace error output in compilall.
Martin v. Löwis <martin@v.loewis.de>
parents:
59644
diff
changeset
|
121 # escape non-printable characters in msg |
| 60648 | 122 msg = err.msg.encode(sys.stdout.encoding, |
| 123 errors='backslashreplace') | |
|
59649
42572e347108
Issue #6716/2: Backslash-replace error output in compilall.
Martin v. Löwis <martin@v.loewis.de>
parents:
59644
diff
changeset
|
124 msg = msg.decode(sys.stdout.encoding) |
|
42572e347108
Issue #6716/2: Backslash-replace error output in compilall.
Martin v. Löwis <martin@v.loewis.de>
parents:
59644
diff
changeset
|
125 print(msg) |
|
59644
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
126 success = 0 |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
127 except (SyntaxError, UnicodeError, IOError) as e: |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
128 if quiet: |
|
70026
85cfbbc7da60
Issue #11169: compileall module uses repr() to format filenames and paths to
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68257
diff
changeset
|
129 print('*** Error compiling {!r}...'.format(fullname)) |
|
59644
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
130 else: |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
131 print('*** ', end='') |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
132 print(e.__class__.__name__ + ':', e) |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
133 success = 0 |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
134 else: |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
135 if ok == 0: |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
136 success = 0 |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
137 return success |
|
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
138 |
| 60648 | 139 def compile_path(skip_curdir=1, maxlevels=0, force=False, quiet=False, |
|
66685
713c6b6ca5ce
Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents:
66328
diff
changeset
|
140 legacy=False, optimize=-1): |
|
7126
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
141 """Byte-compile all module on sys.path. |
|
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
142 |
|
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
143 Arguments (all optional): |
|
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
144 |
|
72186
755a57f987ca
#10454: a few edits to compileall help messages
Éric Araujo <merwok@netwok.org>
parents:
70026
diff
changeset
|
145 skip_curdir: if true, skip current directory (default True) |
|
7126
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
146 maxlevels: max recursion level (default 0) |
| 60648 | 147 force: as for compile_dir() (default False) |
| 148 quiet: as for compile_dir() (default False) | |
| 149 legacy: as for compile_dir() (default False) | |
|
66685
713c6b6ca5ce
Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents:
66328
diff
changeset
|
150 optimize: as for compile_dir() (default -1) |
|
7126
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
151 """ |
|
10498
fe8645e49314
When run as a script, report failures in the exit code as well.
Fred Drake <fdrake@acm.org>
parents:
9664
diff
changeset
|
152 success = 1 |
|
7126
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
153 for dir in sys.path: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7126
diff
changeset
|
154 if (not dir or dir == os.curdir) and skip_curdir: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
155 print('Skipping current directory') |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7126
diff
changeset
|
156 else: |
|
22374
2417694324ef
Patch #495598: add an -q (quiet) option to pycompile.
Martin v. Löwis <martin@v.loewis.de>
parents:
17559
diff
changeset
|
157 success = success and compile_dir(dir, maxlevels, None, |
| 60648 | 158 force, quiet=quiet, |
|
66685
713c6b6ca5ce
Add an "optimize" parameter to compile() to control the optimization level, and provide an interface to it in py_compile, compileall and PyZipFile.
Georg Brandl <georg@python.org>
parents:
66328
diff
changeset
|
159 legacy=legacy, optimize=optimize) |
|
10498
fe8645e49314
When run as a script, report failures in the exit code as well.
Fred Drake <fdrake@acm.org>
parents:
9664
diff
changeset
|
160 return success |
|
1828
b464e1d0b2fb
New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
161 |
|
59644
59931179c5e2
Merged revisions 78983,78985 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
53269
diff
changeset
|
162 |
|
1828
b464e1d0b2fb
New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 def main(): |
|
7126
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
164 """Script main program.""" |
|
66322
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
165 import argparse |
|
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
166 |
|
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
167 parser = argparse.ArgumentParser( |
|
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
168 description='Utilities to support installing Python libraries.') |
|
66986
61935c8dc09e
#10454: clarify the compileall docs and help messages.
R. David Murray <rdmurray@bitdance.com>
parents:
66977
diff
changeset
|
169 parser.add_argument('-l', action='store_const', const=0, |
|
61935c8dc09e
#10454: clarify the compileall docs and help messages.
R. David Murray <rdmurray@bitdance.com>
parents:
66977
diff
changeset
|
170 default=10, dest='maxlevels', |
|
61935c8dc09e
#10454: clarify the compileall docs and help messages.
R. David Murray <rdmurray@bitdance.com>
parents:
66977
diff
changeset
|
171 help="don't recurse into subdirectories") |
|
66322
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
172 parser.add_argument('-f', action='store_true', dest='force', |
|
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
173 help='force rebuild even if timestamps are up to date') |
|
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
174 parser.add_argument('-q', action='store_true', dest='quiet', |
|
66986
61935c8dc09e
#10454: clarify the compileall docs and help messages.
R. David Murray <rdmurray@bitdance.com>
parents:
66977
diff
changeset
|
175 help='output only error messages') |
|
66322
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
176 parser.add_argument('-b', action='store_true', dest='legacy', |
|
66986
61935c8dc09e
#10454: clarify the compileall docs and help messages.
R. David Murray <rdmurray@bitdance.com>
parents:
66977
diff
changeset
|
177 help='use legacy (pre-PEP3147) compiled file locations') |
|
66322
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
178 parser.add_argument('-d', metavar='DESTDIR', dest='ddir', default=None, |
|
66986
61935c8dc09e
#10454: clarify the compileall docs and help messages.
R. David Murray <rdmurray@bitdance.com>
parents:
66977
diff
changeset
|
179 help=('directory to prepend to file paths for use in ' |
|
72186
755a57f987ca
#10454: a few edits to compileall help messages
Éric Araujo <merwok@netwok.org>
parents:
70026
diff
changeset
|
180 'compile-time tracebacks and in runtime ' |
|
66986
61935c8dc09e
#10454: clarify the compileall docs and help messages.
R. David Murray <rdmurray@bitdance.com>
parents:
66977
diff
changeset
|
181 'tracebacks in cases where the source file is ' |
|
61935c8dc09e
#10454: clarify the compileall docs and help messages.
R. David Murray <rdmurray@bitdance.com>
parents:
66977
diff
changeset
|
182 'unavailable')) |
|
66322
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
183 parser.add_argument('-x', metavar='REGEXP', dest='rx', default=None, |
|
72186
755a57f987ca
#10454: a few edits to compileall help messages
Éric Araujo <merwok@netwok.org>
parents:
70026
diff
changeset
|
184 help=('skip files matching the regular expression; ' |
|
755a57f987ca
#10454: a few edits to compileall help messages
Éric Araujo <merwok@netwok.org>
parents:
70026
diff
changeset
|
185 'the regexp is searched for in the full path ' |
|
755a57f987ca
#10454: a few edits to compileall help messages
Éric Araujo <merwok@netwok.org>
parents:
70026
diff
changeset
|
186 'of each file considered for compilation')) |
|
66322
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
187 parser.add_argument('-i', metavar='FILE', dest='flist', |
|
66986
61935c8dc09e
#10454: clarify the compileall docs and help messages.
R. David Murray <rdmurray@bitdance.com>
parents:
66977
diff
changeset
|
188 help=('add all the files and directories listed in ' |
|
72186
755a57f987ca
#10454: a few edits to compileall help messages
Éric Araujo <merwok@netwok.org>
parents:
70026
diff
changeset
|
189 'FILE to the list considered for compilation; ' |
|
755a57f987ca
#10454: a few edits to compileall help messages
Éric Araujo <merwok@netwok.org>
parents:
70026
diff
changeset
|
190 'if "-", names are read from stdin')) |
|
66986
61935c8dc09e
#10454: clarify the compileall docs and help messages.
R. David Murray <rdmurray@bitdance.com>
parents:
66977
diff
changeset
|
191 parser.add_argument('compile_dest', metavar='FILE|DIR', nargs='*', |
|
61935c8dc09e
#10454: clarify the compileall docs and help messages.
R. David Murray <rdmurray@bitdance.com>
parents:
66977
diff
changeset
|
192 help=('zero or more file and directory names ' |
|
61935c8dc09e
#10454: clarify the compileall docs and help messages.
R. David Murray <rdmurray@bitdance.com>
parents:
66977
diff
changeset
|
193 'to compile; if no arguments given, defaults ' |
|
61935c8dc09e
#10454: clarify the compileall docs and help messages.
R. David Murray <rdmurray@bitdance.com>
parents:
66977
diff
changeset
|
194 'to the equivalent of -l sys.path')) |
|
66322
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
195 args = parser.parse_args() |
|
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
196 |
|
66907
40f543636846
More comprehensive compileall cli tests, and fixes.
R. David Murray <rdmurray@bitdance.com>
parents:
66685
diff
changeset
|
197 compile_dests = args.compile_dest |
|
40f543636846
More comprehensive compileall cli tests, and fixes.
R. David Murray <rdmurray@bitdance.com>
parents:
66685
diff
changeset
|
198 |
|
40f543636846
More comprehensive compileall cli tests, and fixes.
R. David Murray <rdmurray@bitdance.com>
parents:
66685
diff
changeset
|
199 if (args.ddir and (len(compile_dests) != 1 |
|
40f543636846
More comprehensive compileall cli tests, and fixes.
R. David Murray <rdmurray@bitdance.com>
parents:
66685
diff
changeset
|
200 or not os.path.isdir(compile_dests[0]))): |
|
40f543636846
More comprehensive compileall cli tests, and fixes.
R. David Murray <rdmurray@bitdance.com>
parents:
66685
diff
changeset
|
201 parser.exit('-d destdir requires exactly one directory argument') |
|
66322
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
202 if args.rx: |
|
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
203 import re |
|
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
204 args.rx = re.compile(args.rx) |
|
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
205 |
|
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
206 # if flist is provided then load it |
|
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
207 if args.flist: |
|
66907
40f543636846
More comprehensive compileall cli tests, and fixes.
R. David Murray <rdmurray@bitdance.com>
parents:
66685
diff
changeset
|
208 try: |
|
40f543636846
More comprehensive compileall cli tests, and fixes.
R. David Murray <rdmurray@bitdance.com>
parents:
66685
diff
changeset
|
209 with (sys.stdin if args.flist=='-' else open(args.flist)) as f: |
|
40f543636846
More comprehensive compileall cli tests, and fixes.
R. David Murray <rdmurray@bitdance.com>
parents:
66685
diff
changeset
|
210 for line in f: |
|
40f543636846
More comprehensive compileall cli tests, and fixes.
R. David Murray <rdmurray@bitdance.com>
parents:
66685
diff
changeset
|
211 compile_dests.append(line.strip()) |
|
40f543636846
More comprehensive compileall cli tests, and fixes.
R. David Murray <rdmurray@bitdance.com>
parents:
66685
diff
changeset
|
212 except EnvironmentError: |
|
40f543636846
More comprehensive compileall cli tests, and fixes.
R. David Murray <rdmurray@bitdance.com>
parents:
66685
diff
changeset
|
213 print("Error reading file list {}".format(args.flist)) |
|
40f543636846
More comprehensive compileall cli tests, and fixes.
R. David Murray <rdmurray@bitdance.com>
parents:
66685
diff
changeset
|
214 return False |
|
66322
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
215 |
|
66907
40f543636846
More comprehensive compileall cli tests, and fixes.
R. David Murray <rdmurray@bitdance.com>
parents:
66685
diff
changeset
|
216 success = True |
|
7126
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
217 try: |
|
66322
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
218 if compile_dests: |
|
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
219 for dest in compile_dests: |
|
66977
ea8f94ce8293
#10719: restore messages generated on invalid compileall args
R. David Murray <rdmurray@bitdance.com>
parents:
66907
diff
changeset
|
220 if os.path.isfile(dest): |
|
ea8f94ce8293
#10719: restore messages generated on invalid compileall args
R. David Murray <rdmurray@bitdance.com>
parents:
66907
diff
changeset
|
221 if not compile_file(dest, args.ddir, args.force, args.rx, |
|
ea8f94ce8293
#10719: restore messages generated on invalid compileall args
R. David Murray <rdmurray@bitdance.com>
parents:
66907
diff
changeset
|
222 args.quiet, args.legacy): |
|
ea8f94ce8293
#10719: restore messages generated on invalid compileall args
R. David Murray <rdmurray@bitdance.com>
parents:
66907
diff
changeset
|
223 success = False |
|
ea8f94ce8293
#10719: restore messages generated on invalid compileall args
R. David Murray <rdmurray@bitdance.com>
parents:
66907
diff
changeset
|
224 else: |
|
66322
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
225 if not compile_dir(dest, args.maxlevels, args.ddir, |
|
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
226 args.force, args.rx, args.quiet, |
|
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
227 args.legacy): |
|
66907
40f543636846
More comprehensive compileall cli tests, and fixes.
R. David Murray <rdmurray@bitdance.com>
parents:
66685
diff
changeset
|
228 success = False |
|
40f543636846
More comprehensive compileall cli tests, and fixes.
R. David Murray <rdmurray@bitdance.com>
parents:
66685
diff
changeset
|
229 return success |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7126
diff
changeset
|
230 else: |
|
66322
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
231 return compile_path(legacy=args.legacy) |
|
7126
d2c110acc60c
Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents:
3902
diff
changeset
|
232 except KeyboardInterrupt: |
|
66328
7af19e99d2f8
Fix typos and style in compileall.
Éric Araujo <merwok@netwok.org>
parents:
66322
diff
changeset
|
233 print("\n[interrupted]") |
|
66907
40f543636846
More comprehensive compileall cli tests, and fixes.
R. David Murray <rdmurray@bitdance.com>
parents:
66685
diff
changeset
|
234 return False |
|
40f543636846
More comprehensive compileall cli tests, and fixes.
R. David Murray <rdmurray@bitdance.com>
parents:
66685
diff
changeset
|
235 return True |
|
66322
4335693df259
#10453: compileall now uses argparse instead of getopt, so -h works.
R. David Murray <rdmurray@bitdance.com>
parents:
61439
diff
changeset
|
236 |
|
1828
b464e1d0b2fb
New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 |
|
b464e1d0b2fb
New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
238 if __name__ == '__main__': |
|
34201
a0c7df52ba41
Bugs item #1069409 C:\Python24\Lib\compileall.py returns False
Raymond Hettinger <python@rcn.com>
parents:
32391
diff
changeset
|
239 exit_status = int(not main()) |
|
17559
28c14a79ad22
Fix compileall.py so that it fails on SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16070
diff
changeset
|
240 sys.exit(exit_status) |
