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