Ignore:
Timestamp:
Mar 19, 2014, 11:11:30 AM (11 years ago)
Author:
dmik
Message:

python: Update vendor to 2.7.6.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/vendor/current/Lib/compileall.py

    r2 r388  
    1 """Module/script to "compile" all .py files to .pyc (or .pyo) file.
     1"""Module/script to byte-compile all .py files to .pyc (or .pyo) files.
    22
    33When called as a script with arguments, this compiles the directories
     
    1010
    1111See module py_compile for details of the actual byte-compilation.
    12 
    1312"""
    14 
    1513import os
    1614import sys
    1715import py_compile
    18 
    19 __all__ = ["compile_dir","compile_path"]
     16import struct
     17import imp
     18
     19__all__ = ["compile_dir","compile_file","compile_path"]
    2020
    2121def compile_dir(dir, maxlevels=10, ddir=None,
     
    2727    dir:       the directory to byte-compile
    2828    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)
     29    ddir:      the directory that will be prepended to the path to the
     30               file as it is compiled into each byte-code file.
    3131    force:     if 1, force compilation, even if timestamps are up-to-date
    3232    quiet:     if 1, be quiet during compilation
    33 
    3433    """
    3534    if not quiet:
     
    4847        else:
    4948            dfile = None
    50         if rx is not None:
    51             mo = rx.search(fullname)
    52             if mo:
    53                 continue
    54         if os.path.isfile(fullname):
    55             head, tail = name[:-3], name[-3:]
    56             if tail == '.py':
    57                 cfile = fullname + (__debug__ and 'c' or 'o')
    58                 ftime = os.stat(fullname).st_mtime
    59                 try: ctime = os.stat(cfile).st_mtime
    60                 except os.error: ctime = 0
    61                 if (ctime > ftime) and not force: continue
    62                 if not quiet:
    63                     print 'Compiling', fullname, '...'
    64                 try:
    65                     ok = py_compile.compile(fullname, None, dfile, True)
    66                 except KeyboardInterrupt:
    67                     raise KeyboardInterrupt
    68                 except py_compile.PyCompileError,err:
    69                     if quiet:
    70                         print 'Compiling', fullname, '...'
    71                     print err.msg
    72                     success = 0
    73                 except IOError, e:
    74                     print "Sorry", e
    75                     success = 0
    76                 else:
    77                     if ok == 0:
    78                         success = 0
     49        if not os.path.isdir(fullname):
     50            if not compile_file(fullname, ddir, force, rx, quiet):
     51                success = 0
    7952        elif maxlevels > 0 and \
    8053             name != os.curdir and name != os.pardir and \
    8154             os.path.isdir(fullname) and \
    8255             not os.path.islink(fullname):
    83             if not compile_dir(fullname, maxlevels - 1, dfile, force, rx, quiet):
    84                 success = 0
     56            if not compile_dir(fullname, maxlevels - 1, dfile, force, rx,
     57                               quiet):
     58                success = 0
     59    return success
     60
     61def compile_file(fullname, ddir=None, force=0, rx=None, quiet=0):
     62    """Byte-compile one file.
     63
     64    Arguments (only fullname is required):
     65
     66    fullname:  the file to byte-compile
     67    ddir:      if given, the directory name compiled in to the
     68               byte-code file.
     69    force:     if 1, force compilation, even if timestamps are up-to-date
     70    quiet:     if 1, be quiet during compilation
     71    """
     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
    85111    return success
    86112
     
    94120    force: as for compile_dir() (default 0)
    95121    quiet: as for compile_dir() (default 0)
    96 
    97122    """
    98123    success = 1
     
    105130    return success
    106131
     132def 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
    107151def main():
    108152    """Script main program."""
    109153    import getopt
    110154    try:
    111         opts, args = getopt.getopt(sys.argv[1:], 'lfqd:x:')
     155        opts, args = getopt.getopt(sys.argv[1:], 'lfqd:x:i:')
    112156    except getopt.error, msg:
    113157        print msg
    114158        print "usage: python compileall.py [-l] [-f] [-q] [-d destdir] " \
    115               "[-x regexp] [directory ...]"
    116         print "-l: don't recurse down"
     159              "[-x regexp] [-i list] [directory|file ...]"
     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"
    117167        print "-f: force rebuild even if timestamps are up-to-date"
    118         print "-q: quiet operation"
    119         print "-d destdir: purported directory name for error messages"
    120         print "   if no directory arguments, -l sys.path is assumed"
    121         print "-x regexp: skip files matching the regular expression regexp"
    122         print "   the regexp is searched for in the full path of the file"
     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
    123181        sys.exit(2)
    124182    maxlevels = 10
     
    127185    quiet = 0
    128186    rx = None
     187    flist = None
    129188    for o, a in opts:
    130189        if o == '-l': maxlevels = 0
     
    135194            import re
    136195            rx = re.compile(a)
     196        if o == '-i': flist = a
    137197    if ddir:
    138         if len(args) != 1:
     198        if len(args) != 1 and not os.path.isdir(args[0]):
    139199            print "-d destdir require exactly one directory argument"
    140200            sys.exit(2)
    141201    success = 1
    142202    try:
    143         if args:
    144             for dir in args:
    145                 if not compile_dir(dir, maxlevels, ddir,
    146                                    force, rx, quiet):
    147                     success = 0
     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
    148218        else:
    149219            success = compile_path()
    150220    except KeyboardInterrupt:
    151         print "\n[interrupt]"
     221        print "\n[interrupted]"
    152222        success = 0
    153223    return success
Note: See TracChangeset for help on using the changeset viewer.