home *** CD-ROM | disk | FTP | other *** search
/ Freelog 33 / Freelog033.iso / Progr / Python-2.2.1.exe / BUILD_SCRIPTS.PY < prev    next >
Encoding:
Python Source  |  2001-12-11  |  3.8 KB  |  111 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.14 2001/12/11 20:44:42 lemburg Exp $"
  8.  
  9. import sys, os, re
  10. from distutils import sysconfig
  11. from distutils.core import Command
  12. from distutils.dep_util import newer
  13. from distutils.util import convert_path
  14.  
  15. # check if Python is called on the first line with this expression
  16. first_line_re = re.compile(r'^#!.*python(\s+.*)?$')
  17.  
  18. class build_scripts (Command):
  19.  
  20.     description = "\"build\" scripts (copy and fixup #! line)"
  21.  
  22.     user_options = [
  23.         ('build-dir=', 'd', "directory to \"build\" (copy) to"),
  24.         ('force', 'f', "forcibly build everything (ignore file timestamps"),
  25.         ]
  26.  
  27.     boolean_options = ['force']
  28.  
  29.  
  30.     def initialize_options (self):
  31.         self.build_dir = None
  32.         self.scripts = None
  33.         self.force = None
  34.         self.outfiles = None
  35.  
  36.     def finalize_options (self):
  37.         self.set_undefined_options('build',
  38.                                    ('build_scripts', 'build_dir'),
  39.                                    ('force', 'force'))
  40.         self.scripts = self.distribution.scripts
  41.  
  42.  
  43.     def run (self):
  44.         if not self.scripts:
  45.             return
  46.         self.copy_scripts()
  47.  
  48.  
  49.     def copy_scripts (self):
  50.         """Copy each script listed in 'self.scripts'; if it's marked as a
  51.         Python script in the Unix way (first line matches 'first_line_re',
  52.         ie. starts with "\#!" and contains "python"), then adjust the first
  53.         line to refer to the current Python interpreter as we copy.
  54.         """
  55.         self.mkpath(self.build_dir)
  56.         for script in self.scripts:
  57.             adjust = 0
  58.             script = convert_path(script)
  59.             outfile = os.path.join(self.build_dir, os.path.basename(script))
  60.  
  61.             if not self.force and not newer(script, outfile):
  62.                 self.announce("not copying %s (up-to-date)" % script)
  63.                 continue
  64.  
  65.             # Always open the file, but ignore failures in dry-run mode --
  66.             # that way, we'll get accurate feedback if we can read the
  67.             # script.
  68.             try:
  69.                 f = open(script, "r")
  70.             except IOError:
  71.                 if not self.dry_run:
  72.                     raise
  73.                 f = None
  74.             else:
  75.                 first_line = f.readline()
  76.                 if not first_line:
  77.                     self.warn("%s is an empty file (skipping)" % script)
  78.                     continue
  79.  
  80.                 match = first_line_re.match(first_line)
  81.                 if match:
  82.                     adjust = 1
  83.                     post_interp = match.group(1) or ''
  84.  
  85.             if adjust:
  86.                 self.announce("copying and adjusting %s -> %s" %
  87.                               (script, self.build_dir))
  88.                 if not self.dry_run:
  89.                     outf = open(outfile, "w")
  90.                     if not sysconfig.python_build:
  91.                         outf.write("#!%s%s\n" % 
  92.                                    (os.path.normpath(sys.executable),
  93.                                     post_interp))
  94.                     else:
  95.                         outf.write("#!%s%s" %
  96.                                    (os.path.join(
  97.                             sysconfig.get_config_var("BINDIR"),
  98.                             "python" + sysconfig.get_config_var("EXE")),
  99.                                     post_interp))
  100.                     outf.writelines(f.readlines())
  101.                     outf.close()
  102.                 if f:
  103.                     f.close()
  104.             else:
  105.                 f.close()
  106.                 self.copy_file(script, outfile)
  107.  
  108.     # copy_scripts ()
  109.  
  110. # class build_scripts
  111.