blob: ba4110cb8aa189b7140f3fe1a1c25ae3d85e5baf [file] [log] [blame]
Greg Ward13ae1c81999-03-22 14:55:25 +00001"""distutils.command.install
2
3Implements the Distutils 'install' command."""
4
5# created 1999/03/13, Greg Ward
6
Greg Ward3ce77fd2000-03-02 01:49:45 +00007__revision__ = "$Id$"
Greg Ward13ae1c81999-03-22 14:55:25 +00008
9import sys, os, string
Greg Ward865de831999-09-21 18:31:14 +000010from types import *
Greg Ward13ae1c81999-03-22 14:55:25 +000011from distutils.core import Command
Greg Wardff2d9b72000-04-26 02:38:01 +000012from distutils import sysconfig
Greg Ward6a647bb2000-04-27 01:56:38 +000013from distutils.util import write_file, native_path, subst_vars, change_root
Greg Ward4f08e4f2000-02-26 00:49:04 +000014from distutils.errors import DistutilsOptionError
Greg Ward13ae1c81999-03-22 14:55:25 +000015
Greg Warda233d862000-03-22 00:15:45 +000016INSTALL_SCHEMES = {
17 'unix_prefix': {
18 'purelib': '$base/lib/python$py_version_short/site-packages',
19 'platlib': '$platbase/lib/python$py_version_short/site-packages',
20 'scripts': '$base/bin',
21 'data' : '$base/share',
22 },
23 'unix_home': {
24 'purelib': '$base/lib/python',
25 'platlib': '$base/lib/python',
26 'scripts': '$base/bin',
27 'data' : '$base/share',
28 },
29 'nt': {
30 'purelib': '$base',
31 'platlib': '$base',
32 'scripts': '$base\\Scripts',
33 'data' : '$base\\Data',
34 },
35 'mac': {
36 'purelib': '$base:Lib',
37 'platlib': '$base:Mac:PlugIns',
38 'scripts': '$base:Scripts',
39 'data' : '$base:Data',
40 }
41 }
42
43
Greg Ward1993f9a2000-02-18 00:13:53 +000044class install (Command):
Greg Ward13ae1c81999-03-22 14:55:25 +000045
Greg Ward37bc8152000-01-30 18:34:15 +000046 description = "install everything from build directory"
47
Greg Wardbbeceea2000-02-18 00:25:39 +000048 user_options = [
Greg Warda233d862000-03-22 00:15:45 +000049 # Select installation scheme and set base director(y|ies)
50 ('prefix=', None,
51 "installation prefix"),
Greg Wardbbeceea2000-02-18 00:25:39 +000052 ('exec-prefix=', None,
Greg Warda233d862000-03-22 00:15:45 +000053 "(Unix only) prefix for platform-specific files"),
54 ('home=', None,
55 "(Unix only) home directory to install under"),
Greg Ward13ae1c81999-03-22 14:55:25 +000056
Greg Warda233d862000-03-22 00:15:45 +000057 # Or, just set the base director(y|ies)
58 ('install-base=', None,
59 "base installation directory (instead of --prefix or --home)"),
60 ('install-platbase=', None,
61 "base installation directory for platform-specific files " +
62 "(instead of --exec-prefix or --home)"),
Greg Ward6a647bb2000-04-27 01:56:38 +000063 ('root=', None,
64 "install everything relative to this alternate root directory"),
Greg Warda233d862000-03-22 00:15:45 +000065
66 # Or, explicitly set the installation scheme
67 ('install-purelib=', None,
68 "installation directory for pure Python module distributions"),
Greg Ward4f08e4f2000-02-26 00:49:04 +000069 ('install-platlib=', None,
Greg Warda233d862000-03-22 00:15:45 +000070 "installation directory for non-pure module distributions"),
71 ('install-lib=', None,
72 "installation directory for all module distributions " +
73 "(overrides --install-purelib and --install-platlib)"),
74
75 ('install-scripts=', None,
76 "installation directory for Python scripts"),
77 ('install-data=', None,
78 "installation directory for data files"),
Greg Ward4f08e4f2000-02-26 00:49:04 +000079
Gregory P. Smith74ead8f2000-05-12 01:46:47 +000080 # For lazy debuggers who just want to test the install
81 # commands without rerunning "build" all the time
82 ('skip-build', None,
83 "skip rebuilding everything (for testing/debugging)"),
84
Greg Wardbbeceea2000-02-18 00:25:39 +000085 # Where to install documentation (eventually!)
Greg Ward4f08e4f2000-02-26 00:49:04 +000086 #('doc-format=', None, "format of documentation to generate"),
87 #('install-man=', None, "directory for Unix man pages"),
88 #('install-html=', None, "directory for HTML documentation"),
89 #('install-info=', None, "directory for GNU info files"),
Greg Wardbbeceea2000-02-18 00:25:39 +000090 ]
Greg Ward13ae1c81999-03-22 14:55:25 +000091
Greg Ward9a337071999-06-08 02:04:36 +000092
Greg Wardee94c572000-03-29 02:15:57 +000093 # 'sub_commands': a list of commands this command might have to run
94 # to get its work done. Each command is represented as a tuple
95 # (func, command) where 'func' is a function to call that returns
96 # true if 'command' (the sub-command name, a string) needs to be
97 # run. If 'func' is None, assume that 'command' must always be run.
Gregory P. Smithb2e3bb32000-05-12 00:52:23 +000098 sub_commands = [(None, 'install_lib'),
99 (None, 'install_scripts'),
100 (None, 'install_data'),
101 ]
Greg Wardee94c572000-03-29 02:15:57 +0000102
103
Greg Warde01149c2000-02-18 00:35:22 +0000104 def initialize_options (self):
Greg Ward13ae1c81999-03-22 14:55:25 +0000105
Greg Ward790c1102000-03-22 00:51:18 +0000106 # High-level options: these select both an installation base
107 # and scheme.
Greg Ward13ae1c81999-03-22 14:55:25 +0000108 self.prefix = None
109 self.exec_prefix = None
Greg Warda233d862000-03-22 00:15:45 +0000110 self.home = None
111
Greg Ward790c1102000-03-22 00:51:18 +0000112 # These select only the installation base; it's up to the user to
113 # specify the installation scheme (currently, that means supplying
114 # the --install-{platlib,purelib,scripts,data} options).
Greg Warda233d862000-03-22 00:15:45 +0000115 self.install_base = None
116 self.install_platbase = None
Greg Ward6a647bb2000-04-27 01:56:38 +0000117 self.root = None
Greg Ward13ae1c81999-03-22 14:55:25 +0000118
Greg Ward790c1102000-03-22 00:51:18 +0000119 # These options are the actual installation directories; if not
120 # supplied by the user, they are filled in using the installation
121 # scheme implied by prefix/exec-prefix/home and the contents of
122 # that installation scheme.
123 self.install_purelib = None # for pure module distributions
124 self.install_platlib = None # non-pure (dists w/ extensions)
125 self.install_lib = None # set to either purelib or platlib
Greg Warda233d862000-03-22 00:15:45 +0000126 self.install_scripts = None
127 self.install_data = None
Greg Ward13ae1c81999-03-22 14:55:25 +0000128
Greg Ward790c1102000-03-22 00:51:18 +0000129 # These two are for putting non-packagized distributions into their
130 # own directory and creating a .pth file if it makes sense.
131 # 'extra_path' comes from the setup file; 'install_path_file' is
132 # set only if we determine that it makes sense to install a path
133 # file.
Greg Warda233d862000-03-22 00:15:45 +0000134 self.extra_path = None
135 self.install_path_file = 0
136
Gregory P. Smith74ead8f2000-05-12 01:46:47 +0000137 self.skip_build = 0
138
Greg Ward066af102000-03-22 00:30:54 +0000139 # These are only here as a conduit from the 'build' command to the
140 # 'install_*' commands that do the real work. ('build_base' isn't
141 # actually used anywhere, but it might be useful in future.) They
142 # are not user options, because if the user told the install
143 # command where the build directory is, that wouldn't affect the
144 # build command.
145 self.build_base = None
146 self.build_lib = None
147
Greg Ward790c1102000-03-22 00:51:18 +0000148 # Not defined yet because we don't know anything about
149 # documentation yet.
Greg Warda233d862000-03-22 00:15:45 +0000150 #self.install_man = None
151 #self.install_html = None
152 #self.install_info = None
Greg Ward13ae1c81999-03-22 14:55:25 +0000153
Greg Ward13ae1c81999-03-22 14:55:25 +0000154
Greg Warde01149c2000-02-18 00:35:22 +0000155 def finalize_options (self):
Greg Ward13ae1c81999-03-22 14:55:25 +0000156
Greg Ward790c1102000-03-22 00:51:18 +0000157 # This method (and its pliant slaves, like 'finalize_unix()',
158 # 'finalize_other()', and 'select_scheme()') is where the default
159 # installation directories for modules, extension modules, and
160 # anything else we care to install from a Python module
161 # distribution. Thus, this code makes a pretty important policy
162 # statement about how third-party stuff is added to a Python
163 # installation! Note that the actual work of installation is done
164 # by the relatively simple 'install_*' commands; they just take
165 # their orders from the installation directory options determined
166 # here.
Greg Ward9a337071999-06-08 02:04:36 +0000167
Greg Ward790c1102000-03-22 00:51:18 +0000168 # Check for errors/inconsistencies in the options; first, stuff
169 # that's wrong on any platform.
Greg Ward13ae1c81999-03-22 14:55:25 +0000170
Greg Warda233d862000-03-22 00:15:45 +0000171 if ((self.prefix or self.exec_prefix or self.home) and
172 (self.install_base or self.install_platbase)):
173 raise DistutilsOptionError, \
174 ("must supply either prefix/exec-prefix/home or " +
175 "install-base/install-platbase -- not both")
Greg Ward4f08e4f2000-02-26 00:49:04 +0000176
Greg Ward790c1102000-03-22 00:51:18 +0000177 # Next, stuff that's wrong (or dubious) only on certain platforms.
Greg Ward4f08e4f2000-02-26 00:49:04 +0000178 if os.name == 'posix':
Greg Warda233d862000-03-22 00:15:45 +0000179 if self.home and (self.prefix or self.exec_prefix):
Greg Ward865de831999-09-21 18:31:14 +0000180 raise DistutilsOptionError, \
Greg Warda233d862000-03-22 00:15:45 +0000181 ("must supply either home or prefix/exec-prefix -- " +
182 "not both")
Greg Ward865de831999-09-21 18:31:14 +0000183 else:
Greg Warda233d862000-03-22 00:15:45 +0000184 if self.exec_prefix:
185 self.warn ("exec-prefix option ignored on this platform")
186 self.exec_prefix = None
187 if self.home:
188 self.warn ("home option ignored on this platform")
189 self.home = None
Greg Ward865de831999-09-21 18:31:14 +0000190
Greg Warda233d862000-03-22 00:15:45 +0000191 # Now the interesting logic -- so interesting that we farm it out
192 # to other methods. The goal of these methods is to set the final
193 # values for the install_{lib,scripts,data,...} options, using as
194 # input a heady brew of prefix, exec_prefix, home, install_base,
195 # install_platbase, user-supplied versions of
196 # install_{purelib,platlib,lib,scripts,data,...}, and the
197 # INSTALL_SCHEME dictionary above. Phew!
Greg Ward865de831999-09-21 18:31:14 +0000198
Greg Ward6a647bb2000-04-27 01:56:38 +0000199 self.dump_dirs ("pre-finalize_xxx")
Greg Wardff2d9b72000-04-26 02:38:01 +0000200
Greg Warda233d862000-03-22 00:15:45 +0000201 if os.name == 'posix':
202 self.finalize_unix ()
203 else:
204 self.finalize_other ()
205
Greg Ward6a647bb2000-04-27 01:56:38 +0000206 self.dump_dirs ("post-finalize_xxx()")
Greg Wardff2d9b72000-04-26 02:38:01 +0000207
208 # Expand configuration variables, tilde, etc. in self.install_base
209 # and self.install_platbase -- that way, we can use $base or
210 # $platbase in the other installation directories and not worry
211 # about needing recursive variable expansion (shudder).
212
213 self.config_vars = {'py_version_short': sys.version[0:3],
214 'sys_prefix': sysconfig.PREFIX,
215 'sys_exec_prefix': sysconfig.EXEC_PREFIX,
216 }
217 self.expand_basedirs ()
218
Greg Ward6a647bb2000-04-27 01:56:38 +0000219 self.dump_dirs ("post-expand_basedirs()")
Greg Wardff2d9b72000-04-26 02:38:01 +0000220
221 # Now define config vars for the base directories so we can expand
222 # everything else.
223 self.config_vars['base'] = self.install_base
224 self.config_vars['platbase'] = self.install_platbase
225
Greg Ward6a647bb2000-04-27 01:56:38 +0000226 from pprint import pprint
Greg Wardff2d9b72000-04-26 02:38:01 +0000227 print "config vars:"
228 pprint (self.config_vars)
229
Greg Warda233d862000-03-22 00:15:45 +0000230 # Expand "~" and configuration variables in the installation
231 # directories.
232 self.expand_dirs ()
233
Greg Ward6a647bb2000-04-27 01:56:38 +0000234 self.dump_dirs ("post-expand_dirs()")
Greg Wardff2d9b72000-04-26 02:38:01 +0000235
Greg Warda233d862000-03-22 00:15:45 +0000236 # Pick the actual directory to install all modules to: either
237 # install_purelib or install_platlib, depending on whether this
238 # module distribution is pure or not. Of course, if the user
239 # already specified install_lib, use their selection.
240 if self.install_lib is None:
241 if self.distribution.ext_modules: # has extensions: non-pure
242 self.install_lib = self.install_platlib
243 else:
244 self.install_lib = self.install_purelib
245
246 # Well, we're not actually fully completely finalized yet: we still
247 # have to deal with 'extra_path', which is the hack for allowing
248 # non-packagized module distributions (hello, Numerical Python!) to
249 # get their own directories.
250 self.handle_extra_path ()
251 self.install_libbase = self.install_lib # needed for .pth file
252 self.install_lib = os.path.join (self.install_lib, self.extra_dirs)
Greg Ward865de831999-09-21 18:31:14 +0000253
Greg Ward6a647bb2000-04-27 01:56:38 +0000254 # If a new root directory was supplied, make all the installation
255 # dirs relative to it.
256 if self.root is not None:
257 for name in ('lib', 'purelib', 'platlib', 'scripts', 'data'):
258 attr = "install_" + name
259 new_val = change_root (self.root, getattr (self, attr))
260 setattr (self, attr, new_val)
261
262 self.dump_dirs ("after prepending root")
263
Greg Ward790c1102000-03-22 00:51:18 +0000264 # Find out the build directories, ie. where to install from.
Greg Ward4f08e4f2000-02-26 00:49:04 +0000265 self.set_undefined_options ('build',
266 ('build_base', 'build_base'),
Greg Ward066af102000-03-22 00:30:54 +0000267 ('build_lib', 'build_lib'))
Greg Ward13ae1c81999-03-22 14:55:25 +0000268
269 # Punt on doc directories for now -- after all, we're punting on
270 # documentation completely!
271
Greg Warde01149c2000-02-18 00:35:22 +0000272 # finalize_options ()
Greg Ward13ae1c81999-03-22 14:55:25 +0000273
274
Greg Ward6a647bb2000-04-27 01:56:38 +0000275 # hack for debugging output
276 def dump_dirs (self, msg):
277 from distutils.fancy_getopt import longopt_xlate
278 print msg + ":"
279 for opt in self.user_options:
Gregory P. Smith74ead8f2000-05-12 01:46:47 +0000280 opt_name = opt[0]
281 if opt_name[-1] == "=":
282 opt_name = opt_name[0:-1]
283 opt_name = string.translate (opt_name, longopt_xlate)
Greg Ward6a647bb2000-04-27 01:56:38 +0000284 val = getattr (self, opt_name)
285 print " %s: %s" % (opt_name, val)
286
287
Greg Warda233d862000-03-22 00:15:45 +0000288 def finalize_unix (self):
289
290 if self.install_base is not None or self.install_platbase is not None:
291 if ((self.install_lib is None and
292 self.install_purelib is None and
293 self.install_platlib is None) or
294 self.install_scripts is None or
295 self.install_data is None):
296 raise DistutilsOptionError, \
297 "install-base or install-platbase supplied, but " + \
298 "installation scheme is incomplete"
299
300 return
301
302 if self.home is not None:
303 self.install_base = self.install_platbase = self.home
304 self.select_scheme ("unix_home")
305 else:
306 if self.prefix is None:
307 if self.exec_prefix is not None:
308 raise DistutilsOptionError, \
309 "must not supply exec-prefix without prefix"
310
311 self.prefix = os.path.normpath (sys.prefix)
312 self.exec_prefix = os.path.normpath (sys.exec_prefix)
313 self.install_path_file = 1
314
315 else:
316 if self.exec_prefix is None:
317 self.exec_prefix = self.prefix
318
319
320 # XXX since we don't *know* that a user-supplied prefix really
321 # points to another Python installation, we can't be sure that
322 # writing a .pth file there will actually work -- so we don't
323 # try. That is, we only set 'install_path_file' if the user
324 # didn't supply prefix. There are certainly circumstances
325 # under which we *should* install a .pth file when the user
326 # supplies a prefix, namely when that prefix actually points to
327 # another Python installation. Hmmm.
328
329 self.install_base = self.prefix
330 self.install_platbase = self.exec_prefix
331 self.select_scheme ("unix_prefix")
332
333 # finalize_unix ()
334
335
336 def finalize_other (self): # Windows and Mac OS for now
337
338 if self.prefix is None:
339 self.prefix = os.path.normpath (sys.prefix)
340 self.install_path_file = 1
341
342 # XXX same caveat regarding 'install_path_file' as in
343 # 'finalize_unix()'.
344
345 self.install_base = self.install_platbase = self.prefix
346 try:
347 self.select_scheme (os.name)
348 except KeyError:
349 raise DistutilsPlatformError, \
350 "I don't know how to install stuff on '%s'" % os.name
351
352 # finalize_other ()
353
354
355 def select_scheme (self, name):
Greg Warda233d862000-03-22 00:15:45 +0000356 # it's the caller's problem if they supply a bad name!
357 scheme = INSTALL_SCHEMES[name]
Greg Warda233d862000-03-22 00:15:45 +0000358 for key in ('purelib', 'platlib', 'scripts', 'data'):
Gregory P. Smith17f641c2000-05-12 01:54:50 +0000359 attrname = 'install_' + key
360 if getattr(self, attrname) is None:
361 setattr(self, attrname, scheme[key])
Greg Warda233d862000-03-22 00:15:45 +0000362
363
Greg Wardff2d9b72000-04-26 02:38:01 +0000364 def _expand_attrs (self, attrs):
365 for attr in attrs:
366 val = getattr (self, attr)
367 if val is not None:
368 if os.name == 'posix':
369 val = os.path.expanduser (val)
370 val = subst_vars (val, self.config_vars)
371 setattr (self, attr, val)
372
373
374 def expand_basedirs (self):
375 self._expand_attrs (['install_base',
Greg Ward6a647bb2000-04-27 01:56:38 +0000376 'install_platbase',
377 'root'])
Greg Wardff2d9b72000-04-26 02:38:01 +0000378
Greg Warda233d862000-03-22 00:15:45 +0000379 def expand_dirs (self):
Greg Wardff2d9b72000-04-26 02:38:01 +0000380 self._expand_attrs (['install_purelib',
381 'install_platlib',
382 'install_lib',
383 'install_scripts',
384 'install_data',])
Greg Warda233d862000-03-22 00:15:45 +0000385
386
387 def handle_extra_path (self):
388
389 if self.extra_path is None:
390 self.extra_path = self.distribution.extra_path
391
392 if self.extra_path is not None:
393 if type (self.extra_path) is StringType:
394 self.extra_path = string.split (self.extra_path, ',')
395
396 if len (self.extra_path) == 1:
397 path_file = extra_dirs = self.extra_path[0]
398 elif len (self.extra_path) == 2:
399 (path_file, extra_dirs) = self.extra_path
400 else:
401 raise DistutilsOptionError, \
402 "'extra_path' option must be a list, tuple, or " + \
403 "comma-separated string with 1 or 2 elements"
404
405 # convert to local form in case Unix notation used (as it
406 # should be in setup scripts)
407 extra_dirs = native_path (extra_dirs)
408
409 else:
410 path_file = None
411 extra_dirs = ''
412
413 # XXX should we warn if path_file and not extra_dirs? (in which
414 # case the path file would be harmless but pointless)
415 self.path_file = path_file
416 self.extra_dirs = extra_dirs
417
418 # handle_extra_path ()
419
420
Greg Ward13ae1c81999-03-22 14:55:25 +0000421 def run (self):
422
Greg Ward9a337071999-06-08 02:04:36 +0000423 # Obviously have to build before we can install
Gregory P. Smith74ead8f2000-05-12 01:46:47 +0000424 if not self.skip_build:
425 self.run_peer ('build')
Greg Ward9a337071999-06-08 02:04:36 +0000426
Greg Wardee94c572000-03-29 02:15:57 +0000427 # Run all sub-commands: currently this just means install all
428 # Python modules using 'install_lib'.
Greg Wardba38d122000-03-31 02:52:02 +0000429 for (func, cmd_name) in self.sub_commands:
Greg Wardee94c572000-03-29 02:15:57 +0000430 if func is None or func():
Greg Wardba38d122000-03-31 02:52:02 +0000431 self.run_peer (cmd_name)
Greg Ward865de831999-09-21 18:31:14 +0000432
433 if self.path_file:
434 self.create_path_file ()
Greg Ward13ae1c81999-03-22 14:55:25 +0000435
Greg Warda233d862000-03-22 00:15:45 +0000436 normalized_path = map (os.path.normpath, sys.path)
437 if (not (self.path_file and self.install_path_file) and
438 os.path.normpath (self.install_lib) not in normalized_path):
439 self.warn (("modules installed to '%s', which is not in " +
440 "Python's module search path (sys.path) -- " +
441 "you'll have to change the search path yourself") %
442 self.install_lib)
443
Greg Ward13ae1c81999-03-22 14:55:25 +0000444 # run ()
445
Greg Ward865de831999-09-21 18:31:14 +0000446
Greg Wardee94c572000-03-29 02:15:57 +0000447 def get_outputs (self):
448 # This command doesn't have any outputs of its own, so just
449 # get the outputs of all its sub-commands.
450 outputs = []
Greg Wardba38d122000-03-31 02:52:02 +0000451 for (func, cmd_name) in self.sub_commands:
Greg Wardee94c572000-03-29 02:15:57 +0000452 if func is None or func():
Greg Wardba38d122000-03-31 02:52:02 +0000453 cmd = self.find_peer (cmd_name)
454 outputs.extend (cmd.get_outputs())
Greg Wardee94c572000-03-29 02:15:57 +0000455
456 return outputs
457
458
Greg Wardba38d122000-03-31 02:52:02 +0000459 def get_inputs (self):
460 # XXX gee, this looks familiar ;-(
461 inputs = []
462 for (func, cmd_name) in self.sub_commands:
463 if func is None or func():
464 cmd = self.find_peer (cmd_name)
465 inputs.extend (cmd.get_inputs())
466
467 return inputs
468
469
Greg Ward865de831999-09-21 18:31:14 +0000470 def create_path_file (self):
Greg Warda233d862000-03-22 00:15:45 +0000471 filename = os.path.join (self.install_libbase,
472 self.path_file + ".pth")
473 if self.install_path_file:
474 self.execute (write_file,
475 (filename, [self.extra_dirs]),
476 "creating %s" % filename)
Greg Ward865de831999-09-21 18:31:14 +0000477 else:
Greg Warda233d862000-03-22 00:15:45 +0000478 self.warn (("path file '%s' not created for alternate or custom " +
479 "installation (path files only work with standard " +
480 "installations)") %
481 filename)
Greg Ward865de831999-09-21 18:31:14 +0000482
Greg Ward13ae1c81999-03-22 14:55:25 +0000483# class Install