home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / distutils / command / build_scripts.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  4.5 KB  |  123 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """distutils.command.build_scripts
  5.  
  6. Implements the Distutils 'build_scripts' command."""
  7. __revision__ = '$Id: build_scripts.py 69599 2009-02-13 23:02:44Z tarek.ziade $'
  8. import os
  9. import re
  10. from stat import ST_MODE
  11. from distutils import sysconfig
  12. from distutils.core import Command
  13. from distutils.dep_util import newer
  14. from distutils.util import convert_path
  15. from distutils import log
  16. first_line_re = re.compile('^#!.*python[0-9.]*([ \t].*)?$')
  17.  
  18. class build_scripts(Command):
  19.     description = '"build" scripts (copy and fixup #! line)'
  20.     user_options = [
  21.         ('build-dir=', 'd', 'directory to "build" (copy) to'),
  22.         ('force', 'f', 'forcibly build everything (ignore file timestamps'),
  23.         ('executable=', 'e', 'specify final destination interpreter path')]
  24.     boolean_options = [
  25.         'force']
  26.     
  27.     def initialize_options(self):
  28.         self.build_dir = None
  29.         self.scripts = None
  30.         self.force = None
  31.         self.executable = None
  32.         self.outfiles = None
  33.  
  34.     
  35.     def finalize_options(self):
  36.         self.set_undefined_options('build', ('build_scripts', 'build_dir'), ('force', 'force'), ('executable', 'executable'))
  37.         self.scripts = self.distribution.scripts
  38.  
  39.     
  40.     def get_source_files(self):
  41.         return self.scripts
  42.  
  43.     
  44.     def run(self):
  45.         if not self.scripts:
  46.             return None
  47.         self.copy_scripts()
  48.  
  49.     
  50.     def copy_scripts(self):
  51.         '''Copy each script listed in \'self.scripts\'; if it\'s marked as a
  52.         Python script in the Unix way (first line matches \'first_line_re\',
  53.         ie. starts with "\\#!" and contains "python"), then adjust the first
  54.         line to refer to the current Python interpreter as we copy.
  55.         '''
  56.         self.mkpath(self.build_dir)
  57.         outfiles = []
  58.         for script in self.scripts:
  59.             adjust = 0
  60.             script = convert_path(script)
  61.             outfile = os.path.join(self.build_dir, os.path.basename(script))
  62.             outfiles.append(outfile)
  63.             if not (self.force) and not newer(script, outfile):
  64.                 log.debug('not copying %s (up-to-date)', script)
  65.                 continue
  66.             
  67.             
  68.             try:
  69.                 f = open(script, 'r')
  70.             except IOError:
  71.                 if not self.dry_run:
  72.                     raise 
  73.                 self.dry_run
  74.                 f = None
  75.  
  76.             first_line = f.readline()
  77.             if not first_line:
  78.                 self.warn('%s is an empty file (skipping)' % script)
  79.                 continue
  80.             
  81.             match = first_line_re.match(first_line)
  82.             if match:
  83.                 adjust = 1
  84.                 if not match.group(1):
  85.                     pass
  86.                 post_interp = ''
  87.             
  88.             if adjust:
  89.                 log.info('copying and adjusting %s -> %s', script, self.build_dir)
  90.                 if not self.dry_run:
  91.                     outf = open(outfile, 'w')
  92.                     if not sysconfig.python_build:
  93.                         outf.write('#!%s%s\n' % (self.executable, post_interp))
  94.                     else:
  95.                         outf.write('#!%s%s\n' % (os.path.join(sysconfig.get_config_var('BINDIR'), 'python%s%s' % (sysconfig.get_config_var('VERSION'), sysconfig.get_config_var('EXE'))), post_interp))
  96.                     outf.writelines(f.readlines())
  97.                     outf.close()
  98.                 
  99.                 if f:
  100.                     f.close()
  101.                 
  102.             f
  103.             if f:
  104.                 f.close()
  105.             
  106.             self.copy_file(script, outfile)
  107.         
  108.         if os.name == 'posix':
  109.             for file in outfiles:
  110.                 if self.dry_run:
  111.                     log.info('changing mode of %s', file)
  112.                     continue
  113.                 oldmode = os.stat(file)[ST_MODE] & 4095
  114.                 newmode = (oldmode | 365) & 4095
  115.                 if newmode != oldmode:
  116.                     log.info('changing mode of %s from %o to %o', file, oldmode, newmode)
  117.                     os.chmod(file, newmode)
  118.                     continue
  119.             
  120.         
  121.  
  122.  
  123.