blob: e56c8b284e835c72417fe5579820242fefc08d5a [file] [log] [blame]
Guido van Rossumc567b811998-01-19 23:07:55 +00001"""Module/script to "compile" all .py files to .pyc (or .pyo) file.
2
3When called as a script with arguments, this compiles the directories
4given as arguments recursively; the -l option prevents it from
5recursing into directories.
6
7Without arguments, if compiles all modules on sys.path, without
8recursing into subdirectories. (Even though it should do so for
9packages -- for now, you'll have to deal with packages separately.)
10
11See module py_compile for details of the actual byte-compilation.
12
13"""
Guido van Rossum3bb54481994-08-29 10:52:58 +000014
15import os
Guido van Rossum0b56a3e1998-12-21 18:23:38 +000016import stat
Guido van Rossum3bb54481994-08-29 10:52:58 +000017import sys
18import py_compile
19
Guido van Rossum0b56a3e1998-12-21 18:23:38 +000020def compile_dir(dir, maxlevels=10, ddir=None, force=0):
Guido van Rossumc567b811998-01-19 23:07:55 +000021 """Byte-compile all modules in the given directory tree.
Guido van Rossum3bb54481994-08-29 10:52:58 +000022
Guido van Rossumc567b811998-01-19 23:07:55 +000023 Arguments (only dir is required):
24
25 dir: the directory to byte-compile
26 maxlevels: maximum recursion level (default 10)
27 ddir: if given, purported directory name (this is the
28 directory name that will show up in error messages)
Guido van Rossum0b56a3e1998-12-21 18:23:38 +000029 force: if 1, force compilation, even if timestamps are up-to-date
Guido van Rossumc567b811998-01-19 23:07:55 +000030
31 """
32 print 'Listing', dir, '...'
33 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000034 names = os.listdir(dir)
Guido van Rossumc567b811998-01-19 23:07:55 +000035 except os.error:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000036 print "Can't list", dir
37 names = []
Guido van Rossumc567b811998-01-19 23:07:55 +000038 names.sort()
Fred Drake9065ea31999-03-29 20:25:40 +000039 success = 1
Guido van Rossumc567b811998-01-19 23:07:55 +000040 for name in names:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000041 fullname = os.path.join(dir, name)
42 if ddir:
43 dfile = os.path.join(ddir, name)
44 else:
45 dfile = None
46 if os.path.isfile(fullname):
47 head, tail = name[:-3], name[-3:]
48 if tail == '.py':
Guido van Rossum0b56a3e1998-12-21 18:23:38 +000049 cfile = fullname + (__debug__ and 'c' or 'o')
50 ftime = os.stat(fullname)[stat.ST_MTIME]
51 try: ctime = os.stat(cfile)[stat.ST_MTIME]
52 except os.error: ctime = 0
53 if (ctime > ftime) and not force: continue
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000054 print 'Compiling', fullname, '...'
55 try:
56 py_compile.compile(fullname, None, dfile)
57 except KeyboardInterrupt:
58 raise KeyboardInterrupt
59 except:
60 if type(sys.exc_type) == type(''):
61 exc_type_name = sys.exc_type
62 else: exc_type_name = sys.exc_type.__name__
63 print 'Sorry:', exc_type_name + ':',
64 print sys.exc_value
Fred Drake9065ea31999-03-29 20:25:40 +000065 success = 0
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000066 elif maxlevels > 0 and \
67 name != os.curdir and name != os.pardir and \
68 os.path.isdir(fullname) and \
69 not os.path.islink(fullname):
Guido van Rossum0b56a3e1998-12-21 18:23:38 +000070 compile_dir(fullname, maxlevels - 1, dfile, force)
Fred Drake9065ea31999-03-29 20:25:40 +000071 return success
Guido van Rossumc567b811998-01-19 23:07:55 +000072
Guido van Rossum0b56a3e1998-12-21 18:23:38 +000073def compile_path(skip_curdir=1, maxlevels=0, force=0):
Guido van Rossumc567b811998-01-19 23:07:55 +000074 """Byte-compile all module on sys.path.
75
76 Arguments (all optional):
77
78 skip_curdir: if true, skip current directory (default true)
79 maxlevels: max recursion level (default 0)
Guido van Rossum0b56a3e1998-12-21 18:23:38 +000080 force: as for compile_dir() (default 0)
Guido van Rossumc567b811998-01-19 23:07:55 +000081
82 """
Fred Drake9065ea31999-03-29 20:25:40 +000083 success = 1
Guido van Rossumc567b811998-01-19 23:07:55 +000084 for dir in sys.path:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000085 if (not dir or dir == os.curdir) and skip_curdir:
86 print 'Skipping current directory'
87 else:
Fred Drake9065ea31999-03-29 20:25:40 +000088 success = success and compile_dir(dir, maxlevels, None, force)
89 return success
Guido van Rossum3bb54481994-08-29 10:52:58 +000090
91def main():
Guido van Rossumc567b811998-01-19 23:07:55 +000092 """Script main program."""
93 import getopt
94 try:
Guido van Rossum0b56a3e1998-12-21 18:23:38 +000095 opts, args = getopt.getopt(sys.argv[1:], 'lfd:')
Guido van Rossumc567b811998-01-19 23:07:55 +000096 except getopt.error, msg:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000097 print msg
Guido van Rossum0b56a3e1998-12-21 18:23:38 +000098 print "usage: compileall [-l] [-f] [-d destdir] [directory ...]"
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000099 print "-l: don't recurse down"
Guido van Rossum0b56a3e1998-12-21 18:23:38 +0000100 print "-f: force rebuild even if timestamps are up-to-date"
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000101 print "-d destdir: purported directory name for error messages"
Guido van Rossum0b56a3e1998-12-21 18:23:38 +0000102 print "if no directory arguments, -l sys.path is assumed"
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000103 sys.exit(2)
Guido van Rossumc567b811998-01-19 23:07:55 +0000104 maxlevels = 10
105 ddir = None
Guido van Rossum0b56a3e1998-12-21 18:23:38 +0000106 force = 0
Guido van Rossumc567b811998-01-19 23:07:55 +0000107 for o, a in opts:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000108 if o == '-l': maxlevels = 0
109 if o == '-d': ddir = a
Guido van Rossum0b56a3e1998-12-21 18:23:38 +0000110 if o == '-f': force = 1
Guido van Rossumc567b811998-01-19 23:07:55 +0000111 if ddir:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000112 if len(args) != 1:
113 print "-d destdir require exactly one directory argument"
114 sys.exit(2)
Fred Drake9065ea31999-03-29 20:25:40 +0000115 success = 1
Guido van Rossumc567b811998-01-19 23:07:55 +0000116 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000117 if args:
118 for dir in args:
Fred Drake9065ea31999-03-29 20:25:40 +0000119 success = success and compile_dir(dir, maxlevels, ddir, force)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000120 else:
Fred Drake9065ea31999-03-29 20:25:40 +0000121 success = compile_path()
Guido van Rossumc567b811998-01-19 23:07:55 +0000122 except KeyboardInterrupt:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000123 print "\n[interrupt]"
Fred Drake9065ea31999-03-29 20:25:40 +0000124 success = 0
125 return success
Guido van Rossum3bb54481994-08-29 10:52:58 +0000126
127if __name__ == '__main__':
Fred Drake9065ea31999-03-29 20:25:40 +0000128 sys.exit(not main())