blob: c70ed9a1d122b5c19e85dd38509b35dae6fbbc89 [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
Greg Wardbbeceea2000-02-18 00:25:39 +000080 # Where to install documentation (eventually!)
Greg Ward4f08e4f2000-02-26 00:49:04 +000081 #('doc-format=', None, "format of documentation to generate"),
82 #('install-man=', None, "directory for Unix man pages"),
83 #('install-html=', None, "directory for HTML documentation"),
84 #('install-info=', None, "directory for GNU info files"),
Greg Wardbbeceea2000-02-18 00:25:39 +000085 ]
Greg Ward13ae1c81999-03-22 14:55:25 +000086
Greg Ward9a337071999-06-08 02:04:36 +000087
Greg Wardee94c572000-03-29 02:15:57 +000088 # 'sub_commands': a list of commands this command might have to run
89 # to get its work done. Each command is represented as a tuple
90 # (func, command) where 'func' is a function to call that returns
91 # true if 'command' (the sub-command name, a string) needs to be
92 # run. If 'func' is None, assume that 'command' must always be run.
93 sub_commands = [(None, 'install_lib')]
94
95
Greg Warde01149c2000-02-18 00:35:22 +000096 def initialize_options (self):
Greg Ward13ae1c81999-03-22 14:55:25 +000097
Greg Ward790c1102000-03-22 00:51:18 +000098 # High-level options: these select both an installation base
99 # and scheme.
Greg Ward13ae1c81999-03-22 14:55:25 +0000100 self.prefix = None
101 self.exec_prefix = None
Greg Warda233d862000-03-22 00:15:45 +0000102 self.home = None
103
Greg Ward790c1102000-03-22 00:51:18 +0000104 # These select only the installation base; it's up to the user to
105 # specify the installation scheme (currently, that means supplying
106 # the --install-{platlib,purelib,scripts,data} options).
Greg Warda233d862000-03-22 00:15:45 +0000107 self.install_base = None
108 self.install_platbase = None
Greg Ward6a647bb2000-04-27 01:56:38 +0000109 self.root = None
Greg Ward13ae1c81999-03-22 14:55:25 +0000110
Greg Ward790c1102000-03-22 00:51:18 +0000111 # These options are the actual installation directories; if not
112 # supplied by the user, they are filled in using the installation
113 # scheme implied by prefix/exec-prefix/home and the contents of
114 # that installation scheme.
115 self.install_purelib = None # for pure module distributions
116 self.install_platlib = None # non-pure (dists w/ extensions)
117 self.install_lib = None # set to either purelib or platlib
Greg Warda233d862000-03-22 00:15:45 +0000118 self.install_scripts = None
119 self.install_data = None
Greg Ward13ae1c81999-03-22 14:55:25 +0000120
Greg Ward790c1102000-03-22 00:51:18 +0000121 # These two are for putting non-packagized distributions into their
122 # own directory and creating a .pth file if it makes sense.
123 # 'extra_path' comes from the setup file; 'install_path_file' is
124 # set only if we determine that it makes sense to install a path
125 # file.
Greg Warda233d862000-03-22 00:15:45 +0000126 self.extra_path = None
127 self.install_path_file = 0
128
Greg Ward066af102000-03-22 00:30:54 +0000129 # These are only here as a conduit from the 'build' command to the
130 # 'install_*' commands that do the real work. ('build_base' isn't
131 # actually used anywhere, but it might be useful in future.) They
132 # are not user options, because if the user told the install
133 # command where the build directory is, that wouldn't affect the
134 # build command.
135 self.build_base = None
136 self.build_lib = None
137
Greg Ward790c1102000-03-22 00:51:18 +0000138 # Not defined yet because we don't know anything about
139 # documentation yet.
Greg Warda233d862000-03-22 00:15:45 +0000140 #self.install_man = None
141 #self.install_html = None
142 #self.install_info = None
Greg Ward13ae1c81999-03-22 14:55:25 +0000143
Greg Ward13ae1c81999-03-22 14:55:25 +0000144
Greg Warde01149c2000-02-18 00:35:22 +0000145 def finalize_options (self):
Greg Ward13ae1c81999-03-22 14:55:25 +0000146
Greg Ward790c1102000-03-22 00:51:18 +0000147 # This method (and its pliant slaves, like 'finalize_unix()',
148 # 'finalize_other()', and 'select_scheme()') is where the default
149 # installation directories for modules, extension modules, and
150 # anything else we care to install from a Python module
151 # distribution. Thus, this code makes a pretty important policy
152 # statement about how third-party stuff is added to a Python
153 # installation! Note that the actual work of installation is done
154 # by the relatively simple 'install_*' commands; they just take
155 # their orders from the installation directory options determined
156 # here.
Greg Ward9a337071999-06-08 02:04:36 +0000157
Greg Ward790c1102000-03-22 00:51:18 +0000158 # Check for errors/inconsistencies in the options; first, stuff
159 # that's wrong on any platform.
Greg Ward13ae1c81999-03-22 14:55:25 +0000160
Greg Warda233d862000-03-22 00:15:45 +0000161 if ((self.prefix or self.exec_prefix or self.home) and
162 (self.install_base or self.install_platbase)):
163 raise DistutilsOptionError, \
164 ("must supply either prefix/exec-prefix/home or " +
165 "install-base/install-platbase -- not both")
Greg Ward4f08e4f2000-02-26 00:49:04 +0000166
Greg Ward790c1102000-03-22 00:51:18 +0000167 # Next, stuff that's wrong (or dubious) only on certain platforms.
Greg Ward4f08e4f2000-02-26 00:49:04 +0000168 if os.name == 'posix':
Greg Warda233d862000-03-22 00:15:45 +0000169 if self.home and (self.prefix or self.exec_prefix):
Greg Ward865de831999-09-21 18:31:14 +0000170 raise DistutilsOptionError, \
Greg Warda233d862000-03-22 00:15:45 +0000171 ("must supply either home or prefix/exec-prefix -- " +
172 "not both")
Greg Ward865de831999-09-21 18:31:14 +0000173 else:
Greg Warda233d862000-03-22 00:15:45 +0000174 if self.exec_prefix:
175 self.warn ("exec-prefix option ignored on this platform")
176 self.exec_prefix = None
177 if self.home:
178 self.warn ("home option ignored on this platform")
179 self.home = None
Greg Ward865de831999-09-21 18:31:14 +0000180
Greg Warda233d862000-03-22 00:15:45 +0000181 # Now the interesting logic -- so interesting that we farm it out
182 # to other methods. The goal of these methods is to set the final
183 # values for the install_{lib,scripts,data,...} options, using as
184 # input a heady brew of prefix, exec_prefix, home, install_base,
185 # install_platbase, user-supplied versions of
186 # install_{purelib,platlib,lib,scripts,data,...}, and the
187 # INSTALL_SCHEME dictionary above. Phew!
Greg Ward865de831999-09-21 18:31:14 +0000188
Greg Ward6a647bb2000-04-27 01:56:38 +0000189 self.dump_dirs ("pre-finalize_xxx")
Greg Wardff2d9b72000-04-26 02:38:01 +0000190
Greg Warda233d862000-03-22 00:15:45 +0000191 if os.name == 'posix':
192 self.finalize_unix ()
193 else:
194 self.finalize_other ()
195
Greg Ward6a647bb2000-04-27 01:56:38 +0000196 self.dump_dirs ("post-finalize_xxx()")
Greg Wardff2d9b72000-04-26 02:38:01 +0000197
198 # Expand configuration variables, tilde, etc. in self.install_base
199 # and self.install_platbase -- that way, we can use $base or
200 # $platbase in the other installation directories and not worry
201 # about needing recursive variable expansion (shudder).
202
203 self.config_vars = {'py_version_short': sys.version[0:3],
204 'sys_prefix': sysconfig.PREFIX,
205 'sys_exec_prefix': sysconfig.EXEC_PREFIX,
206 }
207 self.expand_basedirs ()
208
Greg Ward6a647bb2000-04-27 01:56:38 +0000209 self.dump_dirs ("post-expand_basedirs()")
Greg Wardff2d9b72000-04-26 02:38:01 +0000210
211 # Now define config vars for the base directories so we can expand
212 # everything else.
213 self.config_vars['base'] = self.install_base
214 self.config_vars['platbase'] = self.install_platbase
215
Greg Ward6a647bb2000-04-27 01:56:38 +0000216 from pprint import pprint
Greg Wardff2d9b72000-04-26 02:38:01 +0000217 print "config vars:"
218 pprint (self.config_vars)
219
Greg Warda233d862000-03-22 00:15:45 +0000220 # Expand "~" and configuration variables in the installation
221 # directories.
222 self.expand_dirs ()
223
Greg Ward6a647bb2000-04-27 01:56:38 +0000224 self.dump_dirs ("post-expand_dirs()")
Greg Wardff2d9b72000-04-26 02:38:01 +0000225
Greg Warda233d862000-03-22 00:15:45 +0000226 # Pick the actual directory to install all modules to: either
227 # install_purelib or install_platlib, depending on whether this
228 # module distribution is pure or not. Of course, if the user
229 # already specified install_lib, use their selection.
230 if self.install_lib is None:
231 if self.distribution.ext_modules: # has extensions: non-pure
232 self.install_lib = self.install_platlib
233 else:
234 self.install_lib = self.install_purelib
235
236 # Well, we're not actually fully completely finalized yet: we still
237 # have to deal with 'extra_path', which is the hack for allowing
238 # non-packagized module distributions (hello, Numerical Python!) to
239 # get their own directories.
240 self.handle_extra_path ()
241 self.install_libbase = self.install_lib # needed for .pth file
242 self.install_lib = os.path.join (self.install_lib, self.extra_dirs)
Greg Ward865de831999-09-21 18:31:14 +0000243
Greg Ward6a647bb2000-04-27 01:56:38 +0000244 # If a new root directory was supplied, make all the installation
245 # dirs relative to it.
246 if self.root is not None:
247 for name in ('lib', 'purelib', 'platlib', 'scripts', 'data'):
248 attr = "install_" + name
249 new_val = change_root (self.root, getattr (self, attr))
250 setattr (self, attr, new_val)
251
252 self.dump_dirs ("after prepending root")
253
Greg Ward790c1102000-03-22 00:51:18 +0000254 # Find out the build directories, ie. where to install from.
Greg Ward4f08e4f2000-02-26 00:49:04 +0000255 self.set_undefined_options ('build',
256 ('build_base', 'build_base'),
Greg Ward066af102000-03-22 00:30:54 +0000257 ('build_lib', 'build_lib'))
Greg Ward13ae1c81999-03-22 14:55:25 +0000258
259 # Punt on doc directories for now -- after all, we're punting on
260 # documentation completely!
261
Greg Warde01149c2000-02-18 00:35:22 +0000262 # finalize_options ()
Greg Ward13ae1c81999-03-22 14:55:25 +0000263
264
Greg Ward6a647bb2000-04-27 01:56:38 +0000265 # hack for debugging output
266 def dump_dirs (self, msg):
267 from distutils.fancy_getopt import longopt_xlate
268 print msg + ":"
269 for opt in self.user_options:
270 opt_name = string.translate (opt[0][0:-1], longopt_xlate)
271 val = getattr (self, opt_name)
272 print " %s: %s" % (opt_name, val)
273
274
Greg Warda233d862000-03-22 00:15:45 +0000275 def finalize_unix (self):
276
277 if self.install_base is not None or self.install_platbase is not None:
278 if ((self.install_lib is None and
279 self.install_purelib is None and
280 self.install_platlib is None) or
281 self.install_scripts is None or
282 self.install_data is None):
283 raise DistutilsOptionError, \
284 "install-base or install-platbase supplied, but " + \
285 "installation scheme is incomplete"
286
287 return
288
289 if self.home is not None:
290 self.install_base = self.install_platbase = self.home
291 self.select_scheme ("unix_home")
292 else:
293 if self.prefix is None:
294 if self.exec_prefix is not None:
295 raise DistutilsOptionError, \
296 "must not supply exec-prefix without prefix"
297
298 self.prefix = os.path.normpath (sys.prefix)
299 self.exec_prefix = os.path.normpath (sys.exec_prefix)
300 self.install_path_file = 1
301
302 else:
303 if self.exec_prefix is None:
304 self.exec_prefix = self.prefix
305
306
307 # XXX since we don't *know* that a user-supplied prefix really
308 # points to another Python installation, we can't be sure that
309 # writing a .pth file there will actually work -- so we don't
310 # try. That is, we only set 'install_path_file' if the user
311 # didn't supply prefix. There are certainly circumstances
312 # under which we *should* install a .pth file when the user
313 # supplies a prefix, namely when that prefix actually points to
314 # another Python installation. Hmmm.
315
316 self.install_base = self.prefix
317 self.install_platbase = self.exec_prefix
318 self.select_scheme ("unix_prefix")
319
320 # finalize_unix ()
321
322
323 def finalize_other (self): # Windows and Mac OS for now
324
325 if self.prefix is None:
326 self.prefix = os.path.normpath (sys.prefix)
327 self.install_path_file = 1
328
329 # XXX same caveat regarding 'install_path_file' as in
330 # 'finalize_unix()'.
331
332 self.install_base = self.install_platbase = self.prefix
333 try:
334 self.select_scheme (os.name)
335 except KeyError:
336 raise DistutilsPlatformError, \
337 "I don't know how to install stuff on '%s'" % os.name
338
339 # finalize_other ()
340
341
342 def select_scheme (self, name):
Greg Warda233d862000-03-22 00:15:45 +0000343 # it's the caller's problem if they supply a bad name!
344 scheme = INSTALL_SCHEMES[name]
Greg Warda233d862000-03-22 00:15:45 +0000345 for key in ('purelib', 'platlib', 'scripts', 'data'):
Greg Wardff2d9b72000-04-26 02:38:01 +0000346 setattr (self, 'install_' + key, scheme[key])
Greg Warda233d862000-03-22 00:15:45 +0000347
348
Greg Wardff2d9b72000-04-26 02:38:01 +0000349 def _expand_attrs (self, attrs):
350 for attr in attrs:
351 val = getattr (self, attr)
352 if val is not None:
353 if os.name == 'posix':
354 val = os.path.expanduser (val)
355 val = subst_vars (val, self.config_vars)
356 setattr (self, attr, val)
357
358
359 def expand_basedirs (self):
360 self._expand_attrs (['install_base',
Greg Ward6a647bb2000-04-27 01:56:38 +0000361 'install_platbase',
362 'root'])
Greg Wardff2d9b72000-04-26 02:38:01 +0000363
Greg Warda233d862000-03-22 00:15:45 +0000364 def expand_dirs (self):
Greg Wardff2d9b72000-04-26 02:38:01 +0000365 self._expand_attrs (['install_purelib',
366 'install_platlib',
367 'install_lib',
368 'install_scripts',
369 'install_data',])
Greg Warda233d862000-03-22 00:15:45 +0000370
371
372 def handle_extra_path (self):
373
374 if self.extra_path is None:
375 self.extra_path = self.distribution.extra_path
376
377 if self.extra_path is not None:
378 if type (self.extra_path) is StringType:
379 self.extra_path = string.split (self.extra_path, ',')
380
381 if len (self.extra_path) == 1:
382 path_file = extra_dirs = self.extra_path[0]
383 elif len (self.extra_path) == 2:
384 (path_file, extra_dirs) = self.extra_path
385 else:
386 raise DistutilsOptionError, \
387 "'extra_path' option must be a list, tuple, or " + \
388 "comma-separated string with 1 or 2 elements"
389
390 # convert to local form in case Unix notation used (as it
391 # should be in setup scripts)
392 extra_dirs = native_path (extra_dirs)
393
394 else:
395 path_file = None
396 extra_dirs = ''
397
398 # XXX should we warn if path_file and not extra_dirs? (in which
399 # case the path file would be harmless but pointless)
400 self.path_file = path_file
401 self.extra_dirs = extra_dirs
402
403 # handle_extra_path ()
404
405
Greg Ward13ae1c81999-03-22 14:55:25 +0000406 def run (self):
407
Greg Ward9a337071999-06-08 02:04:36 +0000408 # Obviously have to build before we can install
409 self.run_peer ('build')
410
Greg Wardee94c572000-03-29 02:15:57 +0000411 # Run all sub-commands: currently this just means install all
412 # Python modules using 'install_lib'.
Greg Wardba38d122000-03-31 02:52:02 +0000413 for (func, cmd_name) in self.sub_commands:
Greg Wardee94c572000-03-29 02:15:57 +0000414 if func is None or func():
Greg Wardba38d122000-03-31 02:52:02 +0000415 self.run_peer (cmd_name)
Greg Ward865de831999-09-21 18:31:14 +0000416
417 if self.path_file:
418 self.create_path_file ()
Greg Ward13ae1c81999-03-22 14:55:25 +0000419
Greg Warda233d862000-03-22 00:15:45 +0000420 normalized_path = map (os.path.normpath, sys.path)
421 if (not (self.path_file and self.install_path_file) and
422 os.path.normpath (self.install_lib) not in normalized_path):
423 self.warn (("modules installed to '%s', which is not in " +
424 "Python's module search path (sys.path) -- " +
425 "you'll have to change the search path yourself") %
426 self.install_lib)
427
Greg Ward13ae1c81999-03-22 14:55:25 +0000428 # run ()
429
Greg Ward865de831999-09-21 18:31:14 +0000430
Greg Wardee94c572000-03-29 02:15:57 +0000431 def get_outputs (self):
432 # This command doesn't have any outputs of its own, so just
433 # get the outputs of all its sub-commands.
434 outputs = []
Greg Wardba38d122000-03-31 02:52:02 +0000435 for (func, cmd_name) in self.sub_commands:
Greg Wardee94c572000-03-29 02:15:57 +0000436 if func is None or func():
Greg Wardba38d122000-03-31 02:52:02 +0000437 cmd = self.find_peer (cmd_name)
438 outputs.extend (cmd.get_outputs())
Greg Wardee94c572000-03-29 02:15:57 +0000439
440 return outputs
441
442
Greg Wardba38d122000-03-31 02:52:02 +0000443 def get_inputs (self):
444 # XXX gee, this looks familiar ;-(
445 inputs = []
446 for (func, cmd_name) in self.sub_commands:
447 if func is None or func():
448 cmd = self.find_peer (cmd_name)
449 inputs.extend (cmd.get_inputs())
450
451 return inputs
452
453
Greg Ward865de831999-09-21 18:31:14 +0000454 def create_path_file (self):
Greg Warda233d862000-03-22 00:15:45 +0000455 filename = os.path.join (self.install_libbase,
456 self.path_file + ".pth")
457 if self.install_path_file:
458 self.execute (write_file,
459 (filename, [self.extra_dirs]),
460 "creating %s" % filename)
Greg Ward865de831999-09-21 18:31:14 +0000461 else:
Greg Warda233d862000-03-22 00:15:45 +0000462 self.warn (("path file '%s' not created for alternate or custom " +
463 "installation (path files only work with standard " +
464 "installations)") %
465 filename)
Greg Ward865de831999-09-21 18:31:14 +0000466
Greg Ward13ae1c81999-03-22 14:55:25 +0000467# class Install