home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / bin / py_compilefiles < prev    next >
Encoding:
Text File  |  2006-08-15  |  3.3 KB  |  108 lines

  1. #! /usr/bin/python
  2.  
  3. import os
  4. import sys
  5. import py_compile
  6.  
  7. def compile_files(files, ddir=None, force=0, rx=None, quiet=0):
  8.     """Byte-compile all file.
  9.     file:      the file to byte-compile
  10.     ddir:      if given, purported directory name (this is the
  11.                directory name that will show up in error messages)
  12.     force:     if 1, force compilation, even if timestamps are up-to-date
  13.     quiet:     if 1, be quiet during compilation
  14.  
  15.     """
  16.  
  17.     success = 1
  18.     dfile = None
  19.     for fullname in files:
  20.         if rx is not None:
  21.             mo = rx.search(fullname)
  22.             if mo:
  23.                 continue
  24.         if os.path.isdir(fullname):
  25.             continue
  26.         elif not os.path.isfile(fullname):
  27.             print "file does not exist:", fullname
  28.             success = 0
  29.         elif fullname[-3:] == '.py':
  30.             cfile = fullname + (__debug__ and 'c' or 'o')
  31.             ftime = os.stat(fullname).st_mtime
  32.             try: ctime = os.stat(cfile).st_mtime
  33.             except os.error: ctime = 0
  34.             if (ctime > ftime) and not force: continue
  35.             if not quiet:
  36.                 print 'Compiling', fullname, '...'
  37.             try:
  38.                 ok = py_compile.compile(fullname, None, dfile, True)
  39.             except KeyboardInterrupt:
  40.                 raise KeyboardInterrupt
  41.             except py_compile.PyCompileError,err:
  42.                 if quiet:
  43.                     print 'Compiling', fullname, '...'
  44.                 print err.msg
  45.                 success = 0
  46.             except IOError, e:
  47.                 print "Sorry", e
  48.                 success = 0
  49.             else:
  50.                 if ok == 0:
  51.                     success = 0
  52.     return success
  53.  
  54. def main():
  55.     """Script main program."""
  56.     import getopt
  57.     try:
  58.         opts, args = getopt.getopt(sys.argv[1:], 'lfqd:x:')
  59.     except getopt.error, msg:
  60.         print msg
  61.         print "usage: python compilefiles.py [-s] [-f] [-q] " \
  62.               "[-x regexp] [file ...] [-]"
  63.         print "-f: force rebuild even if timestamps are up-to-date"
  64.         print "-q: quiet operation"
  65.         print "-x regexp: skip files matching the regular expression regexp"
  66.         print "   the regexp is search for in the full path of the file"
  67.         sys.exit(2)
  68.     ddir = None
  69.     force = 0
  70.     quiet = 0
  71.     rx = None
  72.     for o, a in opts:
  73.         if o == '-d': ddir = a
  74.         if o == '-f': force = 1
  75.         if o == '-q': quiet = 1
  76.         if o == '-x':
  77.             import re
  78.             rx = re.compile(a)
  79.     if ddir:
  80.         if len(args) != 1:
  81.             print "-d destdir require exactly one directory argument"
  82.             sys.exit(2)
  83.     success = 1
  84.  
  85.     try:
  86.         files = []
  87.         for arg in args:
  88.             if arg == '-':
  89.                 while 1:
  90.                     line = sys.stdin.readline()
  91.                     if not line:
  92.                         break
  93.                     files.append(line[:-1])
  94.             else:
  95.                 files.append(arg)
  96.         if not files:
  97.             print "py_compilefiles: no files to compile"
  98.         elif not compile_files(files, ddir, force, rx, quiet):
  99.             success = 0
  100.     except KeyboardInterrupt:
  101.         print "\n[interrupt]"
  102.         success = 0
  103.     return success
  104.  
  105. if __name__ == '__main__':
  106.     exit_status = int(not main())
  107.     sys.exit(exit_status)
  108.