home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pytho152.zip / emx / lib / python1.5 / compileall.py < prev    next >
Text File  |  2000-08-10  |  4KB  |  129 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. def compile_dir(dir, maxlevels=10, ddir=None, force=0):
  21.     """Byte-compile all modules in the given directory tree.
  22.  
  23.     Arguments (only dir is required):
  24.  
  25.     dir:       the directory to byte-compile
  26.     maxlevels: maximum recursion level (default 10)
  27.     ddir:      if given, purported directory name (this is the
  28.                directory name that will show up in error messages)
  29.     force:     if 1, force compilation, even if timestamps are up-to-date
  30.  
  31.     """
  32.     print 'Listing', dir, '...'
  33.     try:
  34.         names = os.listdir(dir)
  35.     except os.error:
  36.         print "Can't list", dir
  37.         names = []
  38.     names.sort()
  39.     success = 1
  40.     for name in names:
  41.         fullname = os.path.join(dir, name)
  42.         if ddir:
  43.             dfile = os.path.join(ddir, name)
  44.         else:
  45.             dfile = None
  46.         if os.path.isfile(fullname):
  47.             head, tail = name[:-3], name[-3:]
  48.             if tail == '.py':
  49.                 cfile = fullname + (__debug__ and 'c' or 'o')
  50.                 ftime = os.stat(fullname)[stat.ST_MTIME]
  51.                 try: ctime = os.stat(cfile)[stat.ST_MTIME]
  52.                 except os.error: ctime = 0
  53.                 if (ctime > ftime) and not force: continue
  54.                 print 'Compiling', fullname, '...'
  55.                 try:
  56.                     py_compile.compile(fullname, None, dfile)
  57.                 except KeyboardInterrupt:
  58.                     raise KeyboardInterrupt
  59.                 except:
  60.                     if type(sys.exc_type) == type(''):
  61.                         exc_type_name = sys.exc_type
  62.                     else: exc_type_name = sys.exc_type.__name__
  63.                     print 'Sorry:', exc_type_name + ':',
  64.                     print sys.exc_value
  65.                     success = 0
  66.         elif maxlevels > 0 and \
  67.              name != os.curdir and name != os.pardir and \
  68.              os.path.isdir(fullname) and \
  69.              not os.path.islink(fullname):
  70.             compile_dir(fullname, maxlevels - 1, dfile, force)
  71.     return success
  72.  
  73. def compile_path(skip_curdir=1, maxlevels=0, force=0):
  74.     """Byte-compile all module on sys.path.
  75.  
  76.     Arguments (all optional):
  77.  
  78.     skip_curdir: if true, skip current directory (default true)
  79.     maxlevels:   max recursion level (default 0)
  80.     force: as for compile_dir() (default 0)
  81.  
  82.     """
  83.     success = 1
  84.     for dir in sys.path:
  85.         if (not dir or dir == os.curdir) and skip_curdir:
  86.             print 'Skipping current directory'
  87.         else:
  88.             success = success and compile_dir(dir, maxlevels, None, force)
  89.     return success
  90.  
  91. def main():
  92.     """Script main program."""
  93.     import getopt
  94.     try:
  95.         opts, args = getopt.getopt(sys.argv[1:], 'lfd:')
  96.     except getopt.error, msg:
  97.         print msg
  98.         print "usage: compileall [-l] [-f] [-d destdir] [directory ...]"
  99.         print "-l: don't recurse down"
  100.         print "-f: force rebuild even if timestamps are up-to-date"
  101.         print "-d destdir: purported directory name for error messages"
  102.         print "if no directory arguments, -l sys.path is assumed"
  103.         sys.exit(2)
  104.     maxlevels = 10
  105.     ddir = None
  106.     force = 0
  107.     for o, a in opts:
  108.         if o == '-l': maxlevels = 0
  109.         if o == '-d': ddir = a
  110.         if o == '-f': force = 1
  111.     if ddir:
  112.         if len(args) != 1:
  113.             print "-d destdir require exactly one directory argument"
  114.             sys.exit(2)
  115.     success = 1
  116.     try:
  117.         if args:
  118.             for dir in args:
  119.                 success = success and compile_dir(dir, maxlevels, ddir, force)
  120.         else:
  121.             success = compile_path()
  122.     except KeyboardInterrupt:
  123.         print "\n[interrupt]"
  124.         success = 0
  125.     return success
  126.  
  127. if __name__ == '__main__':
  128.     sys.exit(not main())
  129.