NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

build_scripts.py (3656B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\stdlib\distutils\command\build_scripts.py
      2 __revision__ = '$Id: build_scripts.py 82110 2010-06-20 13:38:51Z kristjan.jonsson $'
      3 import os, re
      4 from stat import ST_MODE
      5 from distutils.core import Command
      6 from distutils.dep_util import newer
      7 from distutils.util import convert_path
      8 from distutils import log
      9 first_line_re = re.compile('^#!.*python[0-9.]*([ \t].*)?$')
     10 
     11 class build_scripts(Command):
     12     description = '"build" scripts (copy and fixup #! line)'
     13     user_options = [('build-dir=', 'd', 'directory to "build" (copy) to'), ('force', 'f', 'forcibly build everything (ignore file timestamps'), ('executable=', 'e', 'specify final destination interpreter path')]
     14     boolean_options = ['force']
     15 
     16     def initialize_options(self):
     17         self.build_dir = None
     18         self.scripts = None
     19         self.force = None
     20         self.executable = None
     21         self.outfiles = None
     22 
     23     def finalize_options(self):
     24         self.set_undefined_options('build', ('build_scripts', 'build_dir'), ('force', 'force'), ('executable', 'executable'))
     25         self.scripts = self.distribution.scripts
     26 
     27     def get_source_files(self):
     28         return self.scripts
     29 
     30     def run(self):
     31         if not self.scripts:
     32             return
     33         self.copy_scripts()
     34 
     35     def copy_scripts(self):
     36         _sysconfig = __import__('sysconfig')
     37         self.mkpath(self.build_dir)
     38         outfiles = []
     39         for script in self.scripts:
     40             adjust = 0
     41             script = convert_path(script)
     42             outfile = os.path.join(self.build_dir, os.path.basename(script))
     43             outfiles.append(outfile)
     44             if not self.force and not newer(script, outfile):
     45                 log.debug('not copying %s (up-to-date)', script)
     46                 continue
     47             try:
     48                 f = open(script, 'r')
     49             except IOError:
     50                 if not self.dry_run:
     51                     raise
     52                 f = None
     53             else:
     54                 first_line = f.readline()
     55                 if not first_line:
     56                     self.warn('%s is an empty file (skipping)' % script)
     57                     continue
     58                 match = first_line_re.match(first_line)
     59                 if match:
     60                     adjust = 1
     61                     post_interp = match.group(1) or ''
     62 
     63             if adjust:
     64                 log.info('copying and adjusting %s -> %s', script, self.build_dir)
     65                 if not self.dry_run:
     66                     outf = open(outfile, 'w')
     67                     if not _sysconfig.is_python_build():
     68                         outf.write('#!%s%s\n' % (self.executable, post_interp))
     69                     else:
     70                         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))
     71                     outf.writelines(f.readlines())
     72                     outf.close()
     73                 if f:
     74                     f.close()
     75             else:
     76                 if f:
     77                     f.close()
     78                 self.copy_file(script, outfile)
     79 
     80         if os.name == 'posix':
     81             for file in outfiles:
     82                 if self.dry_run:
     83                     log.info('changing mode of %s', file)
     84                 else:
     85                     oldmode = os.stat(file)[ST_MODE] & 4095
     86                     newmode = (oldmode | 365) & 4095
     87                     if newmode != oldmode:
     88                         log.info('changing mode of %s from %o to %o', file, oldmode, newmode)
     89                         os.chmod(file, newmode)