home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 December / CHIP_CD_2004-12.iso / bonus / oo / OOo_1.1.3_ru_RU_infra_WinIntel_install.exe / $PLUGINSDIR / f_0372 / python-core-2.2.2 / lib / compileall.py < prev    next >
Text File  |  2004-10-09  |  5KB  |  149 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, rx=None):
  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 rx:
  49.             mo = rx.search(fullname)
  50.             if mo:
  51.                 continue
  52.         if os.path.isfile(fullname):
  53.             head, tail = name[:-3], name[-3:]
  54.             if tail == '.py':
  55.                 cfile = fullname + (__debug__ and 'c' or 'o')
  56.                 ftime = os.stat(fullname)[stat.ST_MTIME]
  57.                 try: ctime = os.stat(cfile)[stat.ST_MTIME]
  58.                 except os.error: ctime = 0
  59.                 if (ctime > ftime) and not force: continue
  60.                 print 'Compiling', fullname, '...'
  61.                 try:
  62.                     ok = py_compile.compile(fullname, None, dfile)
  63.                 except KeyboardInterrupt:
  64.                     raise KeyboardInterrupt
  65.                 except:
  66.                     # XXX py_compile catches SyntaxErrors
  67.                     if type(sys.exc_type) == type(''):
  68.                         exc_type_name = sys.exc_type
  69.                     else: exc_type_name = sys.exc_type.__name__
  70.                     print 'Sorry:', exc_type_name + ':',
  71.                     print sys.exc_value
  72.                     success = 0
  73.                 else:
  74.                     if ok == 0:
  75.                         success = 0
  76.         elif maxlevels > 0 and \
  77.              name != os.curdir and name != os.pardir and \
  78.              os.path.isdir(fullname) and \
  79.              not os.path.islink(fullname):
  80.             if not compile_dir(fullname, maxlevels - 1, dfile, force, rx):
  81.                 success = 0
  82.     return success
  83.  
  84. def compile_path(skip_curdir=1, maxlevels=0, force=0):
  85.     """Byte-compile all module on sys.path.
  86.  
  87.     Arguments (all optional):
  88.  
  89.     skip_curdir: if true, skip current directory (default true)
  90.     maxlevels:   max recursion level (default 0)
  91.     force: as for compile_dir() (default 0)
  92.  
  93.     """
  94.     success = 1
  95.     for dir in sys.path:
  96.         if (not dir or dir == os.curdir) and skip_curdir:
  97.             print 'Skipping current directory'
  98.         else:
  99.             success = success and compile_dir(dir, maxlevels, None, force)
  100.     return success
  101.  
  102. def main():
  103.     """Script main program."""
  104.     import getopt
  105.     try:
  106.         opts, args = getopt.getopt(sys.argv[1:], 'lfd:x:')
  107.     except getopt.error, msg:
  108.         print msg
  109.         print "usage: python compileall.py [-l] [-f] [-d destdir] " \
  110.               "[-s regexp] [directory ...]"
  111.         print "-l: don't recurse down"
  112.         print "-f: force rebuild even if timestamps are up-to-date"
  113.         print "-d destdir: purported directory name for error messages"
  114.         print "   if no directory arguments, -l sys.path is assumed"
  115.         print "-x regexp: skip files matching the regular expression regexp"
  116.         print "   the regexp is search for in the full path of the file"
  117.         sys.exit(2)
  118.     maxlevels = 10
  119.     ddir = None
  120.     force = 0
  121.     rx = None
  122.     for o, a in opts:
  123.         if o == '-l': maxlevels = 0
  124.         if o == '-d': ddir = a
  125.         if o == '-f': force = 1
  126.         if o == '-x':
  127.             import re
  128.             rx = re.compile(a)
  129.     if ddir:
  130.         if len(args) != 1:
  131.             print "-d destdir require exactly one directory argument"
  132.             sys.exit(2)
  133.     success = 1
  134.     try:
  135.         if args:
  136.             for dir in args:
  137.                 if not compile_dir(dir, maxlevels, ddir, force, rx):
  138.                     success = 0
  139.         else:
  140.             success = compile_path()
  141.     except KeyboardInterrupt:
  142.         print "\n[interrupt]"
  143.         success = 0
  144.     return success
  145.  
  146. if __name__ == '__main__':
  147.     exit_status = not main()
  148.     sys.exit(exit_status)
  149.