home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_Lib_compileall.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  4.4 KB  |  131 lines

  1. """Module/script to "compile" all .py files to .pyc (or .pyo) file.
  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. """
  14.  
  15. import os
  16. import stat
  17. import sys
  18. import py_compile
  19.  
  20. __all__ = ["compile_dir","compile_path"]
  21.  
  22. def compile_dir(dir, maxlevels=10, ddir=None, force=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:      if given, purported directory name (this is the
  30.                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.     print 'Listing', dir, '...'
  35.     try:
  36.         names = os.listdir(dir)
  37.     except os.error:
  38.         print "Can't list", dir
  39.         names = []
  40.     names.sort()
  41.     success = 1
  42.     for name in names:
  43.         fullname = os.path.join(dir, name)
  44.         if ddir:
  45.             dfile = os.path.join(ddir, name)
  46.         else:
  47.             dfile = None
  48.         if os.path.isfile(fullname):
  49.             head, tail = name[:-3], name[-3:]
  50.             if tail == '.py':
  51.                 cfile = fullname + (__debug__ and 'c' or 'o')
  52.                 ftime = os.stat(fullname)[stat.ST_MTIME]
  53.                 try: ctime = os.stat(cfile)[stat.ST_MTIME]
  54.                 except os.error: ctime = 0
  55.                 if (ctime > ftime) and not force: continue
  56.                 print 'Compiling', fullname, '...'
  57.                 try:
  58.                     py_compile.compile(fullname, None, dfile)
  59.                 except KeyboardInterrupt:
  60.                     raise KeyboardInterrupt
  61.                 except:
  62.                     if type(sys.exc_type) == type(''):
  63.                         exc_type_name = sys.exc_type
  64.                     else: exc_type_name = sys.exc_type.__name__
  65.                     print 'Sorry:', exc_type_name + ':',
  66.                     print sys.exc_value
  67.                     success = 0
  68.         elif maxlevels > 0 and \
  69.              name != os.curdir and name != os.pardir and \
  70.              os.path.isdir(fullname) and \
  71.              not os.path.islink(fullname):
  72.             compile_dir(fullname, maxlevels - 1, dfile, force)
  73.     return success
  74.  
  75. def compile_path(skip_curdir=1, maxlevels=0, force=0):
  76.     """Byte-compile all module on sys.path.
  77.  
  78.     Arguments (all optional):
  79.  
  80.     skip_curdir: if true, skip current directory (default true)
  81.     maxlevels:   max recursion level (default 0)
  82.     force: as for compile_dir() (default 0)
  83.  
  84.     """
  85.     success = 1
  86.     for dir in sys.path:
  87.         if (not dir or dir == os.curdir) and skip_curdir:
  88.             print 'Skipping current directory'
  89.         else:
  90.             success = success and compile_dir(dir, maxlevels, None, force)
  91.     return success
  92.  
  93. def main():
  94.     """Script main program."""
  95.     import getopt
  96.     try:
  97.         opts, args = getopt.getopt(sys.argv[1:], 'lfd:')
  98.     except getopt.error, msg:
  99.         print msg
  100.         print "usage: compileall [-l] [-f] [-d destdir] [directory ...]"
  101.         print "-l: don't recurse down"
  102.         print "-f: force rebuild even if timestamps are up-to-date"
  103.         print "-d destdir: purported directory name for error messages"
  104.         print "if no directory arguments, -l sys.path is assumed"
  105.         sys.exit(2)
  106.     maxlevels = 10
  107.     ddir = None
  108.     force = 0
  109.     for o, a in opts:
  110.         if o == '-l': maxlevels = 0
  111.         if o == '-d': ddir = a
  112.         if o == '-f': force = 1
  113.     if ddir:
  114.         if len(args) != 1:
  115.             print "-d destdir require exactly one directory argument"
  116.             sys.exit(2)
  117.     success = 1
  118.     try:
  119.         if args:
  120.             for dir in args:
  121.                 success = success and compile_dir(dir, maxlevels, ddir, force)
  122.         else:
  123.             success = compile_path()
  124.     except KeyboardInterrupt:
  125.         print "\n[interrupt]"
  126.         success = 0
  127.     return success
  128.  
  129. if __name__ == '__main__':
  130.     sys.exit(not main())
  131.