1 """distutils.command.install
3 Implements the Distutils 'install' command."""
5 from distutils
import log
7 # This module should be kept compatible with Python 2.1.
11 import sys
, os
, string
13 from distutils
.core
import Command
14 from distutils
.debug
import DEBUG
15 from distutils
.sysconfig
import get_config_vars
16 from distutils
.errors
import DistutilsPlatformError
17 from distutils
.file_util
import write_file
18 from distutils
.util
import convert_path
, subst_vars
, change_root
19 from distutils
.util
import get_platform
20 from distutils
.errors
import DistutilsOptionError
21 from site
import USER_BASE
22 from site
import USER_SITE
25 if sys
.version
< "2.2":
29 'headers': '$base/Include/$dist_name',
30 'scripts': '$base/Scripts',
35 'purelib': '$base/Lib/site-packages',
36 'platlib': '$base/Lib/site-packages',
37 'headers': '$base/Include/$dist_name',
38 'scripts': '$base/Scripts',
44 'purelib': '$base/lib/python$py_version_short/site-packages',
45 'platlib': '$platbase/lib/python$py_version_short/site-packages',
46 'headers': '$base/include/python$py_version_short/$dist_name',
47 'scripts': '$base/bin',
51 'purelib': '$base/lib/python',
52 'platlib': '$base/lib/python',
53 'headers': '$base/include/python/$dist_name',
54 'scripts': '$base/bin',
58 'purelib': '$usersite',
59 'platlib': '$usersite',
60 'headers': '$userbase/include/python$py_version_short/$dist_name',
61 'scripts': '$userbase/bin',
66 'purelib': '$usersite',
67 'platlib': '$usersite',
68 'headers': '$userbase/Python$py_version_nodot/Include/$dist_name',
69 'scripts': '$userbase/Scripts',
73 'purelib': '$base/Lib/site-packages',
74 'platlib': '$base/Lib/site-packages',
75 'headers': '$base/Include/$dist_name',
76 'scripts': '$base/Scripts',
80 'purelib': '$usersite',
81 'platlib': '$usersite',
82 'headers': '$userbase/include/python$py_version_short/$dist_name',
83 'scripts': '$userbase/bin',
88 # The keys to an installation scheme; if any new types of files are to be
89 # installed, be sure to add an entry to every installation scheme above,
90 # and to SCHEME_KEYS here.
91 SCHEME_KEYS
= ('purelib', 'platlib', 'headers', 'scripts', 'data')
94 class install (Command
):
96 description
= "install everything from build directory"
99 # Select installation scheme and set base director(y|ies)
101 "installation prefix"),
102 ('exec-prefix=', None,
103 "(Unix only) prefix for platform-specific files"),
105 "(Unix only) home directory to install under"),
107 "install in user site-package '%s'" % USER_SITE
),
109 # Or, just set the base director(y|ies)
110 ('install-base=', None,
111 "base installation directory (instead of --prefix or --home)"),
112 ('install-platbase=', None,
113 "base installation directory for platform-specific files " +
114 "(instead of --exec-prefix or --home)"),
116 "install everything relative to this alternate root directory"),
118 # Or, explicitly set the installation scheme
119 ('install-purelib=', None,
120 "installation directory for pure Python module distributions"),
121 ('install-platlib=', None,
122 "installation directory for non-pure module distributions"),
123 ('install-lib=', None,
124 "installation directory for all module distributions " +
125 "(overrides --install-purelib and --install-platlib)"),
127 ('install-headers=', None,
128 "installation directory for C/C++ headers"),
129 ('install-scripts=', None,
130 "installation directory for Python scripts"),
131 ('install-data=', None,
132 "installation directory for data files"),
134 # Byte-compilation options -- see install_lib.py for details, as
135 # these are duplicated from there (but only install_lib does
136 # anything with them).
137 ('compile', 'c', "compile .py to .pyc [default]"),
138 ('no-compile', None, "don't compile .py files"),
140 "also compile with optimization: -O1 for \"python -O\", "
141 "-O2 for \"python -OO\", and -O0 to disable [default: -O0]"),
143 # Miscellaneous control options
145 "force installation (overwrite any existing files)"),
147 "skip rebuilding everything (for testing/debugging)"),
149 # Where to install documentation (eventually!)
150 #('doc-format=', None, "format of documentation to generate"),
151 #('install-man=', None, "directory for Unix man pages"),
152 #('install-html=', None, "directory for HTML documentation"),
153 #('install-info=', None, "directory for GNU info files"),
156 "filename in which to record list of installed files"),
159 boolean_options
= ['compile', 'force', 'skip-build', 'user']
160 negative_opt
= {'no-compile' : 'compile'}
163 def initialize_options (self
):
165 # High-level options: these select both an installation base
168 self
.exec_prefix
= None
172 # These select only the installation base; it's up to the user to
173 # specify the installation scheme (currently, that means supplying
174 # the --install-{platlib,purelib,scripts,data} options).
175 self
.install_base
= None
176 self
.install_platbase
= None
179 # These options are the actual installation directories; if not
180 # supplied by the user, they are filled in using the installation
181 # scheme implied by prefix/exec-prefix/home and the contents of
182 # that installation scheme.
183 self
.install_purelib
= None # for pure module distributions
184 self
.install_platlib
= None # non-pure (dists w/ extensions)
185 self
.install_headers
= None # for C/C++ headers
186 self
.install_lib
= None # set to either purelib or platlib
187 self
.install_scripts
= None
188 self
.install_data
= None
189 self
.install_userbase
= USER_BASE
190 self
.install_usersite
= USER_SITE
195 # These two are for putting non-packagized distributions into their
196 # own directory and creating a .pth file if it makes sense.
197 # 'extra_path' comes from the setup file; 'install_path_file' can
198 # be turned off if it makes no sense to install a .pth file. (But
199 # better to install it uselessly than to guess wrong and not
200 # install it when it's necessary and would be used!) Currently,
201 # 'install_path_file' is always true unless some outsider meddles
203 self
.extra_path
= None
204 self
.install_path_file
= 1
206 # 'force' forces installation, even if target files are not
207 # out-of-date. 'skip_build' skips running the "build" command,
208 # handy if you know it's not necessary. 'warn_dir' (which is *not*
209 # a user option, it's just there so the bdist_* commands can turn
210 # it off) determines whether we warn about installing to a
211 # directory not in sys.path.
216 # These are only here as a conduit from the 'build' command to the
217 # 'install_*' commands that do the real work. ('build_base' isn't
218 # actually used anywhere, but it might be useful in future.) They
219 # are not user options, because if the user told the install
220 # command where the build directory is, that wouldn't affect the
222 self
.build_base
= None
223 self
.build_lib
= None
225 # Not defined yet because we don't know anything about
227 #self.install_man = None
228 #self.install_html = None
229 #self.install_info = None
234 # -- Option finalizing methods -------------------------------------
235 # (This is rather more involved than for most commands,
236 # because this is where the policy for installing third-
237 # party Python modules on various platforms given a wide
238 # array of user input is decided. Yes, it's quite complex!)
240 def finalize_options (self
):
242 # This method (and its pliant slaves, like 'finalize_unix()',
243 # 'finalize_other()', and 'select_scheme()') is where the default
244 # installation directories for modules, extension modules, and
245 # anything else we care to install from a Python module
246 # distribution. Thus, this code makes a pretty important policy
247 # statement about how third-party stuff is added to a Python
248 # installation! Note that the actual work of installation is done
249 # by the relatively simple 'install_*' commands; they just take
250 # their orders from the installation directory options determined
253 # Check for errors/inconsistencies in the options; first, stuff
254 # that's wrong on any platform.
256 if ((self
.prefix
or self
.exec_prefix
or self
.home
) and
257 (self
.install_base
or self
.install_platbase
)):
258 raise DistutilsOptionError
, \
259 ("must supply either prefix/exec-prefix/home or " +
260 "install-base/install-platbase -- not both")
262 if self
.home
and (self
.prefix
or self
.exec_prefix
):
263 raise DistutilsOptionError
, \
264 "must supply either home or prefix/exec-prefix -- not both"
266 if self
.user
and (self
.prefix
or self
.exec_prefix
or self
.home
or
267 self
.install_base
or self
.install_platbase
):
268 raise DistutilsOptionError("can't combine user with with prefix/"
269 "exec_prefix/home or install_(plat)base")
271 # Next, stuff that's wrong (or dubious) only on certain platforms.
272 if os
.name
!= "posix":
274 self
.warn("exec-prefix option ignored on this platform")
275 self
.exec_prefix
= None
277 # Now the interesting logic -- so interesting that we farm it out
278 # to other methods. The goal of these methods is to set the final
279 # values for the install_{lib,scripts,data,...} options, using as
280 # input a heady brew of prefix, exec_prefix, home, install_base,
281 # install_platbase, user-supplied versions of
282 # install_{purelib,platlib,lib,scripts,data,...}, and the
283 # INSTALL_SCHEME dictionary above. Phew!
285 self
.dump_dirs("pre-finalize_{unix,other}")
287 if os
.name
== 'posix':
290 self
.finalize_other()
292 self
.dump_dirs("post-finalize_{unix,other}()")
294 # Expand configuration variables, tilde, etc. in self.install_base
295 # and self.install_platbase -- that way, we can use $base or
296 # $platbase in the other installation directories and not worry
297 # about needing recursive variable expansion (shudder).
299 py_version
= (string
.split(sys
.version
))[0]
300 (prefix
, exec_prefix
) = get_config_vars('prefix', 'exec_prefix')
301 self
.config_vars
= {'dist_name': self
.distribution
.get_name(),
302 'dist_version': self
.distribution
.get_version(),
303 'dist_fullname': self
.distribution
.get_fullname(),
304 'py_version': py_version
,
305 'py_version_short': py_version
[0:3],
306 'py_version_nodot': py_version
[0] + py_version
[2],
307 'sys_prefix': prefix
,
309 'sys_exec_prefix': exec_prefix
,
310 'exec_prefix': exec_prefix
,
311 'userbase': self
.install_userbase
,
312 'usersite': self
.install_usersite
,
314 self
.expand_basedirs()
316 self
.dump_dirs("post-expand_basedirs()")
318 # Now define config vars for the base directories so we can expand
320 self
.config_vars
['base'] = self
.install_base
321 self
.config_vars
['platbase'] = self
.install_platbase
324 from pprint
import pprint
326 pprint(self
.config_vars
)
328 # Expand "~" and configuration variables in the installation
332 self
.dump_dirs("post-expand_dirs()")
334 # Create directories in the home dir:
336 self
.create_home_path()
338 # Pick the actual directory to install all modules to: either
339 # install_purelib or install_platlib, depending on whether this
340 # module distribution is pure or not. Of course, if the user
341 # already specified install_lib, use their selection.
342 if self
.install_lib
is None:
343 if self
.distribution
.ext_modules
: # has extensions: non-pure
344 self
.install_lib
= self
.install_platlib
346 self
.install_lib
= self
.install_purelib
349 # Convert directories from Unix /-separated syntax to the local
351 self
.convert_paths('lib', 'purelib', 'platlib',
352 'scripts', 'data', 'headers',
353 'userbase', 'usersite')
355 # Well, we're not actually fully completely finalized yet: we still
356 # have to deal with 'extra_path', which is the hack for allowing
357 # non-packagized module distributions (hello, Numerical Python!) to
358 # get their own directories.
359 self
.handle_extra_path()
360 self
.install_libbase
= self
.install_lib
# needed for .pth file
361 self
.install_lib
= os
.path
.join(self
.install_lib
, self
.extra_dirs
)
363 # If a new root directory was supplied, make all the installation
364 # dirs relative to it.
365 if self
.root
is not None:
366 self
.change_roots('libbase', 'lib', 'purelib', 'platlib',
367 'scripts', 'data', 'headers')
369 self
.dump_dirs("after prepending root")
371 # Find out the build directories, ie. where to install from.
372 self
.set_undefined_options('build',
373 ('build_base', 'build_base'),
374 ('build_lib', 'build_lib'))
376 # Punt on doc directories for now -- after all, we're punting on
377 # documentation completely!
379 # finalize_options ()
382 def dump_dirs (self
, msg
):
384 from distutils
.fancy_getopt
import longopt_xlate
386 for opt
in self
.user_options
:
388 if opt_name
[-1] == "=":
389 opt_name
= opt_name
[0:-1]
390 if opt_name
in self
.negative_opt
:
391 opt_name
= string
.translate(self
.negative_opt
[opt_name
],
393 val
= not getattr(self
, opt_name
)
395 opt_name
= string
.translate(opt_name
, longopt_xlate
)
396 val
= getattr(self
, opt_name
)
397 print " %s: %s" % (opt_name
, val
)
400 def finalize_unix (self
):
402 if self
.install_base
is not None or self
.install_platbase
is not None:
403 if ((self
.install_lib
is None and
404 self
.install_purelib
is None and
405 self
.install_platlib
is None) or
406 self
.install_headers
is None or
407 self
.install_scripts
is None or
408 self
.install_data
is None):
409 raise DistutilsOptionError
, \
410 ("install-base or install-platbase supplied, but "
411 "installation scheme is incomplete")
415 if self
.install_userbase
is None:
416 raise DistutilsPlatformError(
417 "User base directory is not specified")
418 self
.install_base
= self
.install_platbase
= self
.install_userbase
419 self
.select_scheme("unix_user")
420 elif self
.home
is not None:
421 self
.install_base
= self
.install_platbase
= self
.home
422 self
.select_scheme("unix_home")
424 if self
.prefix
is None:
425 if self
.exec_prefix
is not None:
426 raise DistutilsOptionError
, \
427 "must not supply exec-prefix without prefix"
429 self
.prefix
= os
.path
.normpath(sys
.prefix
)
430 self
.exec_prefix
= os
.path
.normpath(sys
.exec_prefix
)
433 if self
.exec_prefix
is None:
434 self
.exec_prefix
= self
.prefix
436 self
.install_base
= self
.prefix
437 self
.install_platbase
= self
.exec_prefix
438 self
.select_scheme("unix_prefix")
443 def finalize_other (self
): # Windows and Mac OS for now
446 if self
.install_userbase
is None:
447 raise DistutilsPlatformError(
448 "User base directory is not specified")
449 self
.install_base
= self
.install_platbase
= self
.install_userbase
450 self
.select_scheme(os
.name
+ "_user")
451 elif self
.home
is not None:
452 self
.install_base
= self
.install_platbase
= self
.home
453 self
.select_scheme("unix_home")
455 if self
.prefix
is None:
456 self
.prefix
= os
.path
.normpath(sys
.prefix
)
458 self
.install_base
= self
.install_platbase
= self
.prefix
460 self
.select_scheme(os
.name
)
462 raise DistutilsPlatformError
, \
463 "I don't know how to install stuff on '%s'" % os
.name
468 def select_scheme (self
, name
):
469 # it's the caller's problem if they supply a bad name!
470 scheme
= INSTALL_SCHEMES
[name
]
471 for key
in SCHEME_KEYS
:
472 attrname
= 'install_' + key
473 if getattr(self
, attrname
) is None:
474 setattr(self
, attrname
, scheme
[key
])
477 def _expand_attrs (self
, attrs
):
479 val
= getattr(self
, attr
)
481 if os
.name
== 'posix' or os
.name
== 'nt':
482 val
= os
.path
.expanduser(val
)
483 val
= subst_vars(val
, self
.config_vars
)
484 setattr(self
, attr
, val
)
487 def expand_basedirs (self
):
488 self
._expand
_attrs
(['install_base',
492 def expand_dirs (self
):
493 self
._expand
_attrs
(['install_purelib',
501 def convert_paths (self
, *names
):
503 attr
= "install_" + name
504 setattr(self
, attr
, convert_path(getattr(self
, attr
)))
507 def handle_extra_path (self
):
509 if self
.extra_path
is None:
510 self
.extra_path
= self
.distribution
.extra_path
512 if self
.extra_path
is not None:
513 if type(self
.extra_path
) is StringType
:
514 self
.extra_path
= string
.split(self
.extra_path
, ',')
516 if len(self
.extra_path
) == 1:
517 path_file
= extra_dirs
= self
.extra_path
[0]
518 elif len(self
.extra_path
) == 2:
519 (path_file
, extra_dirs
) = self
.extra_path
521 raise DistutilsOptionError
, \
522 ("'extra_path' option must be a list, tuple, or "
523 "comma-separated string with 1 or 2 elements")
525 # convert to local form in case Unix notation used (as it
526 # should be in setup scripts)
527 extra_dirs
= convert_path(extra_dirs
)
533 # XXX should we warn if path_file and not extra_dirs? (in which
534 # case the path file would be harmless but pointless)
535 self
.path_file
= path_file
536 self
.extra_dirs
= extra_dirs
538 # handle_extra_path ()
541 def change_roots (self
, *names
):
543 attr
= "install_" + name
544 setattr(self
, attr
, change_root(self
.root
, getattr(self
, attr
)))
546 def create_home_path(self
):
547 """Create directories under ~
551 home
= convert_path(os
.path
.expanduser("~"))
552 for name
, path
in self
.config_vars
.iteritems():
553 if path
.startswith(home
) and not os
.path
.isdir(path
):
554 self
.debug_print("os.makedirs('%s', 0700)" % path
)
555 os
.makedirs(path
, 0700)
557 # -- Command execution methods -------------------------------------
561 # Obviously have to build before we can install
562 if not self
.skip_build
:
563 self
.run_command('build')
564 # If we built for any other platform, we can't install.
565 build_plat
= self
.distribution
.get_command_obj('build').plat_name
566 # check warn_dir - it is a clue that the 'install' is happening
567 # internally, and not to sys.path, so we don't check the platform
568 # matches what we are running.
569 if self
.warn_dir
and build_plat
!= get_platform():
570 raise DistutilsPlatformError("Can't install when "
573 # Run all sub-commands (at least those that need to be run)
574 for cmd_name
in self
.get_sub_commands():
575 self
.run_command(cmd_name
)
578 self
.create_path_file()
580 # write list of installed files, if requested.
582 outputs
= self
.get_outputs()
583 if self
.root
: # strip any package prefix
584 root_len
= len(self
.root
)
585 for counter
in xrange(len(outputs
)):
586 outputs
[counter
] = outputs
[counter
][root_len
:]
587 self
.execute(write_file
,
588 (self
.record
, outputs
),
589 "writing list of installed files to '%s'" %
592 sys_path
= map(os
.path
.normpath
, sys
.path
)
593 sys_path
= map(os
.path
.normcase
, sys_path
)
594 install_lib
= os
.path
.normcase(os
.path
.normpath(self
.install_lib
))
595 if (self
.warn_dir
and
596 not (self
.path_file
and self
.install_path_file
) and
597 install_lib
not in sys_path
):
598 log
.debug(("modules installed to '%s', which is not in "
599 "Python's module search path (sys.path) -- "
600 "you'll have to change the search path yourself"),
605 def create_path_file (self
):
606 filename
= os
.path
.join(self
.install_libbase
,
607 self
.path_file
+ ".pth")
608 if self
.install_path_file
:
609 self
.execute(write_file
,
610 (filename
, [self
.extra_dirs
]),
611 "creating %s" % filename
)
613 self
.warn("path file '%s' not created" % filename
)
616 # -- Reporting methods ---------------------------------------------
618 def get_outputs (self
):
619 # Assemble the outputs of all the sub-commands.
621 for cmd_name
in self
.get_sub_commands():
622 cmd
= self
.get_finalized_command(cmd_name
)
623 # Add the contents of cmd.get_outputs(), ensuring
624 # that outputs doesn't contain duplicate entries
625 for filename
in cmd
.get_outputs():
626 if filename
not in outputs
:
627 outputs
.append(filename
)
629 if self
.path_file
and self
.install_path_file
:
630 outputs
.append(os
.path
.join(self
.install_libbase
,
631 self
.path_file
+ ".pth"))
635 def get_inputs (self
):
636 # XXX gee, this looks familiar ;-(
638 for cmd_name
in self
.get_sub_commands():
639 cmd
= self
.get_finalized_command(cmd_name
)
640 inputs
.extend(cmd
.get_inputs())
645 # -- Predicates for sub-command list -------------------------------
648 """Return true if the current distribution has any Python
649 modules to install."""
650 return (self
.distribution
.has_pure_modules() or
651 self
.distribution
.has_ext_modules())
653 def has_headers (self
):
654 return self
.distribution
.has_headers()
656 def has_scripts (self
):
657 return self
.distribution
.has_scripts()
660 return self
.distribution
.has_data_files()
663 # 'sub_commands': a list of commands this command might have to run to
664 # get its work done. See cmd.py for more info.
665 sub_commands
= [('install_lib', has_lib
),
666 ('install_headers', has_headers
),
667 ('install_scripts', has_scripts
),
668 ('install_data', has_data
),
669 ('install_egg_info', lambda self
:True),