blob: 995fd875c30139553b9f4f9cb826b830687cf491 [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 Warda233d862000-03-22 00:15:45 +000012from distutils.util import write_file, native_path, subst_vars
Greg Ward4f08e4f2000-02-26 00:49:04 +000013from distutils.errors import DistutilsOptionError
Greg Ward13ae1c81999-03-22 14:55:25 +000014
Greg Warda233d862000-03-22 00:15:45 +000015INSTALL_SCHEMES = {
16 'unix_prefix': {
17 'purelib': '$base/lib/python$py_version_short/site-packages',
18 'platlib': '$platbase/lib/python$py_version_short/site-packages',
19 'scripts': '$base/bin',
20 'data' : '$base/share',
21 },
22 'unix_home': {
23 'purelib': '$base/lib/python',
24 'platlib': '$base/lib/python',
25 'scripts': '$base/bin',
26 'data' : '$base/share',
27 },
28 'nt': {
29 'purelib': '$base',
30 'platlib': '$base',
31 'scripts': '$base\\Scripts',
32 'data' : '$base\\Data',
33 },
34 'mac': {
35 'purelib': '$base:Lib',
36 'platlib': '$base:Mac:PlugIns',
37 'scripts': '$base:Scripts',
38 'data' : '$base:Data',
39 }
40 }
41
42
Greg Ward1993f9a2000-02-18 00:13:53 +000043class install (Command):
Greg Ward13ae1c81999-03-22 14:55:25 +000044
Greg Ward37bc8152000-01-30 18:34:15 +000045 description = "install everything from build directory"
46
Greg Wardbbeceea2000-02-18 00:25:39 +000047 user_options = [
Greg Warda233d862000-03-22 00:15:45 +000048 # Select installation scheme and set base director(y|ies)
49 ('prefix=', None,
50 "installation prefix"),
Greg Wardbbeceea2000-02-18 00:25:39 +000051 ('exec-prefix=', None,
Greg Warda233d862000-03-22 00:15:45 +000052 "(Unix only) prefix for platform-specific files"),
53 ('home=', None,
54 "(Unix only) home directory to install under"),
Greg Ward13ae1c81999-03-22 14:55:25 +000055
Greg Warda233d862000-03-22 00:15:45 +000056 # Or, just set the base director(y|ies)
57 ('install-base=', None,
58 "base installation directory (instead of --prefix or --home)"),
59 ('install-platbase=', None,
60 "base installation directory for platform-specific files " +
61 "(instead of --exec-prefix or --home)"),
62
63 # Or, explicitly set the installation scheme
64 ('install-purelib=', None,
65 "installation directory for pure Python module distributions"),
Greg Ward4f08e4f2000-02-26 00:49:04 +000066 ('install-platlib=', None,
Greg Warda233d862000-03-22 00:15:45 +000067 "installation directory for non-pure module distributions"),
68 ('install-lib=', None,
69 "installation directory for all module distributions " +
70 "(overrides --install-purelib and --install-platlib)"),
71
72 ('install-scripts=', None,
73 "installation directory for Python scripts"),
74 ('install-data=', None,
75 "installation directory for data files"),
Greg Ward4f08e4f2000-02-26 00:49:04 +000076
Greg Wardbbeceea2000-02-18 00:25:39 +000077 # Where to install documentation (eventually!)
Greg Ward4f08e4f2000-02-26 00:49:04 +000078 #('doc-format=', None, "format of documentation to generate"),
79 #('install-man=', None, "directory for Unix man pages"),
80 #('install-html=', None, "directory for HTML documentation"),
81 #('install-info=', None, "directory for GNU info files"),
Greg Wardbbeceea2000-02-18 00:25:39 +000082 ]
Greg Ward13ae1c81999-03-22 14:55:25 +000083
Greg Ward9a337071999-06-08 02:04:36 +000084
Greg Wardee94c572000-03-29 02:15:57 +000085 # 'sub_commands': a list of commands this command might have to run
86 # to get its work done. Each command is represented as a tuple
87 # (func, command) where 'func' is a function to call that returns
88 # true if 'command' (the sub-command name, a string) needs to be
89 # run. If 'func' is None, assume that 'command' must always be run.
90 sub_commands = [(None, 'install_lib')]
91
92
Greg Warde01149c2000-02-18 00:35:22 +000093 def initialize_options (self):
Greg Ward13ae1c81999-03-22 14:55:25 +000094
Greg Ward790c1102000-03-22 00:51:18 +000095 # High-level options: these select both an installation base
96 # and scheme.
Greg Ward13ae1c81999-03-22 14:55:25 +000097 self.prefix = None
98 self.exec_prefix = None
Greg Warda233d862000-03-22 00:15:45 +000099 self.home = None
100
Greg Ward790c1102000-03-22 00:51:18 +0000101 # These select only the installation base; it's up to the user to
102 # specify the installation scheme (currently, that means supplying
103 # the --install-{platlib,purelib,scripts,data} options).
Greg Warda233d862000-03-22 00:15:45 +0000104 self.install_base = None
105 self.install_platbase = None
Greg Ward13ae1c81999-03-22 14:55:25 +0000106
Greg Ward790c1102000-03-22 00:51:18 +0000107 # These options are the actual installation directories; if not
108 # supplied by the user, they are filled in using the installation
109 # scheme implied by prefix/exec-prefix/home and the contents of
110 # that installation scheme.
111 self.install_purelib = None # for pure module distributions
112 self.install_platlib = None # non-pure (dists w/ extensions)
113 self.install_lib = None # set to either purelib or platlib
Greg Warda233d862000-03-22 00:15:45 +0000114 self.install_scripts = None
115 self.install_data = None
Greg Ward13ae1c81999-03-22 14:55:25 +0000116
Greg Ward790c1102000-03-22 00:51:18 +0000117 # These two are for putting non-packagized distributions into their
118 # own directory and creating a .pth file if it makes sense.
119 # 'extra_path' comes from the setup file; 'install_path_file' is
120 # set only if we determine that it makes sense to install a path
121 # file.
Greg Warda233d862000-03-22 00:15:45 +0000122 self.extra_path = None
123 self.install_path_file = 0
124
Greg Ward066af102000-03-22 00:30:54 +0000125 # These are only here as a conduit from the 'build' command to the
126 # 'install_*' commands that do the real work. ('build_base' isn't
127 # actually used anywhere, but it might be useful in future.) They
128 # are not user options, because if the user told the install
129 # command where the build directory is, that wouldn't affect the
130 # build command.
131 self.build_base = None
132 self.build_lib = None
133
Greg Ward790c1102000-03-22 00:51:18 +0000134 # Not defined yet because we don't know anything about
135 # documentation yet.
Greg Warda233d862000-03-22 00:15:45 +0000136 #self.install_man = None
137 #self.install_html = None
138 #self.install_info = None
Greg Ward13ae1c81999-03-22 14:55:25 +0000139
Greg Ward13ae1c81999-03-22 14:55:25 +0000140
Greg Warde01149c2000-02-18 00:35:22 +0000141 def finalize_options (self):
Greg Ward13ae1c81999-03-22 14:55:25 +0000142
Greg Ward790c1102000-03-22 00:51:18 +0000143 # This method (and its pliant slaves, like 'finalize_unix()',
144 # 'finalize_other()', and 'select_scheme()') is where the default
145 # installation directories for modules, extension modules, and
146 # anything else we care to install from a Python module
147 # distribution. Thus, this code makes a pretty important policy
148 # statement about how third-party stuff is added to a Python
149 # installation! Note that the actual work of installation is done
150 # by the relatively simple 'install_*' commands; they just take
151 # their orders from the installation directory options determined
152 # here.
Greg Ward9a337071999-06-08 02:04:36 +0000153
Greg Ward790c1102000-03-22 00:51:18 +0000154 # Check for errors/inconsistencies in the options; first, stuff
155 # that's wrong on any platform.
Greg Ward13ae1c81999-03-22 14:55:25 +0000156
Greg Warda233d862000-03-22 00:15:45 +0000157 if ((self.prefix or self.exec_prefix or self.home) and
158 (self.install_base or self.install_platbase)):
159 raise DistutilsOptionError, \
160 ("must supply either prefix/exec-prefix/home or " +
161 "install-base/install-platbase -- not both")
Greg Ward4f08e4f2000-02-26 00:49:04 +0000162
Greg Ward790c1102000-03-22 00:51:18 +0000163 # Next, stuff that's wrong (or dubious) only on certain platforms.
Greg Ward4f08e4f2000-02-26 00:49:04 +0000164 if os.name == 'posix':
Greg Warda233d862000-03-22 00:15:45 +0000165 if self.home and (self.prefix or self.exec_prefix):
Greg Ward865de831999-09-21 18:31:14 +0000166 raise DistutilsOptionError, \
Greg Warda233d862000-03-22 00:15:45 +0000167 ("must supply either home or prefix/exec-prefix -- " +
168 "not both")
Greg Ward865de831999-09-21 18:31:14 +0000169 else:
Greg Warda233d862000-03-22 00:15:45 +0000170 if self.exec_prefix:
171 self.warn ("exec-prefix option ignored on this platform")
172 self.exec_prefix = None
173 if self.home:
174 self.warn ("home option ignored on this platform")
175 self.home = None
Greg Ward865de831999-09-21 18:31:14 +0000176
Greg Warda233d862000-03-22 00:15:45 +0000177 # Now the interesting logic -- so interesting that we farm it out
178 # to other methods. The goal of these methods is to set the final
179 # values for the install_{lib,scripts,data,...} options, using as
180 # input a heady brew of prefix, exec_prefix, home, install_base,
181 # install_platbase, user-supplied versions of
182 # install_{purelib,platlib,lib,scripts,data,...}, and the
183 # INSTALL_SCHEME dictionary above. Phew!
Greg Ward865de831999-09-21 18:31:14 +0000184
Greg Warda233d862000-03-22 00:15:45 +0000185 if os.name == 'posix':
186 self.finalize_unix ()
187 else:
188 self.finalize_other ()
189
190 # Expand "~" and configuration variables in the installation
191 # directories.
192 self.expand_dirs ()
193
194 # Pick the actual directory to install all modules to: either
195 # install_purelib or install_platlib, depending on whether this
196 # module distribution is pure or not. Of course, if the user
197 # already specified install_lib, use their selection.
198 if self.install_lib is None:
199 if self.distribution.ext_modules: # has extensions: non-pure
200 self.install_lib = self.install_platlib
201 else:
202 self.install_lib = self.install_purelib
203
204 # Well, we're not actually fully completely finalized yet: we still
205 # have to deal with 'extra_path', which is the hack for allowing
206 # non-packagized module distributions (hello, Numerical Python!) to
207 # get their own directories.
208 self.handle_extra_path ()
209 self.install_libbase = self.install_lib # needed for .pth file
210 self.install_lib = os.path.join (self.install_lib, self.extra_dirs)
Greg Ward865de831999-09-21 18:31:14 +0000211
Greg Ward790c1102000-03-22 00:51:18 +0000212 # Find out the build directories, ie. where to install from.
Greg Ward4f08e4f2000-02-26 00:49:04 +0000213 self.set_undefined_options ('build',
214 ('build_base', 'build_base'),
Greg Ward066af102000-03-22 00:30:54 +0000215 ('build_lib', 'build_lib'))
Greg Ward13ae1c81999-03-22 14:55:25 +0000216
217 # Punt on doc directories for now -- after all, we're punting on
218 # documentation completely!
219
Greg Warde01149c2000-02-18 00:35:22 +0000220 # finalize_options ()
Greg Ward13ae1c81999-03-22 14:55:25 +0000221
222
Greg Warda233d862000-03-22 00:15:45 +0000223 def finalize_unix (self):
224
225 if self.install_base is not None or self.install_platbase is not None:
226 if ((self.install_lib is None and
227 self.install_purelib is None and
228 self.install_platlib is None) or
229 self.install_scripts is None or
230 self.install_data is None):
231 raise DistutilsOptionError, \
232 "install-base or install-platbase supplied, but " + \
233 "installation scheme is incomplete"
234
235 return
236
237 if self.home is not None:
238 self.install_base = self.install_platbase = self.home
239 self.select_scheme ("unix_home")
240 else:
241 if self.prefix is None:
242 if self.exec_prefix is not None:
243 raise DistutilsOptionError, \
244 "must not supply exec-prefix without prefix"
245
246 self.prefix = os.path.normpath (sys.prefix)
247 self.exec_prefix = os.path.normpath (sys.exec_prefix)
248 self.install_path_file = 1
249
250 else:
251 if self.exec_prefix is None:
252 self.exec_prefix = self.prefix
253
254
255 # XXX since we don't *know* that a user-supplied prefix really
256 # points to another Python installation, we can't be sure that
257 # writing a .pth file there will actually work -- so we don't
258 # try. That is, we only set 'install_path_file' if the user
259 # didn't supply prefix. There are certainly circumstances
260 # under which we *should* install a .pth file when the user
261 # supplies a prefix, namely when that prefix actually points to
262 # another Python installation. Hmmm.
263
264 self.install_base = self.prefix
265 self.install_platbase = self.exec_prefix
266 self.select_scheme ("unix_prefix")
267
268 # finalize_unix ()
269
270
271 def finalize_other (self): # Windows and Mac OS for now
272
273 if self.prefix is None:
274 self.prefix = os.path.normpath (sys.prefix)
275 self.install_path_file = 1
276
277 # XXX same caveat regarding 'install_path_file' as in
278 # 'finalize_unix()'.
279
280 self.install_base = self.install_platbase = self.prefix
281 try:
282 self.select_scheme (os.name)
283 except KeyError:
284 raise DistutilsPlatformError, \
285 "I don't know how to install stuff on '%s'" % os.name
286
287 # finalize_other ()
288
289
290 def select_scheme (self, name):
291
Greg Ward790c1102000-03-22 00:51:18 +0000292 # "select a scheme" means:
293 # - set install-base and install-platbase
294 # - subst. base/platbase/version into the values of the
295 # particular scheme dictionary
296 # - use the resultings strings to set install-lib, etc.
297
Greg Warda233d862000-03-22 00:15:45 +0000298 # it's the caller's problem if they supply a bad name!
299 scheme = INSTALL_SCHEMES[name]
300
301 vars = { 'base': self.install_base,
302 'platbase': self.install_platbase,
303 'py_version_short': sys.version[0:3],
304 }
305
306 for key in ('purelib', 'platlib', 'scripts', 'data'):
307 val = subst_vars (scheme[key], vars)
308 setattr (self, 'install_' + key, val)
309
310
311 def expand_dirs (self):
312
313 # XXX probably don't want to 'expanduser()' on Windows or Mac
314 # XXX should use 'util.subst_vars()' with our own set of
315 # configuration variables
316
317 for att in ('base', 'platbase',
318 'purelib', 'platlib', 'lib',
319 'scripts', 'data'):
320 fullname = "install_" + att
321 val = getattr (self, fullname)
322 if val is not None:
323 setattr (self, fullname,
324 os.path.expandvars (os.path.expanduser (val)))
325
326
327 def handle_extra_path (self):
328
329 if self.extra_path is None:
330 self.extra_path = self.distribution.extra_path
331
332 if self.extra_path is not None:
333 if type (self.extra_path) is StringType:
334 self.extra_path = string.split (self.extra_path, ',')
335
336 if len (self.extra_path) == 1:
337 path_file = extra_dirs = self.extra_path[0]
338 elif len (self.extra_path) == 2:
339 (path_file, extra_dirs) = self.extra_path
340 else:
341 raise DistutilsOptionError, \
342 "'extra_path' option must be a list, tuple, or " + \
343 "comma-separated string with 1 or 2 elements"
344
345 # convert to local form in case Unix notation used (as it
346 # should be in setup scripts)
347 extra_dirs = native_path (extra_dirs)
348
349 else:
350 path_file = None
351 extra_dirs = ''
352
353 # XXX should we warn if path_file and not extra_dirs? (in which
354 # case the path file would be harmless but pointless)
355 self.path_file = path_file
356 self.extra_dirs = extra_dirs
357
358 # handle_extra_path ()
359
360
Greg Ward13ae1c81999-03-22 14:55:25 +0000361 def run (self):
362
Greg Ward9a337071999-06-08 02:04:36 +0000363 # Obviously have to build before we can install
364 self.run_peer ('build')
365
Greg Wardee94c572000-03-29 02:15:57 +0000366 # Run all sub-commands: currently this just means install all
367 # Python modules using 'install_lib'.
Greg Wardba38d122000-03-31 02:52:02 +0000368 for (func, cmd_name) in self.sub_commands:
Greg Wardee94c572000-03-29 02:15:57 +0000369 if func is None or func():
Greg Wardba38d122000-03-31 02:52:02 +0000370 self.run_peer (cmd_name)
Greg Ward865de831999-09-21 18:31:14 +0000371
372 if self.path_file:
373 self.create_path_file ()
Greg Ward13ae1c81999-03-22 14:55:25 +0000374
Greg Warda233d862000-03-22 00:15:45 +0000375 normalized_path = map (os.path.normpath, sys.path)
376 if (not (self.path_file and self.install_path_file) and
377 os.path.normpath (self.install_lib) not in normalized_path):
378 self.warn (("modules installed to '%s', which is not in " +
379 "Python's module search path (sys.path) -- " +
380 "you'll have to change the search path yourself") %
381 self.install_lib)
382
Greg Ward13ae1c81999-03-22 14:55:25 +0000383 # run ()
384
Greg Ward865de831999-09-21 18:31:14 +0000385
Greg Wardee94c572000-03-29 02:15:57 +0000386 def get_outputs (self):
387 # This command doesn't have any outputs of its own, so just
388 # get the outputs of all its sub-commands.
389 outputs = []
Greg Wardba38d122000-03-31 02:52:02 +0000390 for (func, cmd_name) in self.sub_commands:
Greg Wardee94c572000-03-29 02:15:57 +0000391 if func is None or func():
Greg Wardba38d122000-03-31 02:52:02 +0000392 cmd = self.find_peer (cmd_name)
393 outputs.extend (cmd.get_outputs())
Greg Wardee94c572000-03-29 02:15:57 +0000394
395 return outputs
396
397
Greg Wardba38d122000-03-31 02:52:02 +0000398 def get_inputs (self):
399 # XXX gee, this looks familiar ;-(
400 inputs = []
401 for (func, cmd_name) in self.sub_commands:
402 if func is None or func():
403 cmd = self.find_peer (cmd_name)
404 inputs.extend (cmd.get_inputs())
405
406 return inputs
407
408
Greg Ward865de831999-09-21 18:31:14 +0000409 def create_path_file (self):
Greg Warda233d862000-03-22 00:15:45 +0000410 filename = os.path.join (self.install_libbase,
411 self.path_file + ".pth")
412 if self.install_path_file:
413 self.execute (write_file,
414 (filename, [self.extra_dirs]),
415 "creating %s" % filename)
Greg Ward865de831999-09-21 18:31:14 +0000416 else:
Greg Warda233d862000-03-22 00:15:45 +0000417 self.warn (("path file '%s' not created for alternate or custom " +
418 "installation (path files only work with standard " +
419 "installations)") %
420 filename)
Greg Ward865de831999-09-21 18:31:14 +0000421
Greg Ward13ae1c81999-03-22 14:55:25 +0000422# class Install