blob: f1f3bd5c6f5d26a5cfdb90fc3aa826c600b892c7 [file] [log] [blame]
Greg Ward13ae1c81999-03-22 14:55:25 +00001"""distutils.command.install
2
3Implements the Distutils 'install' command."""
4
Tarek Ziadédd7bef92010-03-05 00:16:02 +00005from distutils import log
6
7# This module should be kept compatible with Python 2.1.
8
Greg Ward3ce77fd2000-03-02 01:49:45 +00009__revision__ = "$Id$"
Greg Ward13ae1c81999-03-22 14:55:25 +000010
Tarek Ziadédd7bef92010-03-05 00:16:02 +000011import sys, os, string
12from types import *
Jeremy Hyltonfcd73532002-09-11 16:31:53 +000013from distutils.core import Command
14from distutils.debug import DEBUG
Tarek Ziadédd7bef92010-03-05 00:16:02 +000015from distutils.sysconfig import get_config_vars
Andrew M. Kuchling665f2482001-08-10 18:59:59 +000016from distutils.errors import DistutilsPlatformError
Greg Wardab3a0f32000-08-05 01:31:54 +000017from distutils.file_util import write_file
Tarek Ziadédd7bef92010-03-05 00:16:02 +000018from distutils.util import convert_path, subst_vars, change_root
19from distutils.util import get_platform
Greg Ward4f08e4f2000-02-26 00:49:04 +000020from distutils.errors import DistutilsOptionError
Tarek Ziadédd7bef92010-03-05 00:16:02 +000021from site import USER_BASE
22from site import USER_SITE
Christian Heimesaf748c32008-05-06 22:41:46 +000023
Tarek Ziadédd7bef92010-03-05 00:16:02 +000024
Tarek Ziadéb8c7b012010-02-03 15:38:12 +000025if sys.version < "2.2":
26 WINDOWS_SCHEME = {
27 'purelib': '$base',
28 'platlib': '$base',
29 'headers': '$base/Include/$dist_name',
30 'scripts': '$base/Scripts',
31 'data' : '$base',
32 }
33else:
34 WINDOWS_SCHEME = {
35 'purelib': '$base/Lib/site-packages',
36 'platlib': '$base/Lib/site-packages',
37 'headers': '$base/Include/$dist_name',
38 'scripts': '$base/Scripts',
39 'data' : '$base',
40 }
41
42INSTALL_SCHEMES = {
43 'unix_prefix': {
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',
48 'data' : '$base',
49 },
50 'unix_home': {
51 'purelib': '$base/lib/python',
52 'platlib': '$base/lib/python',
53 'headers': '$base/include/python/$dist_name',
54 'scripts': '$base/bin',
55 'data' : '$base',
56 },
57 'unix_user': {
58 'purelib': '$usersite',
59 'platlib': '$usersite',
60 'headers': '$userbase/include/python$py_version_short/$dist_name',
61 'scripts': '$userbase/bin',
62 'data' : '$userbase',
63 },
64 'nt': WINDOWS_SCHEME,
65 'nt_user': {
66 'purelib': '$usersite',
67 'platlib': '$usersite',
68 'headers': '$userbase/Python$py_version_nodot/Include/$dist_name',
69 'scripts': '$userbase/Scripts',
70 'data' : '$userbase',
71 },
Tarek Ziadéb8c7b012010-02-03 15:38:12 +000072 'os2': {
73 'purelib': '$base/Lib/site-packages',
74 'platlib': '$base/Lib/site-packages',
75 'headers': '$base/Include/$dist_name',
76 'scripts': '$base/Scripts',
77 'data' : '$base',
78 },
79 'os2_home': {
80 'purelib': '$usersite',
81 'platlib': '$usersite',
82 'headers': '$userbase/include/python$py_version_short/$dist_name',
83 'scripts': '$userbase/bin',
84 'data' : '$userbase',
85 },
86 }
87
Tarek Ziadédd7bef92010-03-05 00:16:02 +000088# 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.
Tarek Ziadéb8c7b012010-02-03 15:38:12 +000091SCHEME_KEYS = ('purelib', 'platlib', 'headers', 'scripts', 'data')
Tarek Ziadéb8c7b012010-02-03 15:38:12 +000092
Greg Warda233d862000-03-22 00:15:45 +000093
Tarek Ziadédd7bef92010-03-05 00:16:02 +000094class install (Command):
Greg Ward13ae1c81999-03-22 14:55:25 +000095
Greg Ward37bc8152000-01-30 18:34:15 +000096 description = "install everything from build directory"
97
Greg Wardbbeceea2000-02-18 00:25:39 +000098 user_options = [
Greg Warda233d862000-03-22 00:15:45 +000099 # Select installation scheme and set base director(y|ies)
100 ('prefix=', None,
101 "installation prefix"),
Greg Wardbbeceea2000-02-18 00:25:39 +0000102 ('exec-prefix=', None,
Greg Warda233d862000-03-22 00:15:45 +0000103 "(Unix only) prefix for platform-specific files"),
104 ('home=', None,
105 "(Unix only) home directory to install under"),
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000106 ('user', None,
107 "install in user site-package '%s'" % USER_SITE),
Greg Ward13ae1c81999-03-22 14:55:25 +0000108
Greg Warda233d862000-03-22 00:15:45 +0000109 # 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)"),
Greg Ward6a647bb2000-04-27 01:56:38 +0000115 ('root=', None,
116 "install everything relative to this alternate root directory"),
Greg Warda233d862000-03-22 00:15:45 +0000117
118 # Or, explicitly set the installation scheme
119 ('install-purelib=', None,
120 "installation directory for pure Python module distributions"),
Greg Ward4f08e4f2000-02-26 00:49:04 +0000121 ('install-platlib=', None,
Greg Warda233d862000-03-22 00:15:45 +0000122 "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)"),
126
Greg Wardcae5a1f2000-05-27 01:33:12 +0000127 ('install-headers=', None,
128 "installation directory for C/C++ headers"),
Greg Warda233d862000-03-22 00:15:45 +0000129 ('install-scripts=', None,
130 "installation directory for Python scripts"),
131 ('install-data=', None,
132 "installation directory for data files"),
Greg Ward4f08e4f2000-02-26 00:49:04 +0000133
Greg Ward81610222000-10-02 02:16:04 +0000134 # 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"),
139 ('optimize=', 'O',
140 "also compile with optimization: -O1 for \"python -O\", "
141 "-O2 for \"python -OO\", and -O0 to disable [default: -O0]"),
Fred Drake21d45352001-12-06 21:01:19 +0000142
Greg Ward3a0310a2000-09-13 01:02:25 +0000143 # Miscellaneous control options
144 ('force', 'f',
145 "force installation (overwrite any existing files)"),
Gregory P. Smith74ead8f2000-05-12 01:46:47 +0000146 ('skip-build', None,
147 "skip rebuilding everything (for testing/debugging)"),
148
Greg Wardbbeceea2000-02-18 00:25:39 +0000149 # Where to install documentation (eventually!)
Greg Ward4f08e4f2000-02-26 00:49:04 +0000150 #('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"),
Greg Ward13ae1c81999-03-22 14:55:25 +0000154
Gregory P. Smith0ec8ef12000-05-13 02:16:45 +0000155 ('record=', None,
156 "filename in which to record list of installed files"),
Gregory P. Smith8cb67612000-05-13 01:58:19 +0000157 ]
Greg Ward9a337071999-06-08 02:04:36 +0000158
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000159 boolean_options = ['compile', 'force', 'skip-build', 'user']
Greg Warda9d37832000-10-03 03:31:52 +0000160 negative_opt = {'no-compile' : 'compile'}
Greg Ward99b032e2000-09-25 01:41:15 +0000161
Greg Wardee94c572000-03-29 02:15:57 +0000162
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000163 def initialize_options (self):
164
Greg Ward790c1102000-03-22 00:51:18 +0000165 # High-level options: these select both an installation base
166 # and scheme.
Greg Ward13ae1c81999-03-22 14:55:25 +0000167 self.prefix = None
168 self.exec_prefix = None
Greg Warda233d862000-03-22 00:15:45 +0000169 self.home = None
Christian Heimesaf748c32008-05-06 22:41:46 +0000170 self.user = 0
Greg Warda233d862000-03-22 00:15:45 +0000171
Greg Ward790c1102000-03-22 00:51:18 +0000172 # 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).
Greg Warda233d862000-03-22 00:15:45 +0000175 self.install_base = None
176 self.install_platbase = None
Greg Ward6a647bb2000-04-27 01:56:38 +0000177 self.root = None
Greg Ward13ae1c81999-03-22 14:55:25 +0000178
Greg Ward790c1102000-03-22 00:51:18 +0000179 # 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)
Greg Wardcae5a1f2000-05-27 01:33:12 +0000185 self.install_headers = None # for C/C++ headers
Greg Ward790c1102000-03-22 00:51:18 +0000186 self.install_lib = None # set to either purelib or platlib
Greg Warda233d862000-03-22 00:15:45 +0000187 self.install_scripts = None
188 self.install_data = None
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000189 self.install_userbase = USER_BASE
190 self.install_usersite = USER_SITE
Greg Ward13ae1c81999-03-22 14:55:25 +0000191
Andrew M. Kuchlingb26ca9d2002-03-22 15:35:17 +0000192 self.compile = None
Greg Ward81610222000-10-02 02:16:04 +0000193 self.optimize = None
194
Greg Ward790c1102000-03-22 00:51:18 +0000195 # These two are for putting non-packagized distributions into their
196 # own directory and creating a .pth file if it makes sense.
Greg Ward379a02f2000-09-22 01:31:08 +0000197 # '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
202 # with it.
Greg Warda233d862000-03-22 00:15:45 +0000203 self.extra_path = None
Greg Ward379a02f2000-09-22 01:31:08 +0000204 self.install_path_file = 1
Greg Warda233d862000-03-22 00:15:45 +0000205
Greg Ward379a02f2000-09-22 01:31:08 +0000206 # 'force' forces installation, even if target files are not
207 # out-of-date. 'skip_build' skips running the "build" command,
Anthony Baxterf2113f02004-10-13 12:35:28 +0000208 # handy if you know it's not necessary. 'warn_dir' (which is *not*
Greg Ward379a02f2000-09-22 01:31:08 +0000209 # 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.
Greg Ward3a0310a2000-09-13 01:02:25 +0000212 self.force = 0
Gregory P. Smith74ead8f2000-05-12 01:46:47 +0000213 self.skip_build = 0
Greg Ward379a02f2000-09-22 01:31:08 +0000214 self.warn_dir = 1
Gregory P. Smith74ead8f2000-05-12 01:46:47 +0000215
Greg Ward066af102000-03-22 00:30:54 +0000216 # 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
221 # build command.
222 self.build_base = None
223 self.build_lib = None
224
Greg Ward790c1102000-03-22 00:51:18 +0000225 # Not defined yet because we don't know anything about
226 # documentation yet.
Greg Warda233d862000-03-22 00:15:45 +0000227 #self.install_man = None
228 #self.install_html = None
229 #self.install_info = None
Greg Ward13ae1c81999-03-22 14:55:25 +0000230
Gregory P. Smith8cb67612000-05-13 01:58:19 +0000231 self.record = None
Greg Ward13ae1c81999-03-22 14:55:25 +0000232
Greg Ward70b1fd12000-09-30 17:05:37 +0000233
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!)
239
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000240 def finalize_options (self):
241
Greg Ward790c1102000-03-22 00:51:18 +0000242 # 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
251 # here.
Greg Ward9a337071999-06-08 02:04:36 +0000252
Greg Ward790c1102000-03-22 00:51:18 +0000253 # Check for errors/inconsistencies in the options; first, stuff
254 # that's wrong on any platform.
Greg Ward13ae1c81999-03-22 14:55:25 +0000255
Greg Warda233d862000-03-22 00:15:45 +0000256 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")
Greg Ward4f08e4f2000-02-26 00:49:04 +0000261
Fred Drakeec6229e2004-06-25 23:02:59 +0000262 if self.home and (self.prefix or self.exec_prefix):
263 raise DistutilsOptionError, \
264 "must supply either home or prefix/exec-prefix -- not both"
265
Christian Heimesaf748c32008-05-06 22:41:46 +0000266 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")
270
Greg Ward790c1102000-03-22 00:51:18 +0000271 # Next, stuff that's wrong (or dubious) only on certain platforms.
Fred Drakeec6229e2004-06-25 23:02:59 +0000272 if os.name != "posix":
Greg Warda233d862000-03-22 00:15:45 +0000273 if self.exec_prefix:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000274 self.warn("exec-prefix option ignored on this platform")
Greg Warda233d862000-03-22 00:15:45 +0000275 self.exec_prefix = None
Greg Ward865de831999-09-21 18:31:14 +0000276
Greg Warda233d862000-03-22 00:15:45 +0000277 # 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!
Greg Ward865de831999-09-21 18:31:14 +0000284
Greg Wardcb1f4c42000-09-30 18:27:54 +0000285 self.dump_dirs("pre-finalize_{unix,other}")
Greg Wardff2d9b72000-04-26 02:38:01 +0000286
Greg Warda233d862000-03-22 00:15:45 +0000287 if os.name == 'posix':
Greg Wardcb1f4c42000-09-30 18:27:54 +0000288 self.finalize_unix()
Greg Warda233d862000-03-22 00:15:45 +0000289 else:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000290 self.finalize_other()
Greg Warda233d862000-03-22 00:15:45 +0000291
Greg Wardcb1f4c42000-09-30 18:27:54 +0000292 self.dump_dirs("post-finalize_{unix,other}()")
Greg Wardff2d9b72000-04-26 02:38:01 +0000293
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).
298
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000299 py_version = (string.split(sys.version))[0]
300 (prefix, exec_prefix) = get_config_vars('prefix', 'exec_prefix')
Greg Wardcae5a1f2000-05-27 01:33:12 +0000301 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],
Christian Heimesaf748c32008-05-06 22:41:46 +0000306 'py_version_nodot': py_version[0] + py_version[2],
Greg Ward9bd3e9b2000-09-15 01:20:10 +0000307 'sys_prefix': prefix,
308 'prefix': prefix,
309 'sys_exec_prefix': exec_prefix,
310 'exec_prefix': exec_prefix,
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000311 'userbase': self.install_userbase,
312 'usersite': self.install_usersite,
Greg Wardff2d9b72000-04-26 02:38:01 +0000313 }
Greg Wardcb1f4c42000-09-30 18:27:54 +0000314 self.expand_basedirs()
Greg Wardff2d9b72000-04-26 02:38:01 +0000315
Greg Wardcb1f4c42000-09-30 18:27:54 +0000316 self.dump_dirs("post-expand_basedirs()")
Greg Wardff2d9b72000-04-26 02:38:01 +0000317
318 # Now define config vars for the base directories so we can expand
319 # everything else.
320 self.config_vars['base'] = self.install_base
321 self.config_vars['platbase'] = self.install_platbase
322
Greg Wardfbb04c42000-05-28 23:47:31 +0000323 if DEBUG:
324 from pprint import pprint
325 print "config vars:"
Greg Wardcb1f4c42000-09-30 18:27:54 +0000326 pprint(self.config_vars)
Greg Wardff2d9b72000-04-26 02:38:01 +0000327
Greg Warda233d862000-03-22 00:15:45 +0000328 # Expand "~" and configuration variables in the installation
329 # directories.
Greg Wardcb1f4c42000-09-30 18:27:54 +0000330 self.expand_dirs()
Greg Warda233d862000-03-22 00:15:45 +0000331
Greg Wardcb1f4c42000-09-30 18:27:54 +0000332 self.dump_dirs("post-expand_dirs()")
Greg Wardff2d9b72000-04-26 02:38:01 +0000333
Christian Heimesaf748c32008-05-06 22:41:46 +0000334 # Create directories in the home dir:
335 if self.user:
336 self.create_home_path()
337
Greg Warda233d862000-03-22 00:15:45 +0000338 # 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
345 else:
346 self.install_lib = self.install_purelib
Fred Drake21d45352001-12-06 21:01:19 +0000347
Greg Ward379a02f2000-09-22 01:31:08 +0000348
349 # Convert directories from Unix /-separated syntax to the local
350 # convention.
351 self.convert_paths('lib', 'purelib', 'platlib',
Christian Heimesaf748c32008-05-06 22:41:46 +0000352 'scripts', 'data', 'headers',
353 'userbase', 'usersite')
Greg Ward379a02f2000-09-22 01:31:08 +0000354
Greg Warda233d862000-03-22 00:15:45 +0000355 # 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.
Greg Wardcb1f4c42000-09-30 18:27:54 +0000359 self.handle_extra_path()
Greg Warda233d862000-03-22 00:15:45 +0000360 self.install_libbase = self.install_lib # needed for .pth file
Greg Wardcb1f4c42000-09-30 18:27:54 +0000361 self.install_lib = os.path.join(self.install_lib, self.extra_dirs)
Greg Ward865de831999-09-21 18:31:14 +0000362
Greg Ward6a647bb2000-04-27 01:56:38 +0000363 # If a new root directory was supplied, make all the installation
364 # dirs relative to it.
365 if self.root is not None:
Greg Ward379a02f2000-09-22 01:31:08 +0000366 self.change_roots('libbase', 'lib', 'purelib', 'platlib',
367 'scripts', 'data', 'headers')
Greg Ward6a647bb2000-04-27 01:56:38 +0000368
Greg Wardcb1f4c42000-09-30 18:27:54 +0000369 self.dump_dirs("after prepending root")
Greg Ward6a647bb2000-04-27 01:56:38 +0000370
Greg Ward790c1102000-03-22 00:51:18 +0000371 # Find out the build directories, ie. where to install from.
Greg Wardcb1f4c42000-09-30 18:27:54 +0000372 self.set_undefined_options('build',
373 ('build_base', 'build_base'),
374 ('build_lib', 'build_lib'))
Greg Ward13ae1c81999-03-22 14:55:25 +0000375
376 # Punt on doc directories for now -- after all, we're punting on
377 # documentation completely!
378
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000379 # finalize_options ()
Greg Ward13ae1c81999-03-22 14:55:25 +0000380
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000381
382 def dump_dirs (self, msg):
383 if DEBUG:
384 from distutils.fancy_getopt import longopt_xlate
385 print msg + ":"
386 for opt in self.user_options:
387 opt_name = opt[0]
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],
392 longopt_xlate)
393 val = not getattr(self, opt_name)
394 else:
395 opt_name = string.translate(opt_name, longopt_xlate)
396 val = getattr(self, opt_name)
397 print " %s: %s" % (opt_name, val)
398
399
400 def finalize_unix (self):
401
Greg Warda233d862000-03-22 00:15:45 +0000402 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
Greg Wardcae5a1f2000-05-27 01:33:12 +0000406 self.install_headers is None or
Greg Warda233d862000-03-22 00:15:45 +0000407 self.install_scripts is None or
408 self.install_data is None):
409 raise DistutilsOptionError, \
Jeremy Hyltondc8412e2002-06-04 21:20:08 +0000410 ("install-base or install-platbase supplied, but "
411 "installation scheme is incomplete")
Greg Warda233d862000-03-22 00:15:45 +0000412 return
413
Christian Heimesaf748c32008-05-06 22:41:46 +0000414 if self.user:
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
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000419 self.select_scheme("unix_user")
Christian Heimesaf748c32008-05-06 22:41:46 +0000420 elif self.home is not None:
Greg Warda233d862000-03-22 00:15:45 +0000421 self.install_base = self.install_platbase = self.home
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000422 self.select_scheme("unix_home")
Greg Warda233d862000-03-22 00:15:45 +0000423 else:
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"
428
Greg Wardcb1f4c42000-09-30 18:27:54 +0000429 self.prefix = os.path.normpath(sys.prefix)
430 self.exec_prefix = os.path.normpath(sys.exec_prefix)
Greg Warda233d862000-03-22 00:15:45 +0000431
432 else:
433 if self.exec_prefix is None:
434 self.exec_prefix = self.prefix
435
Greg Warda233d862000-03-22 00:15:45 +0000436 self.install_base = self.prefix
437 self.install_platbase = self.exec_prefix
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000438 self.select_scheme("unix_prefix")
Greg Warda233d862000-03-22 00:15:45 +0000439
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000440 # finalize_unix ()
441
442
443 def finalize_other (self): # Windows and Mac OS for now
444
Christian Heimesaf748c32008-05-06 22:41:46 +0000445 if self.user:
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:
Fred Drakeec6229e2004-06-25 23:02:59 +0000452 self.install_base = self.install_platbase = self.home
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000453 self.select_scheme("unix_home")
Fred Drakeec6229e2004-06-25 23:02:59 +0000454 else:
455 if self.prefix is None:
456 self.prefix = os.path.normpath(sys.prefix)
Greg Warda233d862000-03-22 00:15:45 +0000457
Fred Drakeec6229e2004-06-25 23:02:59 +0000458 self.install_base = self.install_platbase = self.prefix
459 try:
460 self.select_scheme(os.name)
461 except KeyError:
462 raise DistutilsPlatformError, \
463 "I don't know how to install stuff on '%s'" % os.name
Greg Warda233d862000-03-22 00:15:45 +0000464
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000465 # finalize_other ()
Greg Warda233d862000-03-22 00:15:45 +0000466
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000467
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])
475
476
477 def _expand_attrs (self, attrs):
Greg Wardff2d9b72000-04-26 02:38:01 +0000478 for attr in attrs:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000479 val = getattr(self, attr)
Greg Wardff2d9b72000-04-26 02:38:01 +0000480 if val is not None:
Christian Heimesaf748c32008-05-06 22:41:46 +0000481 if os.name == 'posix' or os.name == 'nt':
Greg Wardcb1f4c42000-09-30 18:27:54 +0000482 val = os.path.expanduser(val)
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000483 val = subst_vars(val, self.config_vars)
Greg Wardcb1f4c42000-09-30 18:27:54 +0000484 setattr(self, attr, val)
Greg Wardff2d9b72000-04-26 02:38:01 +0000485
486
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000487 def expand_basedirs (self):
488 self._expand_attrs(['install_base',
489 'install_platbase',
490 'root'])
Greg Wardff2d9b72000-04-26 02:38:01 +0000491
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000492 def expand_dirs (self):
493 self._expand_attrs(['install_purelib',
494 'install_platlib',
495 'install_lib',
496 'install_headers',
497 'install_scripts',
498 'install_data',])
499
500
501 def convert_paths (self, *names):
Greg Ward379a02f2000-09-22 01:31:08 +0000502 for name in names:
503 attr = "install_" + name
504 setattr(self, attr, convert_path(getattr(self, attr)))
505
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000506
507 def handle_extra_path (self):
508
Greg Warda233d862000-03-22 00:15:45 +0000509 if self.extra_path is None:
510 self.extra_path = self.distribution.extra_path
511
512 if self.extra_path is not None:
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000513 if type(self.extra_path) is StringType:
514 self.extra_path = string.split(self.extra_path, ',')
Greg Warda233d862000-03-22 00:15:45 +0000515
Greg Wardcb1f4c42000-09-30 18:27:54 +0000516 if len(self.extra_path) == 1:
Greg Warda233d862000-03-22 00:15:45 +0000517 path_file = extra_dirs = self.extra_path[0]
Greg Wardcb1f4c42000-09-30 18:27:54 +0000518 elif len(self.extra_path) == 2:
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000519 (path_file, extra_dirs) = self.extra_path
Greg Warda233d862000-03-22 00:15:45 +0000520 else:
521 raise DistutilsOptionError, \
Jeremy Hyltondc8412e2002-06-04 21:20:08 +0000522 ("'extra_path' option must be a list, tuple, or "
523 "comma-separated string with 1 or 2 elements")
Greg Warda233d862000-03-22 00:15:45 +0000524
525 # convert to local form in case Unix notation used (as it
526 # should be in setup scripts)
Greg Wardcb1f4c42000-09-30 18:27:54 +0000527 extra_dirs = convert_path(extra_dirs)
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000528
Greg Warda233d862000-03-22 00:15:45 +0000529 else:
530 path_file = None
531 extra_dirs = ''
532
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
537
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000538 # handle_extra_path ()
539
540
541 def change_roots (self, *names):
Greg Ward379a02f2000-09-22 01:31:08 +0000542 for name in names:
543 attr = "install_" + name
544 setattr(self, attr, change_root(self.root, getattr(self, attr)))
545
Christian Heimesaf748c32008-05-06 22:41:46 +0000546 def create_home_path(self):
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000547 """Create directories under ~
548 """
Christian Heimesaf748c32008-05-06 22:41:46 +0000549 if not self.user:
550 return
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)
Greg Ward379a02f2000-09-22 01:31:08 +0000556
Greg Ward70b1fd12000-09-30 17:05:37 +0000557 # -- Command execution methods -------------------------------------
558
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000559 def run (self):
560
Greg Ward9a337071999-06-08 02:04:36 +0000561 # Obviously have to build before we can install
Gregory P. Smith74ead8f2000-05-12 01:46:47 +0000562 if not self.skip_build:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000563 self.run_command('build')
Mark Hammond495cf992008-04-07 01:53:39 +0000564 # 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 "
571 "cross-compiling")
Greg Ward9a337071999-06-08 02:04:36 +0000572
Greg Warda4adafd2000-05-20 15:17:09 +0000573 # Run all sub-commands (at least those that need to be run)
574 for cmd_name in self.get_sub_commands():
Greg Wardcb1f4c42000-09-30 18:27:54 +0000575 self.run_command(cmd_name)
Greg Ward865de831999-09-21 18:31:14 +0000576
577 if self.path_file:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000578 self.create_path_file()
Greg Ward13ae1c81999-03-22 14:55:25 +0000579
Gregory P. Smith8cb67612000-05-13 01:58:19 +0000580 # write list of installed files, if requested.
581 if self.record:
582 outputs = self.get_outputs()
Gregory P. Smith7e855ef2000-05-13 02:13:53 +0000583 if self.root: # strip any package prefix
Gregory P. Smith8cb67612000-05-13 01:58:19 +0000584 root_len = len(self.root)
Greg Wardcb1f4c42000-09-30 18:27:54 +0000585 for counter in xrange(len(outputs)):
Gregory P. Smith8cb67612000-05-13 01:58:19 +0000586 outputs[counter] = outputs[counter][root_len:]
587 self.execute(write_file,
Gregory P. Smith0ec8ef12000-05-13 02:16:45 +0000588 (self.record, outputs),
589 "writing list of installed files to '%s'" %
590 self.record)
Gregory P. Smithf9ebf982000-05-13 02:01:22 +0000591
Greg Ward0b4dafc2000-10-14 03:47:07 +0000592 sys_path = map(os.path.normpath, sys.path)
Greg Wardfa9ff762000-10-14 04:06:40 +0000593 sys_path = map(os.path.normcase, sys_path)
Greg Ward0b4dafc2000-10-14 03:47:07 +0000594 install_lib = os.path.normcase(os.path.normpath(self.install_lib))
Greg Ward379a02f2000-09-22 01:31:08 +0000595 if (self.warn_dir and
596 not (self.path_file and self.install_path_file) and
Greg Ward0b4dafc2000-10-14 03:47:07 +0000597 install_lib not in sys_path):
Jeremy Hyltondc8412e2002-06-04 21:20:08 +0000598 log.debug(("modules installed to '%s', which is not in "
Tim Peters182b5ac2004-07-18 06:16:08 +0000599 "Python's module search path (sys.path) -- "
Jeremy Hyltondc8412e2002-06-04 21:20:08 +0000600 "you'll have to change the search path yourself"),
601 self.install_lib)
Gregory P. Smith8cb67612000-05-13 01:58:19 +0000602
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000603 # run ()
604
605 def create_path_file (self):
Greg Wardcb1f4c42000-09-30 18:27:54 +0000606 filename = os.path.join(self.install_libbase,
607 self.path_file + ".pth")
Greg Ward70b1fd12000-09-30 17:05:37 +0000608 if self.install_path_file:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000609 self.execute(write_file,
610 (filename, [self.extra_dirs]),
611 "creating %s" % filename)
Greg Ward70b1fd12000-09-30 17:05:37 +0000612 else:
613 self.warn("path file '%s' not created" % filename)
614
615
616 # -- Reporting methods ---------------------------------------------
617
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000618 def get_outputs (self):
619 # Assemble the outputs of all the sub-commands.
Greg Ward70b1fd12000-09-30 17:05:37 +0000620 outputs = []
621 for cmd_name in self.get_sub_commands():
Greg Wardcb1f4c42000-09-30 18:27:54 +0000622 cmd = self.get_finalized_command(cmd_name)
Andrew M. Kuchling97102972001-01-16 16:16:03 +0000623 # 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)
Greg Ward70b1fd12000-09-30 17:05:37 +0000628
Andrew M. Kuchlinga8ea5ba2001-09-04 20:06:43 +0000629 if self.path_file and self.install_path_file:
630 outputs.append(os.path.join(self.install_libbase,
631 self.path_file + ".pth"))
Fred Drake21d45352001-12-06 21:01:19 +0000632
Greg Ward70b1fd12000-09-30 17:05:37 +0000633 return outputs
634
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000635 def get_inputs (self):
Greg Ward70b1fd12000-09-30 17:05:37 +0000636 # XXX gee, this looks familiar ;-(
637 inputs = []
638 for cmd_name in self.get_sub_commands():
Greg Wardcb1f4c42000-09-30 18:27:54 +0000639 cmd = self.get_finalized_command(cmd_name)
640 inputs.extend(cmd.get_inputs())
Greg Ward70b1fd12000-09-30 17:05:37 +0000641
642 return inputs
643
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000644
Greg Wardfadefed2000-09-16 15:06:57 +0000645 # -- Predicates for sub-command list -------------------------------
646
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000647 def has_lib (self):
648 """Return true if the current distribution has any Python
Greg Warda4adafd2000-05-20 15:17:09 +0000649 modules to install."""
650 return (self.distribution.has_pure_modules() or
651 self.distribution.has_ext_modules())
652
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000653 def has_headers (self):
Greg Wardcae5a1f2000-05-27 01:33:12 +0000654 return self.distribution.has_headers()
655
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000656 def has_scripts (self):
Greg Warda4adafd2000-05-20 15:17:09 +0000657 return self.distribution.has_scripts()
658
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000659 def has_data (self):
Greg Warda4adafd2000-05-20 15:17:09 +0000660 return self.distribution.has_data_files()
661
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000662
Greg Wardfadefed2000-09-16 15:06:57 +0000663 # '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),
Phillip J. Eby02e19972006-03-27 21:55:21 +0000669 ('install_egg_info', lambda self:True),
Greg Wardfadefed2000-09-16 15:06:57 +0000670 ]
Tarek Ziadédd7bef92010-03-05 00:16:02 +0000671
672# class install