Éric Araujo | 2532f11 | 2011-09-01 20:04:50 +0200 | [diff] [blame] | 1 | """Module/script to byte-compile all .py files to .pyc (or .pyo) files. |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 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. |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 12 | """ |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 13 | import os |
| 14 | import sys |
| 15 | import py_compile |
Brett Cannon | 28d1088 | 2009-02-10 02:07:38 +0000 | [diff] [blame] | 16 | import struct |
| 17 | import imp |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 18 | |
Matthias Klose | fae23dc | 2010-03-15 18:00:01 +0000 | [diff] [blame] | 19 | __all__ = ["compile_dir","compile_file","compile_path"] |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 20 | |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 21 | def compile_dir(dir, maxlevels=10, ddir=None, |
| 22 | force=0, rx=None, quiet=0): |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 23 | """Byte-compile all modules in the given directory tree. |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 25 | Arguments (only dir is required): |
| 26 | |
| 27 | dir: the directory to byte-compile |
| 28 | maxlevels: maximum recursion level (default 10) |
Éric Araujo | 2532f11 | 2011-09-01 20:04:50 +0200 | [diff] [blame] | 29 | ddir: the directory that will be prepended to the path to the |
| 30 | file as it is compiled into each byte-code file. |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 31 | force: if 1, force compilation, even if timestamps are up-to-date |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 32 | quiet: if 1, be quiet during compilation |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 33 | """ |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 34 | if not quiet: |
| 35 | print 'Listing', dir, '...' |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 36 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 37 | names = os.listdir(dir) |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 38 | except os.error: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 39 | print "Can't list", dir |
| 40 | names = [] |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 41 | names.sort() |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 42 | success = 1 |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 43 | for name in names: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 44 | fullname = os.path.join(dir, name) |
Raymond Hettinger | 8989ea6 | 2002-06-01 00:06:20 +0000 | [diff] [blame] | 45 | if ddir is not None: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 46 | dfile = os.path.join(ddir, name) |
| 47 | else: |
| 48 | dfile = None |
Matthias Klose | b13d04c | 2010-03-15 17:44:12 +0000 | [diff] [blame] | 49 | if not os.path.isdir(fullname): |
| 50 | if not compile_file(fullname, ddir, force, rx, quiet): |
| 51 | success = 0 |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 52 | elif maxlevels > 0 and \ |
| 53 | name != os.curdir and name != os.pardir and \ |
| 54 | os.path.isdir(fullname) and \ |
| 55 | not os.path.islink(fullname): |
Brett Cannon | 28d1088 | 2009-02-10 02:07:38 +0000 | [diff] [blame] | 56 | if not compile_dir(fullname, maxlevels - 1, dfile, force, rx, |
| 57 | quiet): |
Jeremy Hylton | 12b6457 | 2001-04-18 01:20:21 +0000 | [diff] [blame] | 58 | success = 0 |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 59 | return success |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 60 | |
Matthias Klose | b13d04c | 2010-03-15 17:44:12 +0000 | [diff] [blame] | 61 | def compile_file(fullname, ddir=None, force=0, rx=None, quiet=0): |
Éric Araujo | c11ba76 | 2010-12-16 06:15:02 +0000 | [diff] [blame] | 62 | """Byte-compile one file. |
| 63 | |
| 64 | Arguments (only fullname is required): |
| 65 | |
| 66 | fullname: the file to byte-compile |
Éric Araujo | 2532f11 | 2011-09-01 20:04:50 +0200 | [diff] [blame] | 67 | ddir: if given, the directory name compiled in to the |
| 68 | byte-code file. |
Matthias Klose | b13d04c | 2010-03-15 17:44:12 +0000 | [diff] [blame] | 69 | force: if 1, force compilation, even if timestamps are up-to-date |
| 70 | quiet: if 1, be quiet during compilation |
Matthias Klose | b13d04c | 2010-03-15 17:44:12 +0000 | [diff] [blame] | 71 | """ |
Matthias Klose | b13d04c | 2010-03-15 17:44:12 +0000 | [diff] [blame] | 72 | success = 1 |
| 73 | name = os.path.basename(fullname) |
| 74 | if ddir is not None: |
| 75 | dfile = os.path.join(ddir, name) |
| 76 | else: |
| 77 | dfile = None |
| 78 | if rx is not None: |
| 79 | mo = rx.search(fullname) |
| 80 | if mo: |
| 81 | return success |
| 82 | if os.path.isfile(fullname): |
| 83 | head, tail = name[:-3], name[-3:] |
| 84 | if tail == '.py': |
| 85 | if not force: |
| 86 | try: |
| 87 | mtime = int(os.stat(fullname).st_mtime) |
| 88 | expect = struct.pack('<4sl', imp.get_magic(), mtime) |
| 89 | cfile = fullname + (__debug__ and 'c' or 'o') |
| 90 | with open(cfile, 'rb') as chandle: |
| 91 | actual = chandle.read(8) |
| 92 | if expect == actual: |
| 93 | return success |
| 94 | except IOError: |
| 95 | pass |
| 96 | if not quiet: |
| 97 | print 'Compiling', fullname, '...' |
| 98 | try: |
| 99 | ok = py_compile.compile(fullname, None, dfile, True) |
| 100 | except py_compile.PyCompileError,err: |
| 101 | if quiet: |
| 102 | print 'Compiling', fullname, '...' |
| 103 | print err.msg |
| 104 | success = 0 |
| 105 | except IOError, e: |
| 106 | print "Sorry", e |
| 107 | success = 0 |
| 108 | else: |
| 109 | if ok == 0: |
| 110 | success = 0 |
| 111 | return success |
| 112 | |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 113 | def compile_path(skip_curdir=1, maxlevels=0, force=0, quiet=0): |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 114 | """Byte-compile all module on sys.path. |
| 115 | |
| 116 | Arguments (all optional): |
| 117 | |
| 118 | skip_curdir: if true, skip current directory (default true) |
| 119 | maxlevels: max recursion level (default 0) |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 120 | force: as for compile_dir() (default 0) |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 121 | quiet: as for compile_dir() (default 0) |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 122 | """ |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 123 | success = 1 |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 124 | for dir in sys.path: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 125 | if (not dir or dir == os.curdir) and skip_curdir: |
| 126 | print 'Skipping current directory' |
| 127 | else: |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 128 | success = success and compile_dir(dir, maxlevels, None, |
| 129 | force, quiet=quiet) |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 130 | return success |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 131 | |
Matthias Klose | b13d04c | 2010-03-15 17:44:12 +0000 | [diff] [blame] | 132 | def expand_args(args, flist): |
| 133 | """read names in flist and append to args""" |
| 134 | expanded = args[:] |
| 135 | if flist: |
| 136 | try: |
| 137 | if flist == '-': |
| 138 | fd = sys.stdin |
| 139 | else: |
| 140 | fd = open(flist) |
| 141 | while 1: |
| 142 | line = fd.readline() |
| 143 | if not line: |
| 144 | break |
| 145 | expanded.append(line[:-1]) |
| 146 | except IOError: |
| 147 | print "Error reading file list %s" % flist |
| 148 | raise |
| 149 | return expanded |
| 150 | |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 151 | def main(): |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 152 | """Script main program.""" |
| 153 | import getopt |
| 154 | try: |
Matthias Klose | b13d04c | 2010-03-15 17:44:12 +0000 | [diff] [blame] | 155 | opts, args = getopt.getopt(sys.argv[1:], 'lfqd:x:i:') |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 156 | except getopt.error, msg: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 157 | print msg |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 158 | print "usage: python compileall.py [-l] [-f] [-q] [-d destdir] " \ |
Matthias Klose | b13d04c | 2010-03-15 17:44:12 +0000 | [diff] [blame] | 159 | "[-x regexp] [-i list] [directory|file ...]" |
Éric Araujo | 2532f11 | 2011-09-01 20:04:50 +0200 | [diff] [blame] | 160 | print |
| 161 | print "arguments: zero or more file and directory names to compile; " \ |
| 162 | "if no arguments given, " |
| 163 | print " defaults to the equivalent of -l sys.path" |
| 164 | print |
| 165 | print "options:" |
| 166 | print "-l: don't recurse into subdirectories" |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 167 | print "-f: force rebuild even if timestamps are up-to-date" |
Éric Araujo | 2532f11 | 2011-09-01 20:04:50 +0200 | [diff] [blame] | 168 | print "-q: output only error messages" |
| 169 | print "-d destdir: directory to prepend to file paths for use in " \ |
| 170 | "compile-time tracebacks and in" |
| 171 | print " runtime tracebacks in cases where the source " \ |
| 172 | "file is unavailable" |
| 173 | print "-x regexp: skip files matching the regular expression regexp; " \ |
| 174 | "the regexp is searched for" |
| 175 | print " in the full path of each file considered for " \ |
| 176 | "compilation" |
| 177 | print "-i file: add all the files and directories listed in file to " \ |
| 178 | "the list considered for" |
| 179 | print ' compilation; if "-", names are read from stdin' |
| 180 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 181 | sys.exit(2) |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 182 | maxlevels = 10 |
| 183 | ddir = None |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 184 | force = 0 |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 185 | quiet = 0 |
Jeremy Hylton | 12b6457 | 2001-04-18 01:20:21 +0000 | [diff] [blame] | 186 | rx = None |
Matthias Klose | b13d04c | 2010-03-15 17:44:12 +0000 | [diff] [blame] | 187 | flist = None |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 188 | for o, a in opts: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 189 | if o == '-l': maxlevels = 0 |
| 190 | if o == '-d': ddir = a |
Guido van Rossum | 0b56a3e | 1998-12-21 18:23:38 +0000 | [diff] [blame] | 191 | if o == '-f': force = 1 |
Martin v. Löwis | 5c137c2 | 2002-03-18 12:44:08 +0000 | [diff] [blame] | 192 | if o == '-q': quiet = 1 |
Jeremy Hylton | 12b6457 | 2001-04-18 01:20:21 +0000 | [diff] [blame] | 193 | if o == '-x': |
| 194 | import re |
| 195 | rx = re.compile(a) |
Matthias Klose | b13d04c | 2010-03-15 17:44:12 +0000 | [diff] [blame] | 196 | if o == '-i': flist = a |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 197 | if ddir: |
Matthias Klose | b13d04c | 2010-03-15 17:44:12 +0000 | [diff] [blame] | 198 | if len(args) != 1 and not os.path.isdir(args[0]): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 199 | print "-d destdir require exactly one directory argument" |
| 200 | sys.exit(2) |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 201 | success = 1 |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 202 | try: |
Matthias Klose | b13d04c | 2010-03-15 17:44:12 +0000 | [diff] [blame] | 203 | if args or flist: |
| 204 | try: |
| 205 | if flist: |
| 206 | args = expand_args(args, flist) |
| 207 | except IOError: |
| 208 | success = 0 |
| 209 | if success: |
| 210 | for arg in args: |
| 211 | if os.path.isdir(arg): |
| 212 | if not compile_dir(arg, maxlevels, ddir, |
| 213 | force, rx, quiet): |
| 214 | success = 0 |
| 215 | else: |
| 216 | if not compile_file(arg, ddir, force, rx, quiet): |
| 217 | success = 0 |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 218 | else: |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 219 | success = compile_path() |
Guido van Rossum | c567b81 | 1998-01-19 23:07:55 +0000 | [diff] [blame] | 220 | except KeyboardInterrupt: |
Éric Araujo | 2532f11 | 2011-09-01 20:04:50 +0200 | [diff] [blame] | 221 | print "\n[interrupted]" |
Fred Drake | 9065ea3 | 1999-03-29 20:25:40 +0000 | [diff] [blame] | 222 | success = 0 |
| 223 | return success |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 224 | |
| 225 | if __name__ == '__main__': |
Raymond Hettinger | 7b4b788 | 2004-12-20 00:29:29 +0000 | [diff] [blame] | 226 | exit_status = int(not main()) |
Jeremy Hylton | 12b6457 | 2001-04-18 01:20:21 +0000 | [diff] [blame] | 227 | sys.exit(exit_status) |