PyXR

c:\python24\lib \ compileall.py



0001 """Module/script to "compile" all .py files to .pyc (or .pyo) file.
0002 
0003 When called as a script with arguments, this compiles the directories
0004 given as arguments recursively; the -l option prevents it from
0005 recursing into directories.
0006 
0007 Without arguments, if compiles all modules on sys.path, without
0008 recursing into subdirectories.  (Even though it should do so for
0009 packages -- for now, you'll have to deal with packages separately.)
0010 
0011 See module py_compile for details of the actual byte-compilation.
0012 
0013 """
0014 
0015 import os
0016 import sys
0017 import py_compile
0018 
0019 __all__ = ["compile_dir","compile_path"]
0020 
0021 def compile_dir(dir, maxlevels=10, ddir=None,
0022                 force=0, rx=None, quiet=0):
0023     """Byte-compile all modules in the given directory tree.
0024 
0025     Arguments (only dir is required):
0026 
0027     dir:       the directory to byte-compile
0028     maxlevels: maximum recursion level (default 10)
0029     ddir:      if given, purported directory name (this is the
0030                directory name that will show up in error messages)
0031     force:     if 1, force compilation, even if timestamps are up-to-date
0032     quiet:     if 1, be quiet during compilation
0033 
0034     """
0035     if not quiet:
0036         print 'Listing', dir, '...'
0037     try:
0038         names = os.listdir(dir)
0039     except os.error:
0040         print "Can't list", dir
0041         names = []
0042     names.sort()
0043     success = 1
0044     for name in names:
0045         fullname = os.path.join(dir, name)
0046         if ddir is not None:
0047             dfile = os.path.join(ddir, name)
0048         else:
0049             dfile = None
0050         if rx is not None:
0051             mo = rx.search(fullname)
0052             if mo:
0053                 continue
0054         if os.path.isfile(fullname):
0055             head, tail = name[:-3], name[-3:]
0056             if tail == '.py':
0057                 cfile = fullname + (__debug__ and 'c' or 'o')
0058                 ftime = os.stat(fullname).st_mtime
0059                 try: ctime = os.stat(cfile).st_mtime
0060                 except os.error: ctime = 0
0061                 if (ctime > ftime) and not force: continue
0062                 if not quiet:
0063                     print 'Compiling', fullname, '...'
0064                 try:
0065                     ok = py_compile.compile(fullname, None, dfile, True)
0066                 except KeyboardInterrupt:
0067                     raise KeyboardInterrupt
0068                 except py_compile.PyCompileError,err:
0069                     if quiet:
0070                         print 'Compiling', fullname, '...'
0071                     print err.msg
0072                     success = 0
0073                 except IOError, e:
0074                     print "Sorry", e
0075                     success = 0
0076                 else:
0077                     if ok == 0:
0078                         success = 0
0079         elif maxlevels > 0 and \
0080              name != os.curdir and name != os.pardir and \
0081              os.path.isdir(fullname) and \
0082              not os.path.islink(fullname):
0083             if not compile_dir(fullname, maxlevels - 1, dfile, force, rx, quiet):
0084                 success = 0
0085     return success
0086 
0087 def compile_path(skip_curdir=1, maxlevels=0, force=0, quiet=0):
0088     """Byte-compile all module on sys.path.
0089 
0090     Arguments (all optional):
0091 
0092     skip_curdir: if true, skip current directory (default true)
0093     maxlevels:   max recursion level (default 0)
0094     force: as for compile_dir() (default 0)
0095     quiet: as for compile_dir() (default 0)
0096 
0097     """
0098     success = 1
0099     for dir in sys.path:
0100         if (not dir or dir == os.curdir) and skip_curdir:
0101             print 'Skipping current directory'
0102         else:
0103             success = success and compile_dir(dir, maxlevels, None,
0104                                               force, quiet=quiet)
0105     return success
0106 
0107 def main():
0108     """Script main program."""
0109     import getopt
0110     try:
0111         opts, args = getopt.getopt(sys.argv[1:], 'lfqd:x:')
0112     except getopt.error, msg:
0113         print msg
0114         print "usage: python compileall.py [-l] [-f] [-q] [-d destdir] " \
0115               "[-x regexp] [directory ...]"
0116         print "-l: don't recurse down"
0117         print "-f: force rebuild even if timestamps are up-to-date"
0118         print "-q: quiet operation"
0119         print "-d destdir: purported directory name for error messages"
0120         print "   if no directory arguments, -l sys.path is assumed"
0121         print "-x regexp: skip files matching the regular expression regexp"
0122         print "   the regexp is search for in the full path of the file"
0123         sys.exit(2)
0124     maxlevels = 10
0125     ddir = None
0126     force = 0
0127     quiet = 0
0128     rx = None
0129     for o, a in opts:
0130         if o == '-l': maxlevels = 0
0131         if o == '-d': ddir = a
0132         if o == '-f': force = 1
0133         if o == '-q': quiet = 1
0134         if o == '-x':
0135             import re
0136             rx = re.compile(a)
0137     if ddir:
0138         if len(args) != 1:
0139             print "-d destdir require exactly one directory argument"
0140             sys.exit(2)
0141     success = 1
0142     try:
0143         if args:
0144             for dir in args:
0145                 if not compile_dir(dir, maxlevels, ddir,
0146                                    force, rx, quiet):
0147                     success = 0
0148         else:
0149             success = compile_path()
0150     except KeyboardInterrupt:
0151         print "\n[interrupt]"
0152         success = 0
0153     return success
0154 
0155 if __name__ == '__main__':
0156     exit_status = not main()
0157     sys.exit(exit_status)
0158 

Generated by PyXR 0.9.4
SourceForge.net Logo