blob: 0f4010f24eb52fcfbaff9cd929bb29900e9a7394 [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
16import sys
17import py_compile
18
Skip Montanaroe99d5ea2001-01-20 19:54:20 +000019__all__ = ["compile_dir","compile_path"]
20
Martin v. Löwis5c137c22002-03-18 12:44:08 +000021def compile_dir(dir, maxlevels=10, ddir=None,
22 force=0, rx=None, quiet=0):
Guido van Rossumc567b811998-01-19 23:07:55 +000023 """Byte-compile all modules in the given directory tree.
Guido van Rossum3bb54481994-08-29 10:52:58 +000024
Guido van Rossumc567b811998-01-19 23:07:55 +000025 Arguments (only dir is required):
26
27 dir: the directory to byte-compile
28 maxlevels: maximum recursion level (default 10)
29 ddir: if given, purported directory name (this is the
30 directory name that will show up in error messages)
Guido van Rossum0b56a3e1998-12-21 18:23:38 +000031 force: if 1, force compilation, even if timestamps are up-to-date
Martin v. Löwis5c137c22002-03-18 12:44:08 +000032 quiet: if 1, be quiet during compilation
Guido van Rossumc567b811998-01-19 23:07:55 +000033
34 """
Martin v. Löwis5c137c22002-03-18 12:44:08 +000035 if not quiet:
36 print 'Listing', dir, '...'
Guido van Rossumc567b811998-01-19 23:07:55 +000037 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000038 names = os.listdir(dir)
Guido van Rossumc567b811998-01-19 23:07:55 +000039 except os.error:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000040 print "Can't list", dir
41 names = []
Guido van Rossumc567b811998-01-19 23:07:55 +000042 names.sort()
Fred Drake9065ea31999-03-29 20:25:40 +000043 success = 1
Guido van Rossumc567b811998-01-19 23:07:55 +000044 for name in names:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000045 fullname = os.path.join(dir, name)
Raymond Hettinger8989ea62002-06-01 00:06:20 +000046 if ddir is not None:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000047 dfile = os.path.join(ddir, name)
48 else:
49 dfile = None
Raymond Hettinger8989ea62002-06-01 00:06:20 +000050 if rx is not None:
Jeremy Hylton12b64572001-04-18 01:20:21 +000051 mo = rx.search(fullname)
52 if mo:
53 continue
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000054 if os.path.isfile(fullname):
55 head, tail = name[:-3], name[-3:]
56 if tail == '.py':
Guido van Rossum0b56a3e1998-12-21 18:23:38 +000057 cfile = fullname + (__debug__ and 'c' or 'o')
Raymond Hettinger32200ae2002-06-01 19:51:15 +000058 ftime = os.stat(fullname).st_mtime
59 try: ctime = os.stat(cfile).st_mtime
Guido van Rossum0b56a3e1998-12-21 18:23:38 +000060 except os.error: ctime = 0
61 if (ctime > ftime) and not force: continue
Martin v. Löwis5c137c22002-03-18 12:44:08 +000062 if not quiet:
63 print 'Compiling', fullname, '...'
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000064 try:
Martin v. Löwis0c6774d2003-01-15 11:51:06 +000065 ok = py_compile.compile(fullname, None, dfile, True)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000066 except KeyboardInterrupt:
67 raise KeyboardInterrupt
Martin v. Löwis0c6774d2003-01-15 11:51:06 +000068 except py_compile.PyCompileError,err:
69 print err.msg
Fred Drake9065ea31999-03-29 20:25:40 +000070 success = 0
Jeremy Hylton12b64572001-04-18 01:20:21 +000071 else:
72 if ok == 0:
73 success = 0
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000074 elif maxlevels > 0 and \
75 name != os.curdir and name != os.pardir and \
76 os.path.isdir(fullname) and \
77 not os.path.islink(fullname):
Martin v. Löwis5c137c22002-03-18 12:44:08 +000078 if not compile_dir(fullname, maxlevels - 1, dfile, force, rx, quiet):
Jeremy Hylton12b64572001-04-18 01:20:21 +000079 success = 0
Fred Drake9065ea31999-03-29 20:25:40 +000080 return success
Guido van Rossumc567b811998-01-19 23:07:55 +000081
Martin v. Löwis5c137c22002-03-18 12:44:08 +000082def compile_path(skip_curdir=1, maxlevels=0, force=0, quiet=0):
Guido van Rossumc567b811998-01-19 23:07:55 +000083 """Byte-compile all module on sys.path.
84
85 Arguments (all optional):
86
87 skip_curdir: if true, skip current directory (default true)
88 maxlevels: max recursion level (default 0)
Guido van Rossum0b56a3e1998-12-21 18:23:38 +000089 force: as for compile_dir() (default 0)
Martin v. Löwis5c137c22002-03-18 12:44:08 +000090 quiet: as for compile_dir() (default 0)
Guido van Rossumc567b811998-01-19 23:07:55 +000091
92 """
Fred Drake9065ea31999-03-29 20:25:40 +000093 success = 1
Guido van Rossumc567b811998-01-19 23:07:55 +000094 for dir in sys.path:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000095 if (not dir or dir == os.curdir) and skip_curdir:
96 print 'Skipping current directory'
97 else:
Martin v. Löwis5c137c22002-03-18 12:44:08 +000098 success = success and compile_dir(dir, maxlevels, None,
99 force, quiet=quiet)
Fred Drake9065ea31999-03-29 20:25:40 +0000100 return success
Guido van Rossum3bb54481994-08-29 10:52:58 +0000101
102def main():
Guido van Rossumc567b811998-01-19 23:07:55 +0000103 """Script main program."""
104 import getopt
105 try:
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000106 opts, args = getopt.getopt(sys.argv[1:], 'lfqd:x:')
Guido van Rossumc567b811998-01-19 23:07:55 +0000107 except getopt.error, msg:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000108 print msg
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000109 print "usage: python compileall.py [-l] [-f] [-q] [-d destdir] " \
Jeremy Hylton12b64572001-04-18 01:20:21 +0000110 "[-s regexp] [directory ...]"
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000111 print "-l: don't recurse down"
Guido van Rossum0b56a3e1998-12-21 18:23:38 +0000112 print "-f: force rebuild even if timestamps are up-to-date"
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000113 print "-q: quiet operation"
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000114 print "-d destdir: purported directory name for error messages"
Jeremy Hylton12b64572001-04-18 01:20:21 +0000115 print " if no directory arguments, -l sys.path is assumed"
116 print "-x regexp: skip files matching the regular expression regexp"
117 print " the regexp is search for in the full path of the file"
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000118 sys.exit(2)
Guido van Rossumc567b811998-01-19 23:07:55 +0000119 maxlevels = 10
120 ddir = None
Guido van Rossum0b56a3e1998-12-21 18:23:38 +0000121 force = 0
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000122 quiet = 0
Jeremy Hylton12b64572001-04-18 01:20:21 +0000123 rx = None
Guido van Rossumc567b811998-01-19 23:07:55 +0000124 for o, a in opts:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000125 if o == '-l': maxlevels = 0
126 if o == '-d': ddir = a
Guido van Rossum0b56a3e1998-12-21 18:23:38 +0000127 if o == '-f': force = 1
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000128 if o == '-q': quiet = 1
Jeremy Hylton12b64572001-04-18 01:20:21 +0000129 if o == '-x':
130 import re
131 rx = re.compile(a)
Guido van Rossumc567b811998-01-19 23:07:55 +0000132 if ddir:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000133 if len(args) != 1:
134 print "-d destdir require exactly one directory argument"
135 sys.exit(2)
Fred Drake9065ea31999-03-29 20:25:40 +0000136 success = 1
Guido van Rossumc567b811998-01-19 23:07:55 +0000137 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000138 if args:
139 for dir in args:
Martin v. Löwis5c137c22002-03-18 12:44:08 +0000140 if not compile_dir(dir, maxlevels, ddir,
141 force, rx, quiet):
Jeremy Hylton12b64572001-04-18 01:20:21 +0000142 success = 0
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000143 else:
Fred Drake9065ea31999-03-29 20:25:40 +0000144 success = compile_path()
Guido van Rossumc567b811998-01-19 23:07:55 +0000145 except KeyboardInterrupt:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000146 print "\n[interrupt]"
Fred Drake9065ea31999-03-29 20:25:40 +0000147 success = 0
148 return success
Guido van Rossum3bb54481994-08-29 10:52:58 +0000149
150if __name__ == '__main__':
Jeremy Hylton12b64572001-04-18 01:20:21 +0000151 exit_status = not main()
152 sys.exit(exit_status)