home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / TemaCD / webclean / !!!python!!! / BeOpen-Python-2.0.exe / BUILD_SCRIPTS.PY < prev    next >
Encoding:
Python Source  |  2000-09-30  |  3.3 KB  |  101 lines

  1. """distutils.command.build_scripts
  2.  
  3. Implements the Distutils 'build_scripts' command."""
  4.  
  5. # created 2000/05/23, Bastian Kleineidam
  6.  
  7. __revision__ = "$Id: build_scripts.py,v 1.6 2000/09/30 18:27:54 gward Exp $"
  8.  
  9. import sys, os, re
  10. from distutils.core import Command
  11. from distutils.dep_util import newer
  12.  
  13. # check if Python is called on the first line with this expression
  14. first_line_re = re.compile(r'^#!.*python(\s+.*)?')
  15.  
  16. class build_scripts (Command):
  17.  
  18.     description = "\"build\" scripts (copy and fixup #! line)"
  19.  
  20.     user_options = [
  21.         ('build-dir=', 'd', "directory to \"build\" (copy) to"),
  22.         ('force', 'f', "forcibly build everything (ignore file timestamps"),
  23.         ]
  24.  
  25.     boolean_options = ['force']
  26.  
  27.  
  28.     def initialize_options (self):
  29.         self.build_dir = None
  30.         self.scripts = None
  31.         self.force = None
  32.         self.outfiles = None
  33.  
  34.     def finalize_options (self):
  35.         self.set_undefined_options('build',
  36.                                    ('build_scripts', 'build_dir'),
  37.                                    ('force', 'force'))
  38.         self.scripts = self.distribution.scripts
  39.  
  40.  
  41.     def run (self):
  42.         if not self.scripts:
  43.             return
  44.         self.copy_scripts()
  45.  
  46.         
  47.     def copy_scripts (self):
  48.         """Copy each script listed in 'self.scripts'; if it's marked as a
  49.         Python script in the Unix way (first line matches 'first_line_re',
  50.         ie. starts with "\#!" and contains "python"), then adjust the first
  51.         line to refer to the current Python interpreter as we copy.
  52.         """
  53.         outfiles = []
  54.         self.mkpath(self.build_dir)
  55.         for script in self.scripts:
  56.             adjust = 0
  57.             outfile = os.path.join(self.build_dir, os.path.basename(script))
  58.  
  59.             if not self.force and not newer(script, outfile):
  60.                 self.announce("not copying %s (output up-to-date)" % script)
  61.                 continue
  62.  
  63.             # Always open the file, but ignore failures in dry-run mode --
  64.             # that way, we'll get accurate feedback if we can read the
  65.             # script.
  66.             try:
  67.                 f = open(script, "r")
  68.             except IOError:
  69.                 if not self.dry_run:
  70.                     raise
  71.                 f = None
  72.             else:
  73.                 first_line = f.readline()
  74.                 if not first_line:
  75.                     self.warn("%s is an empty file (skipping)" % script)
  76.                     continue
  77.  
  78.                 match = first_line_re.match(first_line)
  79.                 if match:
  80.                     adjust = 1
  81.                     post_interp = match.group(1)
  82.  
  83.             if adjust:
  84.                 self.announce("copying and adjusting %s -> %s" %
  85.                               (script, self.build_dir))
  86.                 if not self.dry_run:
  87.                     outf = open(outfile, "w")
  88.                     outf.write("#!%s%s\n" % 
  89.                                (os.path.normpath(sys.executable), post_interp))
  90.                     outf.writelines(f.readlines())
  91.                     outf.close()
  92.                 if f:
  93.                     f.close()
  94.             else:
  95.                 f.close()
  96.                 self.copy_file(script, outfile)
  97.  
  98.     # copy_scripts ()
  99.  
  100. # class build_scripts
  101.