| 1 |
"""distutils.command.install |
| 2 |
|
| 3 |
Implements the Distutils 'install' command.""" |
| 4 |
|
| 5 |
import sys |
| 6 |
import sysconfig |
| 7 |
import os |
| 8 |
import re |
| 9 |
|
| 10 |
from distutils import log |
| 11 |
from distutils.core import Command |
| 12 |
from distutils.debug import DEBUG |
| 13 |
from distutils.sysconfig import get_config_vars |
| 14 |
from distutils.errors import DistutilsPlatformError |
| 15 |
from distutils.file_util import write_file |
| 16 |
from distutils.util import convert_path, subst_vars, change_root |
| 17 |
from distutils.util import get_platform |
| 18 |
from distutils.errors import DistutilsOptionError |
| 19 |
|
| 20 |
from site import USER_BASE |
| 21 |
from site import USER_SITE |
| 22 |
|
| 23 |
HAS_USER_SITE = (USER_SITE is not None) |
| 24 |
# See also: site.py/sysconfig.py |
| 25 |
def _get_implementation(): |
| 26 |
if '__pypy__' in sys.builtin_module_names: |
| 27 |
return 'PyPy' |
| 28 |
return 'Python' |
| 29 |
|
| 30 |
|
| 31 |
IMPLEMENTATION = _get_implementation() |
| 32 |
IMPLEMENTATION_LOWER = IMPLEMENTATION.lower() |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
# The keys to an installation scheme; if any new types of files are to be |
| 37 |
# installed, be sure to add an entry to every scheme in |
| 38 |
# sysconfig._INSTALL_SCHEMES, and to SCHEME_KEYS here. |
| 39 |
SCHEME_KEYS = ('purelib', 'platlib', 'headers', 'scripts', 'data') |
| 40 |
|
| 41 |
# The following code provides backward-compatible INSTALL_SCHEMES |
| 42 |
# while making the sysconfig module the single point of truth. |
| 43 |
# This makes it easier for OS distributions where they need to |
| 44 |
# alter locations for packages installations in a single place. |
| 45 |
# Note that this module is deprecated (PEP 632); all consumers |
| 46 |
# of this information should switch to using sysconfig directly. |
| 47 |
INSTALL_SCHEMES = {"unix_prefix": {}, "unix_home": {}, "nt": {}} |
| 48 |
|
| 49 |
# Copy from sysconfig._INSTALL_SCHEMES |
| 50 |
for key in SCHEME_KEYS: |
| 51 |
for distutils_scheme_name, sys_scheme_name in ( |
| 52 |
("unix_prefix", "posix_prefix"), ("unix_home", "posix_home"), |
| 53 |
("nt", "nt")): |
| 54 |
sys_key = key |
| 55 |
sys_scheme = sysconfig._INSTALL_SCHEMES[sys_scheme_name] |
| 56 |
if key == "headers" and key not in sys_scheme: |
| 57 |
# On POSIX-y platforms, Python will: |
| 58 |
# - Build from .h files in 'headers' (only there when |
| 59 |
# building CPython) |
| 60 |
# - Install .h files to 'include' |
| 61 |
# When 'headers' is missing, fall back to 'include' |
| 62 |
sys_key = 'include' |
| 63 |
INSTALL_SCHEMES[distutils_scheme_name][key] = sys_scheme[sys_key] |
| 64 |
|
| 65 |
# Transformation to different template format |
| 66 |
for main_key in INSTALL_SCHEMES: |
| 67 |
for key, value in INSTALL_SCHEMES[main_key].items(): |
| 68 |
# Change all ocurences of {variable} to $variable |
| 69 |
value = re.sub(r"\{(.+?)\}", r"$\g<1>", value) |
| 70 |
value = value.replace("$installed_base", "$base") |
| 71 |
value = value.replace("$py_version_nodot_plat", "$py_version_nodot") |
| 72 |
if key == "headers": |
| 73 |
value += "/$dist_name" |
| 74 |
if sys.version_info >= (3, 9) and key == "platlib": |
| 75 |
# platlibdir is available since 3.9: bpo-1294959 |
| 76 |
value = value.replace("/lib/", "/$platlibdir/") |
| 77 |
INSTALL_SCHEMES[main_key][key] = value |
| 78 |
|
| 79 |
# The following part of INSTALL_SCHEMES has a different definition |
| 80 |
# than the one in sysconfig, but because both depend on the site module, |
| 81 |
# the outcomes should be the same. |
| 82 |
if HAS_USER_SITE: |
| 83 |
INSTALL_SCHEMES['nt_user'] = { |
| 84 |
'purelib': '$usersite', |
| 85 |
'platlib': '$usersite', |
| 86 |
'headers': '$userbase/' + IMPLEMENTATION + '$py_version_nodot/Include/$dist_name', |
| 87 |
'scripts': '$userbase/' + IMPLEMENTATION + '$py_version_nodot/Scripts', |
| 88 |
'data' : '$userbase', |
| 89 |
} |
| 90 |
|
| 91 |
INSTALL_SCHEMES['unix_user'] = { |
| 92 |
'purelib': '$usersite', |
| 93 |
'platlib': '$usersite', |
| 94 |
'headers': |
| 95 |
'$userbase/include/' + IMPLEMENTATION_LOWER + '$py_version_short$abiflags/$dist_name', |
| 96 |
'scripts': '$userbase/bin', |
| 97 |
'data' : '$userbase', |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
class install(Command): |
| 102 |
|
| 103 |
description = "install everything from build directory" |
| 104 |
|
| 105 |
user_options = [ |
| 106 |
# Select installation scheme and set base director(y|ies) |
| 107 |
('prefix=', None, |
| 108 |
"installation prefix"), |
| 109 |
('exec-prefix=', None, |
| 110 |
"(Unix only) prefix for platform-specific files"), |
| 111 |
('home=', None, |
| 112 |
"(Unix only) home directory to install under"), |
| 113 |
|
| 114 |
# Or, just set the base director(y|ies) |
| 115 |
('install-base=', None, |
| 116 |
"base installation directory (instead of --prefix or --home)"), |
| 117 |
('install-platbase=', None, |
| 118 |
"base installation directory for platform-specific files " + |
| 119 |
"(instead of --exec-prefix or --home)"), |
| 120 |
('root=', None, |
| 121 |
"install everything relative to this alternate root directory"), |
| 122 |
|
| 123 |
# Or, explicitly set the installation scheme |
| 124 |
('install-purelib=', None, |
| 125 |
"installation directory for pure Python module distributions"), |
| 126 |
('install-platlib=', None, |
| 127 |
"installation directory for non-pure module distributions"), |
| 128 |
('install-lib=', None, |
| 129 |
"installation directory for all module distributions " + |
| 130 |
"(overrides --install-purelib and --install-platlib)"), |
| 131 |
|
| 132 |
('install-headers=', None, |
| 133 |
"installation directory for C/C++ headers"), |
| 134 |
('install-scripts=', None, |
| 135 |
"installation directory for Python scripts"), |
| 136 |
('install-data=', None, |
| 137 |
"installation directory for data files"), |
| 138 |
|
| 139 |
# Byte-compilation options -- see install_lib.py for details, as |
| 140 |
# these are duplicated from there (but only install_lib does |
| 141 |
# anything with them). |
| 142 |
('compile', 'c', "compile .py to .pyc [default]"), |
| 143 |
('no-compile', None, "don't compile .py files"), |
| 144 |
('optimize=', 'O', |
| 145 |
"also compile with optimization: -O1 for \"python -O\", " |
| 146 |
"-O2 for \"python -OO\", and -O0 to disable [default: -O0]"), |
| 147 |
|
| 148 |
# Miscellaneous control options |
| 149 |
('force', 'f', |
| 150 |
"force installation (overwrite any existing files)"), |
| 151 |
('skip-build', None, |
| 152 |
"skip rebuilding everything (for testing/debugging)"), |
| 153 |
|
| 154 |
# Where to install documentation (eventually!) |
| 155 |
#('doc-format=', None, "format of documentation to generate"), |
| 156 |
#('install-man=', None, "directory for Unix man pages"), |
| 157 |
#('install-html=', None, "directory for HTML documentation"), |
| 158 |
#('install-info=', None, "directory for GNU info files"), |
| 159 |
|
| 160 |
('record=', None, |
| 161 |
"filename in which to record list of installed files"), |
| 162 |
] |
| 163 |
|
| 164 |
boolean_options = ['compile', 'force', 'skip-build'] |
| 165 |
|
| 166 |
if HAS_USER_SITE: |
| 167 |
user_options.append(('user', None, |
| 168 |
"install in user site-package '%s'" % USER_SITE)) |
| 169 |
boolean_options.append('user') |
| 170 |
|
| 171 |
negative_opt = {'no-compile' : 'compile'} |
| 172 |
|
| 173 |
|
| 174 |
def initialize_options(self): |
| 175 |
"""Initializes options.""" |
| 176 |
# High-level options: these select both an installation base |
| 177 |
# and scheme. |
| 178 |
self.prefix = None |
| 179 |
self.exec_prefix = None |
| 180 |
self.home = None |
| 181 |
self.user = 0 |
| 182 |
|
| 183 |
# These select only the installation base; it's up to the user to |
| 184 |
# specify the installation scheme (currently, that means supplying |
| 185 |
# the --install-{platlib,purelib,scripts,data} options). |
| 186 |
self.install_base = None |
| 187 |
self.install_platbase = None |
| 188 |
self.root = None |
| 189 |
|
| 190 |
# These options are the actual installation directories; if not |
| 191 |
# supplied by the user, they are filled in using the installation |
| 192 |
# scheme implied by prefix/exec-prefix/home and the contents of |
| 193 |
# that installation scheme. |
| 194 |
self.install_purelib = None # for pure module distributions |
| 195 |
self.install_platlib = None # non-pure (dists w/ extensions) |
| 196 |
self.install_headers = None # for C/C++ headers |
| 197 |
self.install_lib = None # set to either purelib or platlib |
| 198 |
self.install_scripts = None |
| 199 |
self.install_data = None |
| 200 |
if HAS_USER_SITE: |
| 201 |
self.install_userbase = USER_BASE |
| 202 |
self.install_usersite = USER_SITE |
| 203 |
|
| 204 |
self.compile = None |
| 205 |
self.optimize = None |
| 206 |
|
| 207 |
# Deprecated |
| 208 |
# These two are for putting non-packagized distributions into their |
| 209 |
# own directory and creating a .pth file if it makes sense. |
| 210 |
# 'extra_path' comes from the setup file; 'install_path_file' can |
| 211 |
# be turned off if it makes no sense to install a .pth file. (But |
| 212 |
# better to install it uselessly than to guess wrong and not |
| 213 |
# install it when it's necessary and would be used!) Currently, |
| 214 |
# 'install_path_file' is always true unless some outsider meddles |
| 215 |
# with it. |
| 216 |
self.extra_path = None |
| 217 |
self.install_path_file = 1 |
| 218 |
|
| 219 |
# 'force' forces installation, even if target files are not |
| 220 |
# out-of-date. 'skip_build' skips running the "build" command, |
| 221 |
# handy if you know it's not necessary. 'warn_dir' (which is *not* |
| 222 |
# a user option, it's just there so the bdist_* commands can turn |
| 223 |
# it off) determines whether we warn about installing to a |
| 224 |
# directory not in sys.path. |
| 225 |
self.force = 0 |
| 226 |
self.skip_build = 0 |
| 227 |
self.warn_dir = 1 |
| 228 |
|
| 229 |
# These are only here as a conduit from the 'build' command to the |
| 230 |
# 'install_*' commands that do the real work. ('build_base' isn't |
| 231 |
# actually used anywhere, but it might be useful in future.) They |
| 232 |
# are not user options, because if the user told the install |
| 233 |
# command where the build directory is, that wouldn't affect the |
| 234 |
# build command. |
| 235 |
self.build_base = None |
| 236 |
self.build_lib = None |
| 237 |
|
| 238 |
# Not defined yet because we don't know anything about |
| 239 |
# documentation yet. |
| 240 |
#self.install_man = None |
| 241 |
#self.install_html = None |
| 242 |
#self.install_info = None |
| 243 |
|
| 244 |
self.record = None |
| 245 |
|
| 246 |
|
| 247 |
# -- Option finalizing methods ------------------------------------- |
| 248 |
# (This is rather more involved than for most commands, |
| 249 |
# because this is where the policy for installing third- |
| 250 |
# party Python modules on various platforms given a wide |
| 251 |
# array of user input is decided. Yes, it's quite complex!) |
| 252 |
|
| 253 |
def finalize_options(self): |
| 254 |
"""Finalizes options.""" |
| 255 |
# This method (and its helpers, like 'finalize_unix()', |
| 256 |
# 'finalize_other()', and 'select_scheme()') is where the default |
| 257 |
# installation directories for modules, extension modules, and |
| 258 |
# anything else we care to install from a Python module |
| 259 |
# distribution. Thus, this code makes a pretty important policy |
| 260 |
# statement about how third-party stuff is added to a Python |
| 261 |
# installation! Note that the actual work of installation is done |
| 262 |
# by the relatively simple 'install_*' commands; they just take |
| 263 |
# their orders from the installation directory options determined |
| 264 |
# here. |
| 265 |
|
| 266 |
# Check for errors/inconsistencies in the options; first, stuff |
| 267 |
# that's wrong on any platform. |
| 268 |
|
| 269 |
if ((self.prefix or self.exec_prefix or self.home) and |
| 270 |
(self.install_base or self.install_platbase)): |
| 271 |
raise DistutilsOptionError( |
| 272 |
"must supply either prefix/exec-prefix/home or " + |
| 273 |
"install-base/install-platbase -- not both") |
| 274 |
|
| 275 |
if self.home and (self.prefix or self.exec_prefix): |
| 276 |
raise DistutilsOptionError( |
| 277 |
"must supply either home or prefix/exec-prefix -- not both") |
| 278 |
|
| 279 |
if self.user and (self.prefix or self.exec_prefix or self.home or |
| 280 |
self.install_base or self.install_platbase): |
| 281 |
raise DistutilsOptionError("can't combine user with prefix, " |
| 282 |
"exec_prefix/home, or install_(plat)base") |
| 283 |
|
| 284 |
# Next, stuff that's wrong (or dubious) only on certain platforms. |
| 285 |
if os.name != "posix": |
| 286 |
if self.exec_prefix: |
| 287 |
self.warn("exec-prefix option ignored on this platform") |
| 288 |
self.exec_prefix = None |
| 289 |
|
| 290 |
# Now the interesting logic -- so interesting that we farm it out |
| 291 |
# to other methods. The goal of these methods is to set the final |
| 292 |
# values for the install_{lib,scripts,data,...} options, using as |
| 293 |
# input a heady brew of prefix, exec_prefix, home, install_base, |
| 294 |
# install_platbase, user-supplied versions of |
| 295 |
# install_{purelib,platlib,lib,scripts,data,...}, and the |
| 296 |
# INSTALL_SCHEME dictionary above. Phew! |
| 297 |
|
| 298 |
self.dump_dirs("pre-finalize_{unix,other}") |
| 299 |
|
| 300 |
if os.name == 'posix': |
| 301 |
self.finalize_unix() |
| 302 |
else: |
| 303 |
self.finalize_other() |
| 304 |
|
| 305 |
self.dump_dirs("post-finalize_{unix,other}()") |
| 306 |
|
| 307 |
# Expand configuration variables, tilde, etc. in self.install_base |
| 308 |
# and self.install_platbase -- that way, we can use $base or |
| 309 |
# $platbase in the other installation directories and not worry |
| 310 |
# about needing recursive variable expansion (shudder). |
| 311 |
|
| 312 |
py_version = sys.version.split()[0] |
| 313 |
(prefix, exec_prefix) = get_config_vars('prefix', 'exec_prefix') |
| 314 |
try: |
| 315 |
abiflags = sys.abiflags |
| 316 |
except AttributeError: |
| 317 |
# sys.abiflags may not be defined on all platforms. |
| 318 |
abiflags = '' |
| 319 |
self.config_vars = {'dist_name': self.distribution.get_name(), |
| 320 |
'dist_version': self.distribution.get_version(), |
| 321 |
'dist_fullname': self.distribution.get_fullname(), |
| 322 |
'py_version': py_version, |
| 323 |
'py_version_short': '%d.%d' % sys.version_info[:2], |
| 324 |
'py_version_nodot': '%d%d' % sys.version_info[:2], |
| 325 |
'sys_prefix': prefix, |
| 326 |
'prefix': prefix, |
| 327 |
'sys_exec_prefix': exec_prefix, |
| 328 |
'exec_prefix': exec_prefix, |
| 329 |
'abiflags': abiflags, |
| 330 |
'platlibdir': sys.platlibdir, |
| 331 |
'implementation': sysconfig._get_implementation(), |
| 332 |
'implementation_lower': sysconfig._get_implementation().lower(), |
| 333 |
} |
| 334 |
|
| 335 |
if HAS_USER_SITE: |
| 336 |
self.config_vars['userbase'] = self.install_userbase |
| 337 |
self.config_vars['usersite'] = self.install_usersite |
| 338 |
|
| 339 |
if sysconfig.is_python_build(True): |
| 340 |
self.config_vars['srcdir'] = sysconfig.get_config_var('srcdir') |
| 341 |
|
| 342 |
self.expand_basedirs() |
| 343 |
|
| 344 |
self.dump_dirs("post-expand_basedirs()") |
| 345 |
|
| 346 |
# Now define config vars for the base directories so we can expand |
| 347 |
# everything else. |
| 348 |
self.config_vars['base'] = self.install_base |
| 349 |
self.config_vars['platbase'] = self.install_platbase |
| 350 |
|
| 351 |
if DEBUG: |
| 352 |
from pprint import pprint |
| 353 |
print("config vars:") |
| 354 |
pprint(self.config_vars) |
| 355 |
|
| 356 |
# Expand "~" and configuration variables in the installation |
| 357 |
# directories. |
| 358 |
self.expand_dirs() |
| 359 |
|
| 360 |
self.dump_dirs("post-expand_dirs()") |
| 361 |
|
| 362 |
# Create directories in the home dir: |
| 363 |
if self.user: |
| 364 |
self.create_home_path() |
| 365 |
|
| 366 |
# Pick the actual directory to install all modules to: either |
| 367 |
# install_purelib or install_platlib, depending on whether this |
| 368 |
# module distribution is pure or not. Of course, if the user |
| 369 |
# already specified install_lib, use their selection. |
| 370 |
if self.install_lib is None: |
| 371 |
if self.distribution.ext_modules: # has extensions: non-pure |
| 372 |
self.install_lib = self.install_platlib |
| 373 |
else: |
| 374 |
self.install_lib = self.install_purelib |
| 375 |
|
| 376 |
|
| 377 |
# Convert directories from Unix /-separated syntax to the local |
| 378 |
# convention. |
| 379 |
self.convert_paths('lib', 'purelib', 'platlib', |
| 380 |
'scripts', 'data', 'headers') |
| 381 |
if HAS_USER_SITE: |
| 382 |
self.convert_paths('userbase', 'usersite') |
| 383 |
|
| 384 |
# Deprecated |
| 385 |
# Well, we're not actually fully completely finalized yet: we still |
| 386 |
# have to deal with 'extra_path', which is the hack for allowing |
| 387 |
# non-packagized module distributions (hello, Numerical Python!) to |
| 388 |
# get their own directories. |
| 389 |
self.handle_extra_path() |
| 390 |
self.install_libbase = self.install_lib # needed for .pth file |
| 391 |
self.install_lib = os.path.join(self.install_lib, self.extra_dirs) |
| 392 |
|
| 393 |
# If a new root directory was supplied, make all the installation |
| 394 |
# dirs relative to it. |
| 395 |
if self.root is not None: |
| 396 |
self.change_roots('libbase', 'lib', 'purelib', 'platlib', |
| 397 |
'scripts', 'data', 'headers') |
| 398 |
|
| 399 |
self.dump_dirs("after prepending root") |
| 400 |
|
| 401 |
# Find out the build directories, ie. where to install from. |
| 402 |
self.set_undefined_options('build', |
| 403 |
('build_base', 'build_base'), |
| 404 |
('build_lib', 'build_lib')) |
| 405 |
|
| 406 |
# Punt on doc directories for now -- after all, we're punting on |
| 407 |
# documentation completely! |
| 408 |
|
| 409 |
def dump_dirs(self, msg): |
| 410 |
"""Dumps the list of user options.""" |
| 411 |
if not DEBUG: |
| 412 |
return |
| 413 |
from distutils.fancy_getopt import longopt_xlate |
| 414 |
log.debug(msg + ":") |
| 415 |
for opt in self.user_options: |
| 416 |
opt_name = opt[0] |
| 417 |
if opt_name[-1] == "=": |
| 418 |
opt_name = opt_name[0:-1] |
| 419 |
if opt_name in self.negative_opt: |
| 420 |
opt_name = self.negative_opt[opt_name] |
| 421 |
opt_name = opt_name.translate(longopt_xlate) |
| 422 |
val = not getattr(self, opt_name) |
| 423 |
else: |
| 424 |
opt_name = opt_name.translate(longopt_xlate) |
| 425 |
val = getattr(self, opt_name) |
| 426 |
log.debug(" %s: %s", opt_name, val) |
| 427 |
|
| 428 |
def finalize_unix(self): |
| 429 |
"""Finalizes options for posix platforms.""" |
| 430 |
if self.install_base is not None or self.install_platbase is not None: |
| 431 |
if ((self.install_lib is None and |
| 432 |
self.install_purelib is None and |
| 433 |
self.install_platlib is None) or |
| 434 |
self.install_headers is None or |
| 435 |
self.install_scripts is None or |
| 436 |
self.install_data is None): |
| 437 |
raise DistutilsOptionError( |
| 438 |
"install-base or install-platbase supplied, but " |
| 439 |
"installation scheme is incomplete") |
| 440 |
return |
| 441 |
|
| 442 |
if self.user: |
| 443 |
if self.install_userbase is None: |
| 444 |
raise DistutilsPlatformError( |
| 445 |
"User base directory is not specified") |
| 446 |
self.install_base = self.install_platbase = self.install_userbase |
| 447 |
self.select_scheme("unix_user") |
| 448 |
elif self.home is not None: |
| 449 |
self.install_base = self.install_platbase = self.home |
| 450 |
self.select_scheme("unix_home") |
| 451 |
else: |
| 452 |
if self.prefix is None: |
| 453 |
if self.exec_prefix is not None: |
| 454 |
raise DistutilsOptionError( |
| 455 |
"must not supply exec-prefix without prefix") |
| 456 |
|
| 457 |
self.prefix = os.path.normpath(sys.prefix) |
| 458 |
self.exec_prefix = os.path.normpath(sys.exec_prefix) |
| 459 |
|
| 460 |
else: |
| 461 |
if self.exec_prefix is None: |
| 462 |
self.exec_prefix = self.prefix |
| 463 |
|
| 464 |
self.install_base = self.prefix |
| 465 |
self.install_platbase = self.exec_prefix |
| 466 |
self.select_scheme("unix_prefix") |
| 467 |
|
| 468 |
def finalize_other(self): |
| 469 |
"""Finalizes options for non-posix platforms""" |
| 470 |
if self.user: |
| 471 |
if self.install_userbase is None: |
| 472 |
raise DistutilsPlatformError( |
| 473 |
"User base directory is not specified") |
| 474 |
self.install_base = self.install_platbase = self.install_userbase |
| 475 |
self.select_scheme(os.name + "_user") |
| 476 |
elif self.home is not None: |
| 477 |
self.install_base = self.install_platbase = self.home |
| 478 |
self.select_scheme("unix_home") |
| 479 |
else: |
| 480 |
if self.prefix is None: |
| 481 |
self.prefix = os.path.normpath(sys.prefix) |
| 482 |
|
| 483 |
self.install_base = self.install_platbase = self.prefix |
| 484 |
try: |
| 485 |
self.select_scheme(os.name) |
| 486 |
except KeyError: |
| 487 |
raise DistutilsPlatformError( |
| 488 |
"I don't know how to install stuff on '%s'" % os.name) |
| 489 |
|
| 490 |
def select_scheme(self, name): |
| 491 |
"""Sets the install directories by applying the install schemes.""" |
| 492 |
# it's the caller's problem if they supply a bad name! |
| 493 |
scheme = INSTALL_SCHEMES[name] |
| 494 |
for key in SCHEME_KEYS: |
| 495 |
attrname = 'install_' + key |
| 496 |
if getattr(self, attrname) is None: |
| 497 |
setattr(self, attrname, scheme[key]) |
| 498 |
|
| 499 |
def _expand_attrs(self, attrs): |
| 500 |
for attr in attrs: |
| 501 |
val = getattr(self, attr) |
| 502 |
if val is not None: |
| 503 |
if os.name == 'posix' or os.name == 'nt': |
| 504 |
val = os.path.expanduser(val) |
| 505 |
val = subst_vars(val, self.config_vars) |
| 506 |
setattr(self, attr, val) |
| 507 |
|
| 508 |
def expand_basedirs(self): |
| 509 |
"""Calls `os.path.expanduser` on install_base, install_platbase and |
| 510 |
root.""" |
| 511 |
self._expand_attrs(['install_base', 'install_platbase', 'root']) |
| 512 |
|
| 513 |
def expand_dirs(self): |
| 514 |
"""Calls `os.path.expanduser` on install dirs.""" |
| 515 |
self._expand_attrs(['install_purelib', 'install_platlib', |
| 516 |
'install_lib', 'install_headers', |
| 517 |
'install_scripts', 'install_data',]) |
| 518 |
|
| 519 |
def convert_paths(self, *names): |
| 520 |
"""Call `convert_path` over `names`.""" |
| 521 |
for name in names: |
| 522 |
attr = "install_" + name |
| 523 |
setattr(self, attr, convert_path(getattr(self, attr))) |
| 524 |
|
| 525 |
def handle_extra_path(self): |
| 526 |
"""Set `path_file` and `extra_dirs` using `extra_path`.""" |
| 527 |
if self.extra_path is None: |
| 528 |
self.extra_path = self.distribution.extra_path |
| 529 |
|
| 530 |
if self.extra_path is not None: |
| 531 |
log.warn( |
| 532 |
"Distribution option extra_path is deprecated. " |
| 533 |
"See issue27919 for details." |
| 534 |
) |
| 535 |
if isinstance(self.extra_path, str): |
| 536 |
self.extra_path = self.extra_path.split(',') |
| 537 |
|
| 538 |
if len(self.extra_path) == 1: |
| 539 |
path_file = extra_dirs = self.extra_path[0] |
| 540 |
elif len(self.extra_path) == 2: |
| 541 |
path_file, extra_dirs = self.extra_path |
| 542 |
else: |
| 543 |
raise DistutilsOptionError( |
| 544 |
"'extra_path' option must be a list, tuple, or " |
| 545 |
"comma-separated string with 1 or 2 elements") |
| 546 |
|
| 547 |
# convert to local form in case Unix notation used (as it |
| 548 |
# should be in setup scripts) |
| 549 |
extra_dirs = convert_path(extra_dirs) |
| 550 |
else: |
| 551 |
path_file = None |
| 552 |
extra_dirs = '' |
| 553 |
|
| 554 |
# XXX should we warn if path_file and not extra_dirs? (in which |
| 555 |
# case the path file would be harmless but pointless) |
| 556 |
self.path_file = path_file |
| 557 |
self.extra_dirs = extra_dirs |
| 558 |
|
| 559 |
def change_roots(self, *names): |
| 560 |
"""Change the install directories pointed by name using root.""" |
| 561 |
for name in names: |
| 562 |
attr = "install_" + name |
| 563 |
setattr(self, attr, change_root(self.root, getattr(self, attr))) |
| 564 |
|
| 565 |
def create_home_path(self): |
| 566 |
"""Create directories under ~.""" |
| 567 |
if not self.user: |
| 568 |
return |
| 569 |
home = convert_path(os.path.expanduser("~")) |
| 570 |
for name, path in self.config_vars.items(): |
| 571 |
if path.startswith(home) and not os.path.isdir(path): |
| 572 |
self.debug_print("os.makedirs('%s', 0o700)" % path) |
| 573 |
os.makedirs(path, 0o700) |
| 574 |
|
| 575 |
# -- Command execution methods ------------------------------------- |
| 576 |
|
| 577 |
def run(self): |
| 578 |
"""Runs the command.""" |
| 579 |
# Obviously have to build before we can install |
| 580 |
if not self.skip_build: |
| 581 |
self.run_command('build') |
| 582 |
# If we built for any other platform, we can't install. |
| 583 |
build_plat = self.distribution.get_command_obj('build').plat_name |
| 584 |
# check warn_dir - it is a clue that the 'install' is happening |
| 585 |
# internally, and not to sys.path, so we don't check the platform |
| 586 |
# matches what we are running. |
| 587 |
if self.warn_dir and build_plat != get_platform(): |
| 588 |
raise DistutilsPlatformError("Can't install when " |
| 589 |
"cross-compiling") |
| 590 |
|
| 591 |
# Run all sub-commands (at least those that need to be run) |
| 592 |
for cmd_name in self.get_sub_commands(): |
| 593 |
self.run_command(cmd_name) |
| 594 |
|
| 595 |
if self.path_file: |
| 596 |
self.create_path_file() |
| 597 |
|
| 598 |
# write list of installed files, if requested. |
| 599 |
if self.record: |
| 600 |
outputs = self.get_outputs() |
| 601 |
if self.root: # strip any package prefix |
| 602 |
root_len = len(self.root) |
| 603 |
for counter in range(len(outputs)): |
| 604 |
outputs[counter] = outputs[counter][root_len:] |
| 605 |
self.execute(write_file, |
| 606 |
(self.record, outputs), |
| 607 |
"writing list of installed files to '%s'" % |
| 608 |
self.record) |
| 609 |
|
| 610 |
sys_path = map(os.path.normpath, sys.path) |
| 611 |
sys_path = map(os.path.normcase, sys_path) |
| 612 |
install_lib = os.path.normcase(os.path.normpath(self.install_lib)) |
| 613 |
if (self.warn_dir and |
| 614 |
not (self.path_file and self.install_path_file) and |
| 615 |
install_lib not in sys_path): |
| 616 |
log.debug(("modules installed to '%s', which is not in " |
| 617 |
"Python's module search path (sys.path) -- " |
| 618 |
"you'll have to change the search path yourself"), |
| 619 |
self.install_lib) |
| 620 |
|
| 621 |
def create_path_file(self): |
| 622 |
"""Creates the .pth file""" |
| 623 |
filename = os.path.join(self.install_libbase, |
| 624 |
self.path_file + ".pth") |
| 625 |
if self.install_path_file: |
| 626 |
self.execute(write_file, |
| 627 |
(filename, [self.extra_dirs]), |
| 628 |
"creating %s" % filename) |
| 629 |
else: |
| 630 |
self.warn("path file '%s' not created" % filename) |
| 631 |
|
| 632 |
|
| 633 |
# -- Reporting methods --------------------------------------------- |
| 634 |
|
| 635 |
def get_outputs(self): |
| 636 |
"""Assembles the outputs of all the sub-commands.""" |
| 637 |
outputs = [] |
| 638 |
for cmd_name in self.get_sub_commands(): |
| 639 |
cmd = self.get_finalized_command(cmd_name) |
| 640 |
# Add the contents of cmd.get_outputs(), ensuring |
| 641 |
# that outputs doesn't contain duplicate entries |
| 642 |
for filename in cmd.get_outputs(): |
| 643 |
if filename not in outputs: |
| 644 |
outputs.append(filename) |
| 645 |
|
| 646 |
if self.path_file and self.install_path_file: |
| 647 |
outputs.append(os.path.join(self.install_libbase, |
| 648 |
self.path_file + ".pth")) |
| 649 |
|
| 650 |
return outputs |
| 651 |
|
| 652 |
def get_inputs(self): |
| 653 |
"""Returns the inputs of all the sub-commands""" |
| 654 |
# XXX gee, this looks familiar ;-( |
| 655 |
inputs = [] |
| 656 |
for cmd_name in self.get_sub_commands(): |
| 657 |
cmd = self.get_finalized_command(cmd_name) |
| 658 |
inputs.extend(cmd.get_inputs()) |
| 659 |
|
| 660 |
return inputs |
| 661 |
|
| 662 |
# -- Predicates for sub-command list ------------------------------- |
| 663 |
|
| 664 |
def has_lib(self): |
| 665 |
"""Returns true if the current distribution has any Python |
| 666 |
modules to install.""" |
| 667 |
return (self.distribution.has_pure_modules() or |
| 668 |
self.distribution.has_ext_modules()) |
| 669 |
|
| 670 |
def has_headers(self): |
| 671 |
"""Returns true if the current distribution has any headers to |
| 672 |
install.""" |
| 673 |
return self.distribution.has_headers() |
| 674 |
|
| 675 |
def has_scripts(self): |
| 676 |
"""Returns true if the current distribution has any scripts to. |
| 677 |
install.""" |
| 678 |
return self.distribution.has_scripts() |
| 679 |
|
| 680 |
def has_data(self): |
| 681 |
"""Returns true if the current distribution has any data to. |
| 682 |
install.""" |
| 683 |
return self.distribution.has_data_files() |
| 684 |
|
| 685 |
# 'sub_commands': a list of commands this command might have to run to |
| 686 |
# get its work done. See cmd.py for more info. |
| 687 |
sub_commands = [('install_lib', has_lib), |
| 688 |
('install_headers', has_headers), |
| 689 |
('install_scripts', has_scripts), |
| 690 |
('install_data', has_data), |
| 691 |
('install_egg_info', lambda self:True), |
| 692 |
] |