blob: 98b08cd7c658f98b2cffa87edf4be1afea9893d1 [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 Warda233d862000-03-22 00:15:45 +000013from distutils.util import write_file, native_path, subst_vars
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)"),
63
64 # Or, explicitly set the installation scheme
65 ('install-purelib=', None,
66 "installation directory for pure Python module distributions"),
Greg Ward4f08e4f2000-02-26 00:49:04 +000067 ('install-platlib=', None,
Greg Warda233d862000-03-22 00:15:45 +000068 "installation directory for non-pure module distributions"),
69 ('install-lib=', None,
70 "installation directory for all module distributions " +
71 "(overrides --install-purelib and --install-platlib)"),
72
73 ('install-scripts=', None,
74 "installation directory for Python scripts"),
75 ('install-data=', None,
76 "installation directory for data files"),
Greg Ward4f08e4f2000-02-26 00:49:04 +000077
Greg Wardbbeceea2000-02-18 00:25:39 +000078 # Where to install documentation (eventually!)
Greg Ward4f08e4f2000-02-26 00:49:04 +000079 #('doc-format=', None, "format of documentation to generate"),
80 #('install-man=', None, "directory for Unix man pages"),
81 #('install-html=', None, "directory for HTML documentation"),
82 #('install-info=', None, "directory for GNU info files"),
Greg Wardbbeceea2000-02-18 00:25:39 +000083 ]
Greg Ward13ae1c81999-03-22 14:55:25 +000084
Greg Ward9a337071999-06-08 02:04:36 +000085
Greg Wardee94c572000-03-29 02:15:57 +000086 # 'sub_commands': a list of commands this command might have to run
87 # to get its work done. Each command is represented as a tuple
88 # (func, command) where 'func' is a function to call that returns
89 # true if 'command' (the sub-command name, a string) needs to be
90 # run. If 'func' is None, assume that 'command' must always be run.
91 sub_commands = [(None, 'install_lib')]
92
93
Greg Warde01149c2000-02-18 00:35:22 +000094 def initialize_options (self):
Greg Ward13ae1c81999-03-22 14:55:25 +000095
Greg Ward790c1102000-03-22 00:51:18 +000096 # High-level options: these select both an installation base
97 # and scheme.
Greg Ward13ae1c81999-03-22 14:55:25 +000098 self.prefix = None
99 self.exec_prefix = None
Greg Warda233d862000-03-22 00:15:45 +0000100 self.home = None
101
Greg Ward790c1102000-03-22 00:51:18 +0000102 # These select only the installation base; it's up to the user to
103 # specify the installation scheme (currently, that means supplying
104 # the --install-{platlib,purelib,scripts,data} options).
Greg Warda233d862000-03-22 00:15:45 +0000105 self.install_base = None
106 self.install_platbase = None
Greg Ward13ae1c81999-03-22 14:55:25 +0000107
Greg Ward790c1102000-03-22 00:51:18 +0000108 # These options are the actual installation directories; if not
109 # supplied by the user, they are filled in using the installation
110 # scheme implied by prefix/exec-prefix/home and the contents of
111 # that installation scheme.
112 self.install_purelib = None # for pure module distributions
113 self.install_platlib = None # non-pure (dists w/ extensions)
114 self.install_lib = None # set to either purelib or platlib
Greg Warda233d862000-03-22 00:15:45 +0000115 self.install_scripts = None
116 self.install_data = None
Greg Ward13ae1c81999-03-22 14:55:25 +0000117
Greg Ward790c1102000-03-22 00:51:18 +0000118 # These two are for putting non-packagized distributions into their
119 # own directory and creating a .pth file if it makes sense.
120 # 'extra_path' comes from the setup file; 'install_path_file' is
121 # set only if we determine that it makes sense to install a path
122 # file.
Greg Warda233d862000-03-22 00:15:45 +0000123 self.extra_path = None
124 self.install_path_file = 0
125
Greg Ward066af102000-03-22 00:30:54 +0000126 # These are only here as a conduit from the 'build' command to the
127 # 'install_*' commands that do the real work. ('build_base' isn't
128 # actually used anywhere, but it might be useful in future.) They
129 # are not user options, because if the user told the install
130 # command where the build directory is, that wouldn't affect the
131 # build command.
132 self.build_base = None
133 self.build_lib = None
134
Greg Ward790c1102000-03-22 00:51:18 +0000135 # Not defined yet because we don't know anything about
136 # documentation yet.
Greg Warda233d862000-03-22 00:15:45 +0000137 #self.install_man = None
138 #self.install_html = None
139 #self.install_info = None
Greg Ward13ae1c81999-03-22 14:55:25 +0000140
Greg Ward13ae1c81999-03-22 14:55:25 +0000141
Greg Warde01149c2000-02-18 00:35:22 +0000142 def finalize_options (self):
Greg Ward13ae1c81999-03-22 14:55:25 +0000143
Greg Ward790c1102000-03-22 00:51:18 +0000144 # This method (and its pliant slaves, like 'finalize_unix()',
145 # 'finalize_other()', and 'select_scheme()') is where the default
146 # installation directories for modules, extension modules, and
147 # anything else we care to install from a Python module
148 # distribution. Thus, this code makes a pretty important policy
149 # statement about how third-party stuff is added to a Python
150 # installation! Note that the actual work of installation is done
151 # by the relatively simple 'install_*' commands; they just take
152 # their orders from the installation directory options determined
153 # here.
Greg Ward9a337071999-06-08 02:04:36 +0000154
Greg Ward790c1102000-03-22 00:51:18 +0000155 # Check for errors/inconsistencies in the options; first, stuff
156 # that's wrong on any platform.
Greg Ward13ae1c81999-03-22 14:55:25 +0000157
Greg Warda233d862000-03-22 00:15:45 +0000158 if ((self.prefix or self.exec_prefix or self.home) and
159 (self.install_base or self.install_platbase)):
160 raise DistutilsOptionError, \
161 ("must supply either prefix/exec-prefix/home or " +
162 "install-base/install-platbase -- not both")
Greg Ward4f08e4f2000-02-26 00:49:04 +0000163
Greg Ward790c1102000-03-22 00:51:18 +0000164 # Next, stuff that's wrong (or dubious) only on certain platforms.
Greg Ward4f08e4f2000-02-26 00:49:04 +0000165 if os.name == 'posix':
Greg Warda233d862000-03-22 00:15:45 +0000166 if self.home and (self.prefix or self.exec_prefix):
Greg Ward865de831999-09-21 18:31:14 +0000167 raise DistutilsOptionError, \
Greg Warda233d862000-03-22 00:15:45 +0000168 ("must supply either home or prefix/exec-prefix -- " +
169 "not both")
Greg Ward865de831999-09-21 18:31:14 +0000170 else:
Greg Warda233d862000-03-22 00:15:45 +0000171 if self.exec_prefix:
172 self.warn ("exec-prefix option ignored on this platform")
173 self.exec_prefix = None
174 if self.home:
175 self.warn ("home option ignored on this platform")
176 self.home = None
Greg Ward865de831999-09-21 18:31:14 +0000177
Greg Warda233d862000-03-22 00:15:45 +0000178 # Now the interesting logic -- so interesting that we farm it out
179 # to other methods. The goal of these methods is to set the final
180 # values for the install_{lib,scripts,data,...} options, using as
181 # input a heady brew of prefix, exec_prefix, home, install_base,
182 # install_platbase, user-supplied versions of
183 # install_{purelib,platlib,lib,scripts,data,...}, and the
184 # INSTALL_SCHEME dictionary above. Phew!
Greg Ward865de831999-09-21 18:31:14 +0000185
Greg Wardff2d9b72000-04-26 02:38:01 +0000186 from pprint import pprint
187 print "pre-finalize:"
188 pprint (self.__dict__)
189
Greg Warda233d862000-03-22 00:15:45 +0000190 if os.name == 'posix':
191 self.finalize_unix ()
192 else:
193 self.finalize_other ()
194
Greg Wardff2d9b72000-04-26 02:38:01 +0000195 print "post-finalize:"
196 pprint (self.__dict__)
197
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
209 print "post-expand_basedirs:"
210 pprint (self.__dict__)
211
212 # Now define config vars for the base directories so we can expand
213 # everything else.
214 self.config_vars['base'] = self.install_base
215 self.config_vars['platbase'] = self.install_platbase
216
217 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 Wardff2d9b72000-04-26 02:38:01 +0000224 print "post-expand:"
225 pprint (self.__dict__)
226
Greg Warda233d862000-03-22 00:15:45 +0000227 # Pick the actual directory to install all modules to: either
228 # install_purelib or install_platlib, depending on whether this
229 # module distribution is pure or not. Of course, if the user
230 # already specified install_lib, use their selection.
231 if self.install_lib is None:
232 if self.distribution.ext_modules: # has extensions: non-pure
233 self.install_lib = self.install_platlib
234 else:
235 self.install_lib = self.install_purelib
236
237 # Well, we're not actually fully completely finalized yet: we still
238 # have to deal with 'extra_path', which is the hack for allowing
239 # non-packagized module distributions (hello, Numerical Python!) to
240 # get their own directories.
241 self.handle_extra_path ()
242 self.install_libbase = self.install_lib # needed for .pth file
243 self.install_lib = os.path.join (self.install_lib, self.extra_dirs)
Greg Ward865de831999-09-21 18:31:14 +0000244
Greg Ward790c1102000-03-22 00:51:18 +0000245 # Find out the build directories, ie. where to install from.
Greg Ward4f08e4f2000-02-26 00:49:04 +0000246 self.set_undefined_options ('build',
247 ('build_base', 'build_base'),
Greg Ward066af102000-03-22 00:30:54 +0000248 ('build_lib', 'build_lib'))
Greg Ward13ae1c81999-03-22 14:55:25 +0000249
250 # Punt on doc directories for now -- after all, we're punting on
251 # documentation completely!
252
Greg Warde01149c2000-02-18 00:35:22 +0000253 # finalize_options ()
Greg Ward13ae1c81999-03-22 14:55:25 +0000254
255
Greg Warda233d862000-03-22 00:15:45 +0000256 def finalize_unix (self):
257
258 if self.install_base is not None or self.install_platbase is not None:
259 if ((self.install_lib is None and
260 self.install_purelib is None and
261 self.install_platlib is None) or
262 self.install_scripts is None or
263 self.install_data is None):
264 raise DistutilsOptionError, \
265 "install-base or install-platbase supplied, but " + \
266 "installation scheme is incomplete"
267
268 return
269
270 if self.home is not None:
271 self.install_base = self.install_platbase = self.home
272 self.select_scheme ("unix_home")
273 else:
274 if self.prefix is None:
275 if self.exec_prefix is not None:
276 raise DistutilsOptionError, \
277 "must not supply exec-prefix without prefix"
278
279 self.prefix = os.path.normpath (sys.prefix)
280 self.exec_prefix = os.path.normpath (sys.exec_prefix)
281 self.install_path_file = 1
282
283 else:
284 if self.exec_prefix is None:
285 self.exec_prefix = self.prefix
286
287
288 # XXX since we don't *know* that a user-supplied prefix really
289 # points to another Python installation, we can't be sure that
290 # writing a .pth file there will actually work -- so we don't
291 # try. That is, we only set 'install_path_file' if the user
292 # didn't supply prefix. There are certainly circumstances
293 # under which we *should* install a .pth file when the user
294 # supplies a prefix, namely when that prefix actually points to
295 # another Python installation. Hmmm.
296
297 self.install_base = self.prefix
298 self.install_platbase = self.exec_prefix
299 self.select_scheme ("unix_prefix")
300
301 # finalize_unix ()
302
303
304 def finalize_other (self): # Windows and Mac OS for now
305
306 if self.prefix is None:
307 self.prefix = os.path.normpath (sys.prefix)
308 self.install_path_file = 1
309
310 # XXX same caveat regarding 'install_path_file' as in
311 # 'finalize_unix()'.
312
313 self.install_base = self.install_platbase = self.prefix
314 try:
315 self.select_scheme (os.name)
316 except KeyError:
317 raise DistutilsPlatformError, \
318 "I don't know how to install stuff on '%s'" % os.name
319
320 # finalize_other ()
321
322
323 def select_scheme (self, name):
Greg Warda233d862000-03-22 00:15:45 +0000324 # it's the caller's problem if they supply a bad name!
325 scheme = INSTALL_SCHEMES[name]
Greg Warda233d862000-03-22 00:15:45 +0000326 for key in ('purelib', 'platlib', 'scripts', 'data'):
Greg Wardff2d9b72000-04-26 02:38:01 +0000327 setattr (self, 'install_' + key, scheme[key])
Greg Warda233d862000-03-22 00:15:45 +0000328
329
Greg Wardff2d9b72000-04-26 02:38:01 +0000330 def _expand_attrs (self, attrs):
331 for attr in attrs:
332 val = getattr (self, attr)
333 if val is not None:
334 if os.name == 'posix':
335 val = os.path.expanduser (val)
336 val = subst_vars (val, self.config_vars)
337 setattr (self, attr, val)
338
339
340 def expand_basedirs (self):
341 self._expand_attrs (['install_base',
342 'install_platbase'])
343
Greg Warda233d862000-03-22 00:15:45 +0000344 def expand_dirs (self):
Greg Wardff2d9b72000-04-26 02:38:01 +0000345 self._expand_attrs (['install_purelib',
346 'install_platlib',
347 'install_lib',
348 'install_scripts',
349 'install_data',])
Greg Warda233d862000-03-22 00:15:45 +0000350
351
352 def handle_extra_path (self):
353
354 if self.extra_path is None:
355 self.extra_path = self.distribution.extra_path
356
357 if self.extra_path is not None:
358 if type (self.extra_path) is StringType:
359 self.extra_path = string.split (self.extra_path, ',')
360
361 if len (self.extra_path) == 1:
362 path_file = extra_dirs = self.extra_path[0]
363 elif len (self.extra_path) == 2:
364 (path_file, extra_dirs) = self.extra_path
365 else:
366 raise DistutilsOptionError, \
367 "'extra_path' option must be a list, tuple, or " + \
368 "comma-separated string with 1 or 2 elements"
369
370 # convert to local form in case Unix notation used (as it
371 # should be in setup scripts)
372 extra_dirs = native_path (extra_dirs)
373
374 else:
375 path_file = None
376 extra_dirs = ''
377
378 # XXX should we warn if path_file and not extra_dirs? (in which
379 # case the path file would be harmless but pointless)
380 self.path_file = path_file
381 self.extra_dirs = extra_dirs
382
383 # handle_extra_path ()
384
385
Greg Ward13ae1c81999-03-22 14:55:25 +0000386 def run (self):
387
Greg Ward9a337071999-06-08 02:04:36 +0000388 # Obviously have to build before we can install
389 self.run_peer ('build')
390
Greg Wardee94c572000-03-29 02:15:57 +0000391 # Run all sub-commands: currently this just means install all
392 # Python modules using 'install_lib'.
Greg Wardba38d122000-03-31 02:52:02 +0000393 for (func, cmd_name) in self.sub_commands:
Greg Wardee94c572000-03-29 02:15:57 +0000394 if func is None or func():
Greg Wardba38d122000-03-31 02:52:02 +0000395 self.run_peer (cmd_name)
Greg Ward865de831999-09-21 18:31:14 +0000396
397 if self.path_file:
398 self.create_path_file ()
Greg Ward13ae1c81999-03-22 14:55:25 +0000399
Greg Warda233d862000-03-22 00:15:45 +0000400 normalized_path = map (os.path.normpath, sys.path)
401 if (not (self.path_file and self.install_path_file) and
402 os.path.normpath (self.install_lib) not in normalized_path):
403 self.warn (("modules installed to '%s', which is not in " +
404 "Python's module search path (sys.path) -- " +
405 "you'll have to change the search path yourself") %
406 self.install_lib)
407
Greg Ward13ae1c81999-03-22 14:55:25 +0000408 # run ()
409
Greg Ward865de831999-09-21 18:31:14 +0000410
Greg Wardee94c572000-03-29 02:15:57 +0000411 def get_outputs (self):
412 # This command doesn't have any outputs of its own, so just
413 # get the outputs of all its sub-commands.
414 outputs = []
Greg Wardba38d122000-03-31 02:52:02 +0000415 for (func, cmd_name) in self.sub_commands:
Greg Wardee94c572000-03-29 02:15:57 +0000416 if func is None or func():
Greg Wardba38d122000-03-31 02:52:02 +0000417 cmd = self.find_peer (cmd_name)
418 outputs.extend (cmd.get_outputs())
Greg Wardee94c572000-03-29 02:15:57 +0000419
420 return outputs
421
422
Greg Wardba38d122000-03-31 02:52:02 +0000423 def get_inputs (self):
424 # XXX gee, this looks familiar ;-(
425 inputs = []
426 for (func, cmd_name) in self.sub_commands:
427 if func is None or func():
428 cmd = self.find_peer (cmd_name)
429 inputs.extend (cmd.get_inputs())
430
431 return inputs
432
433
Greg Ward865de831999-09-21 18:31:14 +0000434 def create_path_file (self):
Greg Warda233d862000-03-22 00:15:45 +0000435 filename = os.path.join (self.install_libbase,
436 self.path_file + ".pth")
437 if self.install_path_file:
438 self.execute (write_file,
439 (filename, [self.extra_dirs]),
440 "creating %s" % filename)
Greg Ward865de831999-09-21 18:31:14 +0000441 else:
Greg Warda233d862000-03-22 00:15:45 +0000442 self.warn (("path file '%s' not created for alternate or custom " +
443 "installation (path files only work with standard " +
444 "installations)") %
445 filename)
Greg Ward865de831999-09-21 18:31:14 +0000446
Greg Ward13ae1c81999-03-22 14:55:25 +0000447# class Install