annotate Lib/compileall.py @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents 0864a63a10a9
children d2c110acc60c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1828
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Routines to force "compilation" of all .py files in a directory
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 # tree or on sys.path. By default recursion is pruned at a depth of
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # 10 and the current directory, if it occurs in sys.path, is skipped.
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 # When called as a script, compiles argument directories, or sys.path
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # if no arguments.
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 # After a similar module by Sjoerd Mullender.
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 import os
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 import sys
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 import py_compile
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 def compile_dir(dir, maxlevels = 10):
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 print 'Listing', dir, '...'
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 try:
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 names = os.listdir(dir)
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 except os.error:
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 print "Can't list", dir
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 names = []
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 names.sort()
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 for name in names:
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 fullname = os.path.join(dir, name)
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 if os.path.isfile(fullname):
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 head, tail = name[:-3], name[-3:]
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 if tail == '.py':
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 print 'Compiling', fullname, '...'
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 try:
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 py_compile.compile(fullname)
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 except KeyboardInterrupt:
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 del names[:]
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 print '\n[interrupt]'
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 break
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 except:
2440
d577d749918d handle class exceptions
Guido van Rossum <guido@python.org>
parents: 1828
diff changeset
33 if type(sys.exc_type) == type(''):
d577d749918d handle class exceptions
Guido van Rossum <guido@python.org>
parents: 1828
diff changeset
34 exc_type_name = sys.exc_type
d577d749918d handle class exceptions
Guido van Rossum <guido@python.org>
parents: 1828
diff changeset
35 else: exc_type_name = sys.exc_type.__name__
d577d749918d handle class exceptions
Guido van Rossum <guido@python.org>
parents: 1828
diff changeset
36 print 'Sorry:', exc_type_name + ':',
1828
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 print sys.exc_value
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 elif maxlevels > 0 and \
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 name != os.curdir and name != os.pardir and \
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 os.path.isdir(fullname) and \
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 not os.path.islink(fullname):
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 compile_dir(fullname, maxlevels - 1)
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 def compile_path(skip_curdir = 1):
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 for dir in sys.path:
3902
0864a63a10a9 Don't trip over empty string in sys.path.
Guido van Rossum <guido@python.org>
parents: 2440
diff changeset
46 if (not dir or dir == os.curdir) and skip_curdir:
1828
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 print 'Skipping current directory'
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 else:
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 compile_dir(dir, 0)
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 def main():
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 import getopt
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 try:
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 opts, args = getopt.getopt(sys.argv[1:], 'l')
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 except getopt.error, msg:
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 print msg
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 print "usage: compileall [-l] [directory ...]"
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 print "-l: don't recurse down"
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 print "if no arguments, -l sys.path is assumed"
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 maxlevels = 10
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 for o, a in opts:
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 if o == '-l': maxlevels = 0
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 if args:
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 for dir in sys.argv[1:]:
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 compile_dir(dir, maxlevels)
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 else:
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 compile_path()
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 if __name__ == '__main__':
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 main()