compileall.py 4.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
"""Module/script to "compile" all .py files to .pyc (or .pyo) file.

When called as a script with arguments, this compiles the directories
given as arguments recursively; the -l option prevents it from
recursing into directories.

Without arguments, if compiles all modules on sys.path, without
recursing into subdirectories.  (Even though it should do so for
packages -- for now, you'll have to deal with packages separately.)

See module py_compile for details of the actual byte-compilation.

"""
14 15

import os
16
import stat
17 18 19
import sys
import py_compile

20 21
__all__ = ["compile_dir","compile_path"]

22
def compile_dir(dir, maxlevels=10, ddir=None, force=0, rx=None):
23 24 25 26 27 28 29 30
    """Byte-compile all modules in the given directory tree.

    Arguments (only dir is required):

    dir:       the directory to byte-compile
    maxlevels: maximum recursion level (default 10)
    ddir:      if given, purported directory name (this is the
               directory name that will show up in error messages)
31
    force:     if 1, force compilation, even if timestamps are up-to-date
32

33 34 35
    """
    print 'Listing', dir, '...'
    try:
36
        names = os.listdir(dir)
37
    except os.error:
38 39
        print "Can't list", dir
        names = []
40
    names.sort()
41
    success = 1
42
    for name in names:
43 44 45 46 47
        fullname = os.path.join(dir, name)
        if ddir:
            dfile = os.path.join(ddir, name)
        else:
            dfile = None
48 49 50 51
        if rx:
            mo = rx.search(fullname)
            if mo:
                continue
52 53 54
        if os.path.isfile(fullname):
            head, tail = name[:-3], name[-3:]
            if tail == '.py':
55 56 57 58 59
                cfile = fullname + (__debug__ and 'c' or 'o')
                ftime = os.stat(fullname)[stat.ST_MTIME]
                try: ctime = os.stat(cfile)[stat.ST_MTIME]
                except os.error: ctime = 0
                if (ctime > ftime) and not force: continue
60 61
                print 'Compiling', fullname, '...'
                try:
62
                    ok = py_compile.compile(fullname, None, dfile)
63 64 65
                except KeyboardInterrupt:
                    raise KeyboardInterrupt
                except:
66
                    # XXX py_compile catches SyntaxErrors
67 68 69 70 71
                    if type(sys.exc_type) == type(''):
                        exc_type_name = sys.exc_type
                    else: exc_type_name = sys.exc_type.__name__
                    print 'Sorry:', exc_type_name + ':',
                    print sys.exc_value
72
                    success = 0
73 74 75
                else:
                    if ok == 0:
                        success = 0
76 77 78 79
        elif maxlevels > 0 and \
             name != os.curdir and name != os.pardir and \
             os.path.isdir(fullname) and \
             not os.path.islink(fullname):
80 81
            if not compile_dir(fullname, maxlevels - 1, dfile, force, rx):
                success = 0
82
    return success
83

84
def compile_path(skip_curdir=1, maxlevels=0, force=0):
85 86 87 88 89 90
    """Byte-compile all module on sys.path.

    Arguments (all optional):

    skip_curdir: if true, skip current directory (default true)
    maxlevels:   max recursion level (default 0)
91
    force: as for compile_dir() (default 0)
92 93

    """
94
    success = 1
95
    for dir in sys.path:
96 97 98
        if (not dir or dir == os.curdir) and skip_curdir:
            print 'Skipping current directory'
        else:
99 100
            success = success and compile_dir(dir, maxlevels, None, force)
    return success
101 102

def main():
103 104 105
    """Script main program."""
    import getopt
    try:
106
        opts, args = getopt.getopt(sys.argv[1:], 'lfd:x:')
107
    except getopt.error, msg:
108
        print msg
109 110
        print "usage: python compileall.py [-l] [-f] [-d destdir] " \
              "[-s regexp] [directory ...]"
111
        print "-l: don't recurse down"
112
        print "-f: force rebuild even if timestamps are up-to-date"
113
        print "-d destdir: purported directory name for error messages"
114 115 116
        print "   if no directory arguments, -l sys.path is assumed"
        print "-x regexp: skip files matching the regular expression regexp"
        print "   the regexp is search for in the full path of the file"
117
        sys.exit(2)
118 119
    maxlevels = 10
    ddir = None
120
    force = 0
121
    rx = None
122
    for o, a in opts:
123 124
        if o == '-l': maxlevels = 0
        if o == '-d': ddir = a
125
        if o == '-f': force = 1
126 127 128
        if o == '-x':
            import re
            rx = re.compile(a)
129
    if ddir:
130 131 132
        if len(args) != 1:
            print "-d destdir require exactly one directory argument"
            sys.exit(2)
133
    success = 1
134
    try:
135 136
        if args:
            for dir in args:
137 138
                if not compile_dir(dir, maxlevels, ddir, force, rx):
                    success = 0
139
        else:
140
            success = compile_path()
141
    except KeyboardInterrupt:
142
        print "\n[interrupt]"
143 144
        success = 0
    return success
145 146

if __name__ == '__main__':
147 148
    exit_status = not main()
    sys.exit(exit_status)