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