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