blob: ed74971efa76e302c227514ca4dc25ef72617b88 [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 Warde01149c2000-02-18 00:35:22 +000085 def initialize_options (self):
Greg Ward13ae1c81999-03-22 14:55:25 +000086
Greg Ward790c1102000-03-22 00:51:18 +000087 # High-level options: these select both an installation base
88 # and scheme.
Greg Ward13ae1c81999-03-22 14:55:25 +000089 self.prefix = None
90 self.exec_prefix = None
Greg Warda233d862000-03-22 00:15:45 +000091 self.home = None
92
Greg Ward790c1102000-03-22 00:51:18 +000093 # These select only the installation base; it's up to the user to
94 # specify the installation scheme (currently, that means supplying
95 # the --install-{platlib,purelib,scripts,data} options).
Greg Warda233d862000-03-22 00:15:45 +000096 self.install_base = None
97 self.install_platbase = None
Greg Ward13ae1c81999-03-22 14:55:25 +000098
Greg Ward790c1102000-03-22 00:51:18 +000099 # These options are the actual installation directories; if not
100 # supplied by the user, they are filled in using the installation
101 # scheme implied by prefix/exec-prefix/home and the contents of
102 # that installation scheme.
103 self.install_purelib = None # for pure module distributions
104 self.install_platlib = None # non-pure (dists w/ extensions)
105 self.install_lib = None # set to either purelib or platlib
Greg Warda233d862000-03-22 00:15:45 +0000106 self.install_scripts = None
107 self.install_data = None
Greg Ward13ae1c81999-03-22 14:55:25 +0000108
Greg Ward790c1102000-03-22 00:51:18 +0000109 # These two are for putting non-packagized distributions into their
110 # own directory and creating a .pth file if it makes sense.
111 # 'extra_path' comes from the setup file; 'install_path_file' is
112 # set only if we determine that it makes sense to install a path
113 # file.
Greg Warda233d862000-03-22 00:15:45 +0000114 self.extra_path = None
115 self.install_path_file = 0
116
Greg Ward066af102000-03-22 00:30:54 +0000117 # These are only here as a conduit from the 'build' command to the
118 # 'install_*' commands that do the real work. ('build_base' isn't
119 # actually used anywhere, but it might be useful in future.) They
120 # are not user options, because if the user told the install
121 # command where the build directory is, that wouldn't affect the
122 # build command.
123 self.build_base = None
124 self.build_lib = None
125
Greg Ward790c1102000-03-22 00:51:18 +0000126 # Not defined yet because we don't know anything about
127 # documentation yet.
Greg Warda233d862000-03-22 00:15:45 +0000128 #self.install_man = None
129 #self.install_html = None
130 #self.install_info = None
Greg Ward13ae1c81999-03-22 14:55:25 +0000131
Greg Ward13ae1c81999-03-22 14:55:25 +0000132
Greg Warde01149c2000-02-18 00:35:22 +0000133 def finalize_options (self):
Greg Ward13ae1c81999-03-22 14:55:25 +0000134
Greg Ward790c1102000-03-22 00:51:18 +0000135 # This method (and its pliant slaves, like 'finalize_unix()',
136 # 'finalize_other()', and 'select_scheme()') is where the default
137 # installation directories for modules, extension modules, and
138 # anything else we care to install from a Python module
139 # distribution. Thus, this code makes a pretty important policy
140 # statement about how third-party stuff is added to a Python
141 # installation! Note that the actual work of installation is done
142 # by the relatively simple 'install_*' commands; they just take
143 # their orders from the installation directory options determined
144 # here.
Greg Ward9a337071999-06-08 02:04:36 +0000145
Greg Ward790c1102000-03-22 00:51:18 +0000146 # Check for errors/inconsistencies in the options; first, stuff
147 # that's wrong on any platform.
Greg Ward13ae1c81999-03-22 14:55:25 +0000148
Greg Warda233d862000-03-22 00:15:45 +0000149 if ((self.prefix or self.exec_prefix or self.home) and
150 (self.install_base or self.install_platbase)):
151 raise DistutilsOptionError, \
152 ("must supply either prefix/exec-prefix/home or " +
153 "install-base/install-platbase -- not both")
Greg Ward4f08e4f2000-02-26 00:49:04 +0000154
Greg Ward790c1102000-03-22 00:51:18 +0000155 # Next, stuff that's wrong (or dubious) only on certain platforms.
Greg Ward4f08e4f2000-02-26 00:49:04 +0000156 if os.name == 'posix':
Greg Warda233d862000-03-22 00:15:45 +0000157 if self.home and (self.prefix or self.exec_prefix):
Greg Ward865de831999-09-21 18:31:14 +0000158 raise DistutilsOptionError, \
Greg Warda233d862000-03-22 00:15:45 +0000159 ("must supply either home or prefix/exec-prefix -- " +
160 "not both")
Greg Ward865de831999-09-21 18:31:14 +0000161 else:
Greg Warda233d862000-03-22 00:15:45 +0000162 if self.exec_prefix:
163 self.warn ("exec-prefix option ignored on this platform")
164 self.exec_prefix = None
165 if self.home:
166 self.warn ("home option ignored on this platform")
167 self.home = None
Greg Ward865de831999-09-21 18:31:14 +0000168
Greg Warda233d862000-03-22 00:15:45 +0000169 # Now the interesting logic -- so interesting that we farm it out
170 # to other methods. The goal of these methods is to set the final
171 # values for the install_{lib,scripts,data,...} options, using as
172 # input a heady brew of prefix, exec_prefix, home, install_base,
173 # install_platbase, user-supplied versions of
174 # install_{purelib,platlib,lib,scripts,data,...}, and the
175 # INSTALL_SCHEME dictionary above. Phew!
Greg Ward865de831999-09-21 18:31:14 +0000176
Greg Warda233d862000-03-22 00:15:45 +0000177 if os.name == 'posix':
178 self.finalize_unix ()
179 else:
180 self.finalize_other ()
181
182 # Expand "~" and configuration variables in the installation
183 # directories.
184 self.expand_dirs ()
185
186 # Pick the actual directory to install all modules to: either
187 # install_purelib or install_platlib, depending on whether this
188 # module distribution is pure or not. Of course, if the user
189 # already specified install_lib, use their selection.
190 if self.install_lib is None:
191 if self.distribution.ext_modules: # has extensions: non-pure
192 self.install_lib = self.install_platlib
193 else:
194 self.install_lib = self.install_purelib
195
196 # Well, we're not actually fully completely finalized yet: we still
197 # have to deal with 'extra_path', which is the hack for allowing
198 # non-packagized module distributions (hello, Numerical Python!) to
199 # get their own directories.
200 self.handle_extra_path ()
201 self.install_libbase = self.install_lib # needed for .pth file
202 self.install_lib = os.path.join (self.install_lib, self.extra_dirs)
Greg Ward865de831999-09-21 18:31:14 +0000203
Greg Ward790c1102000-03-22 00:51:18 +0000204 # Find out the build directories, ie. where to install from.
Greg Ward4f08e4f2000-02-26 00:49:04 +0000205 self.set_undefined_options ('build',
206 ('build_base', 'build_base'),
Greg Ward066af102000-03-22 00:30:54 +0000207 ('build_lib', 'build_lib'))
Greg Ward13ae1c81999-03-22 14:55:25 +0000208
209 # Punt on doc directories for now -- after all, we're punting on
210 # documentation completely!
211
Greg Warde01149c2000-02-18 00:35:22 +0000212 # finalize_options ()
Greg Ward13ae1c81999-03-22 14:55:25 +0000213
214
Greg Warda233d862000-03-22 00:15:45 +0000215 def finalize_unix (self):
216
217 if self.install_base is not None or self.install_platbase is not None:
218 if ((self.install_lib is None and
219 self.install_purelib is None and
220 self.install_platlib is None) or
221 self.install_scripts is None or
222 self.install_data is None):
223 raise DistutilsOptionError, \
224 "install-base or install-platbase supplied, but " + \
225 "installation scheme is incomplete"
226
227 return
228
229 if self.home is not None:
230 self.install_base = self.install_platbase = self.home
231 self.select_scheme ("unix_home")
232 else:
233 if self.prefix is None:
234 if self.exec_prefix is not None:
235 raise DistutilsOptionError, \
236 "must not supply exec-prefix without prefix"
237
238 self.prefix = os.path.normpath (sys.prefix)
239 self.exec_prefix = os.path.normpath (sys.exec_prefix)
240 self.install_path_file = 1
241
242 else:
243 if self.exec_prefix is None:
244 self.exec_prefix = self.prefix
245
246
247 # XXX since we don't *know* that a user-supplied prefix really
248 # points to another Python installation, we can't be sure that
249 # writing a .pth file there will actually work -- so we don't
250 # try. That is, we only set 'install_path_file' if the user
251 # didn't supply prefix. There are certainly circumstances
252 # under which we *should* install a .pth file when the user
253 # supplies a prefix, namely when that prefix actually points to
254 # another Python installation. Hmmm.
255
256 self.install_base = self.prefix
257 self.install_platbase = self.exec_prefix
258 self.select_scheme ("unix_prefix")
259
260 # finalize_unix ()
261
262
263 def finalize_other (self): # Windows and Mac OS for now
264
265 if self.prefix is None:
266 self.prefix = os.path.normpath (sys.prefix)
267 self.install_path_file = 1
268
269 # XXX same caveat regarding 'install_path_file' as in
270 # 'finalize_unix()'.
271
272 self.install_base = self.install_platbase = self.prefix
273 try:
274 self.select_scheme (os.name)
275 except KeyError:
276 raise DistutilsPlatformError, \
277 "I don't know how to install stuff on '%s'" % os.name
278
279 # finalize_other ()
280
281
282 def select_scheme (self, name):
283
Greg Ward790c1102000-03-22 00:51:18 +0000284 # "select a scheme" means:
285 # - set install-base and install-platbase
286 # - subst. base/platbase/version into the values of the
287 # particular scheme dictionary
288 # - use the resultings strings to set install-lib, etc.
289
Greg Warda233d862000-03-22 00:15:45 +0000290 # it's the caller's problem if they supply a bad name!
291 scheme = INSTALL_SCHEMES[name]
292
293 vars = { 'base': self.install_base,
294 'platbase': self.install_platbase,
295 'py_version_short': sys.version[0:3],
296 }
297
298 for key in ('purelib', 'platlib', 'scripts', 'data'):
299 val = subst_vars (scheme[key], vars)
300 setattr (self, 'install_' + key, val)
301
302
303 def expand_dirs (self):
304
305 # XXX probably don't want to 'expanduser()' on Windows or Mac
306 # XXX should use 'util.subst_vars()' with our own set of
307 # configuration variables
308
309 for att in ('base', 'platbase',
310 'purelib', 'platlib', 'lib',
311 'scripts', 'data'):
312 fullname = "install_" + att
313 val = getattr (self, fullname)
314 if val is not None:
315 setattr (self, fullname,
316 os.path.expandvars (os.path.expanduser (val)))
317
318
319 def handle_extra_path (self):
320
321 if self.extra_path is None:
322 self.extra_path = self.distribution.extra_path
323
324 if self.extra_path is not None:
325 if type (self.extra_path) is StringType:
326 self.extra_path = string.split (self.extra_path, ',')
327
328 if len (self.extra_path) == 1:
329 path_file = extra_dirs = self.extra_path[0]
330 elif len (self.extra_path) == 2:
331 (path_file, extra_dirs) = self.extra_path
332 else:
333 raise DistutilsOptionError, \
334 "'extra_path' option must be a list, tuple, or " + \
335 "comma-separated string with 1 or 2 elements"
336
337 # convert to local form in case Unix notation used (as it
338 # should be in setup scripts)
339 extra_dirs = native_path (extra_dirs)
340
341 else:
342 path_file = None
343 extra_dirs = ''
344
345 # XXX should we warn if path_file and not extra_dirs? (in which
346 # case the path file would be harmless but pointless)
347 self.path_file = path_file
348 self.extra_dirs = extra_dirs
349
350 # handle_extra_path ()
351
352
Greg Ward13ae1c81999-03-22 14:55:25 +0000353 def run (self):
354
Greg Ward9a337071999-06-08 02:04:36 +0000355 # Obviously have to build before we can install
356 self.run_peer ('build')
357
Greg Ward048ca7d2000-03-22 00:40:16 +0000358 # Now install all Python modules -- don't bother to make this
359 # conditional; why would someone distribute a Python module
360 # distribution without Python modules?
361 self.run_peer ('install_lib')
Greg Ward865de831999-09-21 18:31:14 +0000362
363 if self.path_file:
364 self.create_path_file ()
Greg Ward13ae1c81999-03-22 14:55:25 +0000365
Greg Warda233d862000-03-22 00:15:45 +0000366 normalized_path = map (os.path.normpath, sys.path)
367 if (not (self.path_file and self.install_path_file) and
368 os.path.normpath (self.install_lib) not in normalized_path):
369 self.warn (("modules installed to '%s', which is not in " +
370 "Python's module search path (sys.path) -- " +
371 "you'll have to change the search path yourself") %
372 self.install_lib)
373
Greg Ward13ae1c81999-03-22 14:55:25 +0000374 # run ()
375
Greg Ward865de831999-09-21 18:31:14 +0000376
377 def create_path_file (self):
Greg Warda233d862000-03-22 00:15:45 +0000378 filename = os.path.join (self.install_libbase,
379 self.path_file + ".pth")
380 if self.install_path_file:
381 self.execute (write_file,
382 (filename, [self.extra_dirs]),
383 "creating %s" % filename)
Greg Ward865de831999-09-21 18:31:14 +0000384 else:
Greg Warda233d862000-03-22 00:15:45 +0000385 self.warn (("path file '%s' not created for alternate or custom " +
386 "installation (path files only work with standard " +
387 "installations)") %
388 filename)
Greg Ward865de831999-09-21 18:31:14 +0000389
Greg Ward13ae1c81999-03-22 14:55:25 +0000390# class Install