Skip to content

Commit 995d45e

Browse files
committed
Revamped, to match py_compile.py:
- added docstrings - support option to specify a different purported directory name - reindented with 4 spaces
1 parent 774faed commit 995d45e

File tree

1 file changed

+97
-58
lines changed

1 file changed

+97
-58
lines changed

Lib/compileall.py

Lines changed: 97 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,109 @@
1-
# Routines to force "compilation" of all .py files in a directory
2-
# tree or on sys.path. By default recursion is pruned at a depth of
3-
# 10 and the current directory, if it occurs in sys.path, is skipped.
4-
# When called as a script, compiles argument directories, or sys.path
5-
# if no arguments.
6-
# After a similar module by Sjoerd Mullender.
1+
"""Module/script to "compile" all .py files to .pyc (or .pyo) file.
2+
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.
6+
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.)
10+
11+
See module py_compile for details of the actual byte-compilation.
12+
13+
"""
714

815
import os
916
import sys
1017
import py_compile
1118

12-
def compile_dir(dir, maxlevels = 10):
13-
print 'Listing', dir, '...'
14-
try:
15-
names = os.listdir(dir)
16-
except os.error:
17-
print "Can't list", dir
18-
names = []
19-
names.sort()
20-
for name in names:
21-
fullname = os.path.join(dir, name)
22-
if os.path.isfile(fullname):
23-
head, tail = name[:-3], name[-3:]
24-
if tail == '.py':
25-
print 'Compiling', fullname, '...'
26-
try:
27-
py_compile.compile(fullname)
28-
except KeyboardInterrupt:
29-
del names[:]
30-
print '\n[interrupt]'
31-
break
32-
except:
33-
if type(sys.exc_type) == type(''):
34-
exc_type_name = sys.exc_type
35-
else: exc_type_name = sys.exc_type.__name__
36-
print 'Sorry:', exc_type_name + ':',
37-
print sys.exc_value
38-
elif maxlevels > 0 and \
39-
name != os.curdir and name != os.pardir and \
40-
os.path.isdir(fullname) and \
41-
not os.path.islink(fullname):
42-
compile_dir(fullname, maxlevels - 1)
19+
def compile_dir(dir, maxlevels=10, ddir=None):
20+
"""Byte-compile all modules in the given directory tree.
21+
22+
Arguments (only dir is required):
23+
24+
dir: the directory to byte-compile
25+
maxlevels: maximum recursion level (default 10)
26+
ddir: if given, purported directory name (this is the
27+
directory name that will show up in error messages)
4328
44-
def compile_path(skip_curdir = 1):
45-
for dir in sys.path:
46-
if (not dir or dir == os.curdir) and skip_curdir:
47-
print 'Skipping current directory'
48-
else:
49-
compile_dir(dir, 0)
29+
"""
30+
print 'Listing', dir, '...'
31+
try:
32+
names = os.listdir(dir)
33+
except os.error:
34+
print "Can't list", dir
35+
names = []
36+
names.sort()
37+
for name in names:
38+
fullname = os.path.join(dir, name)
39+
if ddir:
40+
dfile = os.path.join(ddir, name)
41+
else:
42+
dfile = None
43+
if os.path.isfile(fullname):
44+
head, tail = name[:-3], name[-3:]
45+
if tail == '.py':
46+
print 'Compiling', fullname, '...'
47+
try:
48+
py_compile.compile(fullname, None, dfile)
49+
except KeyboardInterrupt:
50+
raise KeyboardInterrupt
51+
except:
52+
if type(sys.exc_type) == type(''):
53+
exc_type_name = sys.exc_type
54+
else: exc_type_name = sys.exc_type.__name__
55+
print 'Sorry:', exc_type_name + ':',
56+
print sys.exc_value
57+
elif maxlevels > 0 and \
58+
name != os.curdir and name != os.pardir and \
59+
os.path.isdir(fullname) and \
60+
not os.path.islink(fullname):
61+
compile_dir(fullname, maxlevels - 1, dfile)
62+
63+
def compile_path(skip_curdir=1, maxlevels=0):
64+
"""Byte-compile all module on sys.path.
65+
66+
Arguments (all optional):
67+
68+
skip_curdir: if true, skip current directory (default true)
69+
maxlevels: max recursion level (default 0)
70+
71+
"""
72+
for dir in sys.path:
73+
if (not dir or dir == os.curdir) and skip_curdir:
74+
print 'Skipping current directory'
75+
else:
76+
compile_dir(dir, maxlevels)
5077

5178
def main():
52-
import getopt
53-
try:
54-
opts, args = getopt.getopt(sys.argv[1:], 'l')
55-
except getopt.error, msg:
56-
print msg
57-
print "usage: compileall [-l] [directory ...]"
58-
print "-l: don't recurse down"
59-
print "if no arguments, -l sys.path is assumed"
60-
maxlevels = 10
61-
for o, a in opts:
62-
if o == '-l': maxlevels = 0
79+
"""Script main program."""
80+
import getopt
81+
try:
82+
opts, args = getopt.getopt(sys.argv[1:], 'ld:')
83+
except getopt.error, msg:
84+
print msg
85+
print "usage: compileall [-l] [-d destdir] [directory ...]"
86+
print "-l: don't recurse down"
87+
print "-d destdir: purported directory name for error messages"
88+
print "if no arguments, -l sys.path is assumed"
89+
sys.exit(2)
90+
maxlevels = 10
91+
ddir = None
92+
for o, a in opts:
93+
if o == '-l': maxlevels = 0
94+
if o == '-d': ddir = a
95+
if ddir:
96+
if len(args) != 1:
97+
print "-d destdir require exactly one directory argument"
98+
sys.exit(2)
99+
try:
63100
if args:
64-
for dir in sys.argv[1:]:
65-
compile_dir(dir, maxlevels)
101+
for dir in args:
102+
compile_dir(dir, maxlevels, ddir)
66103
else:
67-
compile_path()
104+
compile_path()
105+
except KeyboardInterrupt:
106+
print "\n[interrupt]"
68107

69108
if __name__ == '__main__':
70-
main()
109+
main()

0 commit comments

Comments
 (0)