annotate Lib/compileall.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 4ae85348e3e8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
72200
4ae85348e3e8 Clarify compileall command-line options (#10454).
Éric Araujo <merwok@netwok.org>
parents: 66963
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
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 import py_compile
51500
584c47d8c93f compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents: 45657
diff changeset
16 import struct
584c47d8c93f compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents: 45657
diff changeset
17 import imp
1828
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
59642
c19b4bfb806a - Fix typo in Lib/compileall.py(__all__).
Matthias Klose <doko@ubuntu.com>
parents: 59641
diff changeset
19 __all__ = ["compile_dir","compile_file","compile_path"]
16070
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 10498
diff changeset
20
22374
2417694324ef Patch #495598: add an -q (quiet) option to pycompile.
Martin v. Löwis <martin@v.loewis.de>
parents: 17559
diff changeset
21 def compile_dir(dir, maxlevels=10, ddir=None,
2417694324ef Patch #495598: add an -q (quiet) option to pycompile.
Martin v. Löwis <martin@v.loewis.de>
parents: 17559
diff changeset
22 force=0, rx=None, quiet=0):
7126
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
23 """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
24
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
25 Arguments (only dir is required):
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
26
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
27 dir: the directory to byte-compile
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
28 maxlevels: maximum recursion level (default 10)
72200
4ae85348e3e8 Clarify compileall command-line options (#10454).
Éric Araujo <merwok@netwok.org>
parents: 66963
diff changeset
29 ddir: the directory that will be prepended to the path to the
4ae85348e3e8 Clarify compileall command-line options (#10454).
Éric Araujo <merwok@netwok.org>
parents: 66963
diff changeset
30 file as it is compiled into each byte-code file.
9664
4eee061dac44 A mod whose author I forget. (I must've mislaid the email. If it's
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
31 force: if 1, force compilation, even if timestamps are up-to-date
22374
2417694324ef Patch #495598: add an -q (quiet) option to pycompile.
Martin v. Löwis <martin@v.loewis.de>
parents: 17559
diff changeset
32 quiet: if 1, be quiet during compilation
7126
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
33 """
22374
2417694324ef Patch #495598: add an -q (quiet) option to pycompile.
Martin v. Löwis <martin@v.loewis.de>
parents: 17559
diff changeset
34 if not quiet:
2417694324ef Patch #495598: add an -q (quiet) option to pycompile.
Martin v. Löwis <martin@v.loewis.de>
parents: 17559
diff changeset
35 print 'Listing', dir, '...'
7126
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
36 try:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7126
diff changeset
37 names = os.listdir(dir)
7126
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
38 except os.error:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7126
diff changeset
39 print "Can't list", dir
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7126
diff changeset
40 names = []
7126
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
41 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
42 success = 1
7126
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
43 for name in names:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7126
diff changeset
44 fullname = os.path.join(dir, name)
23610
0a2818cc342c Use is None rather than general boolean
Raymond Hettinger <python@rcn.com>
parents: 22374
diff changeset
45 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
46 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
47 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7126
diff changeset
48 dfile = None
59641
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
49 if not os.path.isdir(fullname):
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
50 if not compile_file(fullname, ddir, force, rx, quiet):
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
51 success = 0
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7126
diff changeset
52 elif maxlevels > 0 and \
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7126
diff changeset
53 name != os.curdir and name != os.pardir and \
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7126
diff changeset
54 os.path.isdir(fullname) and \
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7126
diff changeset
55 not os.path.islink(fullname):
51500
584c47d8c93f compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents: 45657
diff changeset
56 if not compile_dir(fullname, maxlevels - 1, dfile, force, rx,
584c47d8c93f compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents: 45657
diff changeset
57 quiet):
17559
28c14a79ad22 Fix compileall.py so that it fails on SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16070
diff changeset
58 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
59 return success
7126
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
60
59641
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
61 def compile_file(fullname, ddir=None, force=0, rx=None, quiet=0):
66963
d84ec85600b1 Add doc for compileall.compile_file
Éric Araujo <merwok@netwok.org>
parents: 59642
diff changeset
62 """Byte-compile one file.
d84ec85600b1 Add doc for compileall.compile_file
Éric Araujo <merwok@netwok.org>
parents: 59642
diff changeset
63
d84ec85600b1 Add doc for compileall.compile_file
Éric Araujo <merwok@netwok.org>
parents: 59642
diff changeset
64 Arguments (only fullname is required):
d84ec85600b1 Add doc for compileall.compile_file
Éric Araujo <merwok@netwok.org>
parents: 59642
diff changeset
65
d84ec85600b1 Add doc for compileall.compile_file
Éric Araujo <merwok@netwok.org>
parents: 59642
diff changeset
66 fullname: the file to byte-compile
72200
4ae85348e3e8 Clarify compileall command-line options (#10454).
Éric Araujo <merwok@netwok.org>
parents: 66963
diff changeset
67 ddir: if given, the directory name compiled in to the
4ae85348e3e8 Clarify compileall command-line options (#10454).
Éric Araujo <merwok@netwok.org>
parents: 66963
diff changeset
68 byte-code file.
59641
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
69 force: if 1, force compilation, even if timestamps are up-to-date
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
70 quiet: if 1, be quiet during compilation
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
71 """
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
72 success = 1
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
73 name = os.path.basename(fullname)
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
74 if ddir is not None:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
75 dfile = os.path.join(ddir, name)
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
76 else:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
77 dfile = None
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
78 if rx is not None:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
79 mo = rx.search(fullname)
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
80 if mo:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
81 return success
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
82 if os.path.isfile(fullname):
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
83 head, tail = name[:-3], name[-3:]
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
84 if tail == '.py':
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
85 if not force:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
86 try:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
87 mtime = int(os.stat(fullname).st_mtime)
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
88 expect = struct.pack('<4sl', imp.get_magic(), mtime)
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
89 cfile = fullname + (__debug__ and 'c' or 'o')
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
90 with open(cfile, 'rb') as chandle:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
91 actual = chandle.read(8)
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
92 if expect == actual:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
93 return success
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
94 except IOError:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
95 pass
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
96 if not quiet:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
97 print 'Compiling', fullname, '...'
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
98 try:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
99 ok = py_compile.compile(fullname, None, dfile, True)
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
100 except py_compile.PyCompileError,err:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
101 if quiet:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
102 print 'Compiling', fullname, '...'
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
103 print err.msg
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
104 success = 0
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
105 except IOError, e:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
106 print "Sorry", e
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
107 success = 0
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
108 else:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
109 if ok == 0:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
110 success = 0
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
111 return success
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
112
22374
2417694324ef Patch #495598: add an -q (quiet) option to pycompile.
Martin v. Löwis <martin@v.loewis.de>
parents: 17559
diff changeset
113 def compile_path(skip_curdir=1, maxlevels=0, force=0, quiet=0):
7126
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
114 """Byte-compile all module on sys.path.
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
115
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
116 Arguments (all optional):
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
117
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
118 skip_curdir: if true, skip current directory (default true)
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
119 maxlevels: max recursion level (default 0)
9664
4eee061dac44 A mod whose author I forget. (I must've mislaid the email. If it's
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
120 force: as for compile_dir() (default 0)
22374
2417694324ef Patch #495598: add an -q (quiet) option to pycompile.
Martin v. Löwis <martin@v.loewis.de>
parents: 17559
diff changeset
121 quiet: as for compile_dir() (default 0)
7126
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
122 """
10498
fe8645e49314 When run as a script, report failures in the exit code as well.
Fred Drake <fdrake@acm.org>
parents: 9664
diff changeset
123 success = 1
7126
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
124 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
125 if (not dir or dir == os.curdir) and skip_curdir:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7126
diff changeset
126 print 'Skipping current directory'
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7126
diff changeset
127 else:
22374
2417694324ef Patch #495598: add an -q (quiet) option to pycompile.
Martin v. Löwis <martin@v.loewis.de>
parents: 17559
diff changeset
128 success = success and compile_dir(dir, maxlevels, None,
2417694324ef Patch #495598: add an -q (quiet) option to pycompile.
Martin v. Löwis <martin@v.loewis.de>
parents: 17559
diff changeset
129 force, quiet=quiet)
10498
fe8645e49314 When run as a script, report failures in the exit code as well.
Fred Drake <fdrake@acm.org>
parents: 9664
diff changeset
130 return success
1828
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
131
59641
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
132 def expand_args(args, flist):
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
133 """read names in flist and append to args"""
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
134 expanded = args[:]
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
135 if flist:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
136 try:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
137 if flist == '-':
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
138 fd = sys.stdin
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
139 else:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
140 fd = open(flist)
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
141 while 1:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
142 line = fd.readline()
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
143 if not line:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
144 break
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
145 expanded.append(line[:-1])
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
146 except IOError:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
147 print "Error reading file list %s" % flist
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
148 raise
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
149 return expanded
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
150
1828
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 def main():
7126
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
152 """Script main program."""
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
153 import getopt
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
154 try:
59641
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
155 opts, args = getopt.getopt(sys.argv[1:], 'lfqd:x:i:')
7126
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
156 except getopt.error, msg:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7126
diff changeset
157 print msg
22374
2417694324ef Patch #495598: add an -q (quiet) option to pycompile.
Martin v. Löwis <martin@v.loewis.de>
parents: 17559
diff changeset
158 print "usage: python compileall.py [-l] [-f] [-q] [-d destdir] " \
59641
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
159 "[-x regexp] [-i list] [directory|file ...]"
72200
4ae85348e3e8 Clarify compileall command-line options (#10454).
Éric Araujo <merwok@netwok.org>
parents: 66963
diff changeset
160 print
4ae85348e3e8 Clarify compileall command-line options (#10454).
Éric Araujo <merwok@netwok.org>
parents: 66963
diff changeset
161 print "arguments: zero or more file and directory names to compile; " \
4ae85348e3e8 Clarify compileall command-line options (#10454).
Éric Araujo <merwok@netwok.org>
parents: 66963
diff changeset
162 "if no arguments given, "
4ae85348e3e8 Clarify compileall command-line options (#10454).
Éric Araujo <merwok@netwok.org>
parents: 66963
diff changeset
163 print " defaults to the equivalent of -l sys.path"
4ae85348e3e8 Clarify compileall command-line options (#10454).
Éric Araujo <merwok@netwok.org>
parents: 66963
diff changeset
164 print
4ae85348e3e8 Clarify compileall command-line options (#10454).
Éric Araujo <merwok@netwok.org>
parents: 66963
diff changeset
165 print "options:"
4ae85348e3e8 Clarify compileall command-line options (#10454).
Éric Araujo <merwok@netwok.org>
parents: 66963
diff changeset
166 print "-l: don't recurse into subdirectories"
9664
4eee061dac44 A mod whose author I forget. (I must've mislaid the email. If it's
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
167 print "-f: force rebuild even if timestamps are up-to-date"
72200
4ae85348e3e8 Clarify compileall command-line options (#10454).
Éric Araujo <merwok@netwok.org>
parents: 66963
diff changeset
168 print "-q: output only error messages"
4ae85348e3e8 Clarify compileall command-line options (#10454).
Éric Araujo <merwok@netwok.org>
parents: 66963
diff changeset
169 print "-d destdir: directory to prepend to file paths for use in " \
4ae85348e3e8 Clarify compileall command-line options (#10454).
Éric Araujo <merwok@netwok.org>
parents: 66963
diff changeset
170 "compile-time tracebacks and in"
4ae85348e3e8 Clarify compileall command-line options (#10454).
Éric Araujo <merwok@netwok.org>
parents: 66963
diff changeset
171 print " runtime tracebacks in cases where the source " \
4ae85348e3e8 Clarify compileall command-line options (#10454).
Éric Araujo <merwok@netwok.org>
parents: 66963
diff changeset
172 "file is unavailable"
4ae85348e3e8 Clarify compileall command-line options (#10454).
Éric Araujo <merwok@netwok.org>
parents: 66963
diff changeset
173 print "-x regexp: skip files matching the regular expression regexp; " \
4ae85348e3e8 Clarify compileall command-line options (#10454).
Éric Araujo <merwok@netwok.org>
parents: 66963
diff changeset
174 "the regexp is searched for"
4ae85348e3e8 Clarify compileall command-line options (#10454).
Éric Araujo <merwok@netwok.org>
parents: 66963
diff changeset
175 print " in the full path of each file considered for " \
4ae85348e3e8 Clarify compileall command-line options (#10454).
Éric Araujo <merwok@netwok.org>
parents: 66963
diff changeset
176 "compilation"
4ae85348e3e8 Clarify compileall command-line options (#10454).
Éric Araujo <merwok@netwok.org>
parents: 66963
diff changeset
177 print "-i file: add all the files and directories listed in file to " \
4ae85348e3e8 Clarify compileall command-line options (#10454).
Éric Araujo <merwok@netwok.org>
parents: 66963
diff changeset
178 "the list considered for"
4ae85348e3e8 Clarify compileall command-line options (#10454).
Éric Araujo <merwok@netwok.org>
parents: 66963
diff changeset
179 print ' compilation; if "-", names are read from stdin'
4ae85348e3e8 Clarify compileall command-line options (#10454).
Éric Araujo <merwok@netwok.org>
parents: 66963
diff changeset
180
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7126
diff changeset
181 sys.exit(2)
7126
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
182 maxlevels = 10
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
183 ddir = None
9664
4eee061dac44 A mod whose author I forget. (I must've mislaid the email. If it's
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
184 force = 0
22374
2417694324ef Patch #495598: add an -q (quiet) option to pycompile.
Martin v. Löwis <martin@v.loewis.de>
parents: 17559
diff changeset
185 quiet = 0
17559
28c14a79ad22 Fix compileall.py so that it fails on SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16070
diff changeset
186 rx = None
59641
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
187 flist = None
7126
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
188 for o, a in opts:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7126
diff changeset
189 if o == '-l': maxlevels = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7126
diff changeset
190 if o == '-d': ddir = a
9664
4eee061dac44 A mod whose author I forget. (I must've mislaid the email. If it's
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
191 if o == '-f': force = 1
22374
2417694324ef Patch #495598: add an -q (quiet) option to pycompile.
Martin v. Löwis <martin@v.loewis.de>
parents: 17559
diff changeset
192 if o == '-q': quiet = 1
17559
28c14a79ad22 Fix compileall.py so that it fails on SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16070
diff changeset
193 if o == '-x':
28c14a79ad22 Fix compileall.py so that it fails on SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16070
diff changeset
194 import re
28c14a79ad22 Fix compileall.py so that it fails on SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16070
diff changeset
195 rx = re.compile(a)
59641
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
196 if o == '-i': flist = a
7126
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
197 if ddir:
59641
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
198 if len(args) != 1 and not os.path.isdir(args[0]):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7126
diff changeset
199 print "-d destdir require exactly one directory argument"
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7126
diff changeset
200 sys.exit(2)
10498
fe8645e49314 When run as a script, report failures in the exit code as well.
Fred Drake <fdrake@acm.org>
parents: 9664
diff changeset
201 success = 1
7126
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
202 try:
59641
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
203 if args or flist:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
204 try:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
205 if flist:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
206 args = expand_args(args, flist)
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
207 except IOError:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
208 success = 0
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
209 if success:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
210 for arg in args:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
211 if os.path.isdir(arg):
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
212 if not compile_dir(arg, maxlevels, ddir,
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
213 force, rx, quiet):
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
214 success = 0
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
215 else:
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
216 if not compile_file(arg, ddir, force, rx, quiet):
eeb9bf1525ba - Issue #8140: extend compileall to compile single files. Add -i option.
Matthias Klose <doko@ubuntu.com>
parents: 59628
diff changeset
217 success = 0
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7126
diff changeset
218 else:
10498
fe8645e49314 When run as a script, report failures in the exit code as well.
Fred Drake <fdrake@acm.org>
parents: 9664
diff changeset
219 success = compile_path()
7126
d2c110acc60c Revamped, to match py_compile.py:
Guido van Rossum <guido@python.org>
parents: 3902
diff changeset
220 except KeyboardInterrupt:
72200
4ae85348e3e8 Clarify compileall command-line options (#10454).
Éric Araujo <merwok@netwok.org>
parents: 66963
diff changeset
221 print "\n[interrupted]"
10498
fe8645e49314 When run as a script, report failures in the exit code as well.
Fred Drake <fdrake@acm.org>
parents: 9664
diff changeset
222 success = 0
fe8645e49314 When run as a script, report failures in the exit code as well.
Fred Drake <fdrake@acm.org>
parents: 9664
diff changeset
223 return success
1828
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
224
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 if __name__ == '__main__':
34201
a0c7df52ba41 Bugs item #1069409 C:\Python24\Lib\compileall.py returns False
Raymond Hettinger <python@rcn.com>
parents: 32391
diff changeset
226 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
227 sys.exit(exit_status)