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

install.py (16500B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\stdlib\distutils\command\install.py
      2 from distutils import log
      3 __revision__ = '$Id: install.py 82110 2010-06-20 13:38:51Z kristjan.jonsson $'
      4 import sys, os, string
      5 from types import *
      6 from distutils.core import Command
      7 from distutils.debug import DEBUG
      8 from distutils.sysconfig import get_config_vars
      9 from distutils.errors import DistutilsPlatformError
     10 from distutils.file_util import write_file
     11 from distutils.util import convert_path, subst_vars, change_root
     12 from distutils.util import get_platform
     13 from distutils.errors import DistutilsOptionError
     14 from site import USER_BASE
     15 from site import USER_SITE
     16 if sys.version < '2.2':
     17     WINDOWS_SCHEME = {'purelib': '$base',
     18      'platlib': '$base',
     19      'headers': '$base/Include/$dist_name',
     20      'scripts': '$base/Scripts',
     21      'data': '$base'}
     22 else:
     23     WINDOWS_SCHEME = {'purelib': '$base/Lib/site-packages',
     24      'platlib': '$base/Lib/site-packages',
     25      'headers': '$base/Include/$dist_name',
     26      'scripts': '$base/Scripts',
     27      'data': '$base'}
     28 INSTALL_SCHEMES = {'unix_prefix': {'purelib': '$base/lib/python$py_version_short/site-packages',
     29                  'platlib': '$platbase/lib/python$py_version_short/site-packages',
     30                  'headers': '$base/include/python$py_version_short/$dist_name',
     31                  'scripts': '$base/bin',
     32                  'data': '$base'},
     33  'unix_home': {'purelib': '$base/lib/python',
     34                'platlib': '$base/lib/python',
     35                'headers': '$base/include/python/$dist_name',
     36                'scripts': '$base/bin',
     37                'data': '$base'},
     38  'unix_user': {'purelib': '$usersite',
     39                'platlib': '$usersite',
     40                'headers': '$userbase/include/python$py_version_short/$dist_name',
     41                'scripts': '$userbase/bin',
     42                'data': '$userbase'},
     43  'nt': WINDOWS_SCHEME,
     44  'nt_user': {'purelib': '$usersite',
     45              'platlib': '$usersite',
     46              'headers': '$userbase/Python$py_version_nodot/Include/$dist_name',
     47              'scripts': '$userbase/Scripts',
     48              'data': '$userbase'},
     49  'os2': {'purelib': '$base/Lib/site-packages',
     50          'platlib': '$base/Lib/site-packages',
     51          'headers': '$base/Include/$dist_name',
     52          'scripts': '$base/Scripts',
     53          'data': '$base'},
     54  'os2_home': {'purelib': '$usersite',
     55               'platlib': '$usersite',
     56               'headers': '$userbase/include/python$py_version_short/$dist_name',
     57               'scripts': '$userbase/bin',
     58               'data': '$userbase'}}
     59 SCHEME_KEYS = ('purelib', 'platlib', 'headers', 'scripts', 'data')
     60 
     61 class install(Command):
     62     description = 'install everything from build directory'
     63     user_options = [('prefix=', None, 'installation prefix'),
     64      ('exec-prefix=', None, '(Unix only) prefix for platform-specific files'),
     65      ('home=', None, '(Unix only) home directory to install under'),
     66      ('user', None, "install in user site-package '%s'" % USER_SITE),
     67      ('install-base=', None, 'base installation directory (instead of --prefix or --home)'),
     68      ('install-platbase=', None, 'base installation directory for platform-specific files ' + '(instead of --exec-prefix or --home)'),
     69      ('root=', None, 'install everything relative to this alternate root directory'),
     70      ('install-purelib=', None, 'installation directory for pure Python module distributions'),
     71      ('install-platlib=', None, 'installation directory for non-pure module distributions'),
     72      ('install-lib=', None, 'installation directory for all module distributions ' + '(overrides --install-purelib and --install-platlib)'),
     73      ('install-headers=', None, 'installation directory for C/C++ headers'),
     74      ('install-scripts=', None, 'installation directory for Python scripts'),
     75      ('install-data=', None, 'installation directory for data files'),
     76      ('compile', 'c', 'compile .py to .pyc [default]'),
     77      ('no-compile', None, "don't compile .py files"),
     78      ('optimize=', 'O', 'also compile with optimization: -O1 for "python -O", -O2 for "python -OO", and -O0 to disable [default: -O0]'),
     79      ('force', 'f', 'force installation (overwrite any existing files)'),
     80      ('skip-build', None, 'skip rebuilding everything (for testing/debugging)'),
     81      ('record=', None, 'filename in which to record list of installed files')]
     82     boolean_options = ['compile',
     83      'force',
     84      'skip-build',
     85      'user']
     86     negative_opt = {'no-compile': 'compile'}
     87 
     88     def initialize_options(self):
     89         self.prefix = None
     90         self.exec_prefix = None
     91         self.home = None
     92         self.user = 0
     93         self.install_base = None
     94         self.install_platbase = None
     95         self.root = None
     96         self.install_purelib = None
     97         self.install_platlib = None
     98         self.install_headers = None
     99         self.install_lib = None
    100         self.install_scripts = None
    101         self.install_data = None
    102         self.install_userbase = USER_BASE
    103         self.install_usersite = USER_SITE
    104         self.compile = None
    105         self.optimize = None
    106         self.extra_path = None
    107         self.install_path_file = 1
    108         self.force = 0
    109         self.skip_build = 0
    110         self.warn_dir = 1
    111         self.build_base = None
    112         self.build_lib = None
    113         self.record = None
    114 
    115     def finalize_options(self):
    116         if (self.prefix or self.exec_prefix or self.home) and (self.install_base or self.install_platbase):
    117             raise DistutilsOptionError, 'must supply either prefix/exec-prefix/home or ' + 'install-base/install-platbase -- not both'
    118         if self.home and (self.prefix or self.exec_prefix):
    119             raise DistutilsOptionError, 'must supply either home or prefix/exec-prefix -- not both'
    120         if self.user and (self.prefix or self.exec_prefix or self.home or self.install_base or self.install_platbase):
    121             raise DistutilsOptionError("can't combine user with with prefix/exec_prefix/home or install_(plat)base")
    122         if os.name != 'posix':
    123             if self.exec_prefix:
    124                 self.warn('exec-prefix option ignored on this platform')
    125                 self.exec_prefix = None
    126         self.dump_dirs('pre-finalize_{unix,other}')
    127         if os.name == 'posix':
    128             self.finalize_unix()
    129         else:
    130             self.finalize_other()
    131         self.dump_dirs('post-finalize_{unix,other}()')
    132         py_version = string.split(sys.version)[0]
    133         prefix, exec_prefix = get_config_vars('prefix', 'exec_prefix')
    134         self.config_vars = {'dist_name': self.distribution.get_name(),
    135          'dist_version': self.distribution.get_version(),
    136          'dist_fullname': self.distribution.get_fullname(),
    137          'py_version': py_version,
    138          'py_version_short': py_version[0:3],
    139          'py_version_nodot': py_version[0] + py_version[2],
    140          'sys_prefix': prefix,
    141          'prefix': prefix,
    142          'sys_exec_prefix': exec_prefix,
    143          'exec_prefix': exec_prefix,
    144          'userbase': self.install_userbase,
    145          'usersite': self.install_usersite}
    146         self.expand_basedirs()
    147         self.dump_dirs('post-expand_basedirs()')
    148         self.config_vars['base'] = self.install_base
    149         self.config_vars['platbase'] = self.install_platbase
    150         if DEBUG:
    151             from pprint import pprint
    152             print 'config vars:'
    153             pprint(self.config_vars)
    154         self.expand_dirs()
    155         self.dump_dirs('post-expand_dirs()')
    156         if self.user:
    157             self.create_home_path()
    158         if self.install_lib is None:
    159             if self.distribution.ext_modules:
    160                 self.install_lib = self.install_platlib
    161             else:
    162                 self.install_lib = self.install_purelib
    163         self.convert_paths('lib', 'purelib', 'platlib', 'scripts', 'data', 'headers', 'userbase', 'usersite')
    164         self.handle_extra_path()
    165         self.install_libbase = self.install_lib
    166         self.install_lib = os.path.join(self.install_lib, self.extra_dirs)
    167         if self.root is not None:
    168             self.change_roots('libbase', 'lib', 'purelib', 'platlib', 'scripts', 'data', 'headers')
    169         self.dump_dirs('after prepending root')
    170         self.set_undefined_options('build', ('build_base', 'build_base'), ('build_lib', 'build_lib'))
    171 
    172     def dump_dirs(self, msg):
    173         if DEBUG:
    174             from distutils.fancy_getopt import longopt_xlate
    175             print msg + ':'
    176             for opt in self.user_options:
    177                 opt_name = opt[0]
    178                 if opt_name[-1] == '=':
    179                     opt_name = opt_name[0:-1]
    180                 if opt_name in self.negative_opt:
    181                     opt_name = string.translate(self.negative_opt[opt_name], longopt_xlate)
    182                     val = not getattr(self, opt_name)
    183                 else:
    184                     opt_name = string.translate(opt_name, longopt_xlate)
    185                     val = getattr(self, opt_name)
    186                 print '  %s: %s' % (opt_name, val)
    187 
    188     def finalize_unix(self):
    189         if self.install_base is not None or self.install_platbase is not None:
    190             if self.install_lib is None and self.install_purelib is None and self.install_platlib is None or self.install_headers is None or self.install_scripts is None or self.install_data is None:
    191                 raise DistutilsOptionError, 'install-base or install-platbase supplied, but installation scheme is incomplete'
    192             return
    193         if self.user:
    194             if self.install_userbase is None:
    195                 raise DistutilsPlatformError('User base directory is not specified')
    196             self.install_base = self.install_platbase = self.install_userbase
    197             self.select_scheme('unix_user')
    198         elif self.home is not None:
    199             self.install_base = self.install_platbase = self.home
    200             self.select_scheme('unix_home')
    201         else:
    202             if self.prefix is None:
    203                 if self.exec_prefix is not None:
    204                     raise DistutilsOptionError, 'must not supply exec-prefix without prefix'
    205                 self.prefix = os.path.normpath(sys.prefix)
    206                 self.exec_prefix = os.path.normpath(sys.exec_prefix)
    207             elif self.exec_prefix is None:
    208                 self.exec_prefix = self.prefix
    209             self.install_base = self.prefix
    210             self.install_platbase = self.exec_prefix
    211             self.select_scheme('unix_prefix')
    212 
    213     def finalize_other(self):
    214         if self.user:
    215             if self.install_userbase is None:
    216                 raise DistutilsPlatformError('User base directory is not specified')
    217             self.install_base = self.install_platbase = self.install_userbase
    218             self.select_scheme(os.name + '_user')
    219         elif self.home is not None:
    220             self.install_base = self.install_platbase = self.home
    221             self.select_scheme('unix_home')
    222         else:
    223             if self.prefix is None:
    224                 self.prefix = os.path.normpath(sys.prefix)
    225             self.install_base = self.install_platbase = self.prefix
    226             try:
    227                 self.select_scheme(os.name)
    228             except KeyError:
    229                 raise DistutilsPlatformError, "I don't know how to install stuff on '%s'" % os.name
    230 
    231     def select_scheme(self, name):
    232         scheme = INSTALL_SCHEMES[name]
    233         for key in SCHEME_KEYS:
    234             attrname = 'install_' + key
    235             if getattr(self, attrname) is None:
    236                 setattr(self, attrname, scheme[key])
    237 
    238     def _expand_attrs(self, attrs):
    239         for attr in attrs:
    240             val = getattr(self, attr)
    241             if val is not None:
    242                 if os.name == 'posix' or os.name == 'nt':
    243                     val = os.path.expanduser(val)
    244                 val = subst_vars(val, self.config_vars)
    245                 setattr(self, attr, val)
    246 
    247     def expand_basedirs(self):
    248         self._expand_attrs(['install_base', 'install_platbase', 'root'])
    249 
    250     def expand_dirs(self):
    251         self._expand_attrs(['install_purelib',
    252          'install_platlib',
    253          'install_lib',
    254          'install_headers',
    255          'install_scripts',
    256          'install_data'])
    257 
    258     def convert_paths(self, *names):
    259         for name in names:
    260             attr = 'install_' + name
    261             setattr(self, attr, convert_path(getattr(self, attr)))
    262 
    263     def handle_extra_path(self):
    264         if self.extra_path is None:
    265             self.extra_path = self.distribution.extra_path
    266         if self.extra_path is not None:
    267             if type(self.extra_path) is StringType:
    268                 self.extra_path = string.split(self.extra_path, ',')
    269             if len(self.extra_path) == 1:
    270                 path_file = extra_dirs = self.extra_path[0]
    271             elif len(self.extra_path) == 2:
    272                 path_file, extra_dirs = self.extra_path
    273             else:
    274                 raise DistutilsOptionError, "'extra_path' option must be a list, tuple, or comma-separated string with 1 or 2 elements"
    275             extra_dirs = convert_path(extra_dirs)
    276         else:
    277             path_file = None
    278             extra_dirs = ''
    279         self.path_file = path_file
    280         self.extra_dirs = extra_dirs
    281 
    282     def change_roots(self, *names):
    283         for name in names:
    284             attr = 'install_' + name
    285             setattr(self, attr, change_root(self.root, getattr(self, attr)))
    286 
    287     def create_home_path(self):
    288         if not self.user:
    289             return
    290         home = convert_path(os.path.expanduser('~'))
    291         for name, path in self.config_vars.iteritems():
    292             if path.startswith(home) and not os.path.isdir(path):
    293                 self.debug_print("os.makedirs('%s', 0700)" % path)
    294                 os.makedirs(path, 448)
    295 
    296     def run(self):
    297         if not self.skip_build:
    298             self.run_command('build')
    299             build_plat = self.distribution.get_command_obj('build').plat_name
    300             if self.warn_dir and build_plat != get_platform():
    301                 raise DistutilsPlatformError("Can't install when cross-compiling")
    302         for cmd_name in self.get_sub_commands():
    303             self.run_command(cmd_name)
    304 
    305         if self.path_file:
    306             self.create_path_file()
    307         if self.record:
    308             outputs = self.get_outputs()
    309             if self.root:
    310                 root_len = len(self.root)
    311                 for counter in xrange(len(outputs)):
    312                     outputs[counter] = outputs[counter][root_len:]
    313 
    314             self.execute(write_file, (self.record, outputs), "writing list of installed files to '%s'" % self.record)
    315         sys_path = map(os.path.normpath, sys.path)
    316         sys_path = map(os.path.normcase, sys_path)
    317         install_lib = os.path.normcase(os.path.normpath(self.install_lib))
    318         if self.warn_dir and not (self.path_file and self.install_path_file) and install_lib not in sys_path:
    319             log.debug("modules installed to '%s', which is not in Python's module search path (sys.path) -- you'll have to change the search path yourself", self.install_lib)
    320 
    321     def create_path_file(self):
    322         filename = os.path.join(self.install_libbase, self.path_file + '.pth')
    323         if self.install_path_file:
    324             self.execute(write_file, (filename, [self.extra_dirs]), 'creating %s' % filename)
    325         else:
    326             self.warn("path file '%s' not created" % filename)
    327 
    328     def get_outputs(self):
    329         outputs = []
    330         for cmd_name in self.get_sub_commands():
    331             cmd = self.get_finalized_command(cmd_name)
    332             for filename in cmd.get_outputs():
    333                 if filename not in outputs:
    334                     outputs.append(filename)
    335 
    336         if self.path_file and self.install_path_file:
    337             outputs.append(os.path.join(self.install_libbase, self.path_file + '.pth'))
    338         return outputs
    339 
    340     def get_inputs(self):
    341         inputs = []
    342         for cmd_name in self.get_sub_commands():
    343             cmd = self.get_finalized_command(cmd_name)
    344             inputs.extend(cmd.get_inputs())
    345 
    346         return inputs
    347 
    348     def has_lib(self):
    349         return self.distribution.has_pure_modules() or self.distribution.has_ext_modules()
    350 
    351     def has_headers(self):
    352         return self.distribution.has_headers()
    353 
    354     def has_scripts(self):
    355         return self.distribution.has_scripts()
    356 
    357     def has_data(self):
    358         return self.distribution.has_data_files()
    359 
    360     sub_commands = [('install_lib', has_lib),
    361      ('install_headers', has_headers),
    362      ('install_scripts', has_scripts),
    363      ('install_data', has_data),
    364      ('install_egg_info', lambda self: True)]