blob: 00aa0720473148e4fb8017db91a2dfe4dce61496 [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
83 # Flags for 'build_py'
Greg Ward4f08e4f2000-02-26 00:49:04 +000084 #('compile-py', None, "compile .py to .pyc"),
85 #('optimize-py', None, "compile .py to .pyo (optimized)"),
Greg Wardbbeceea2000-02-18 00:25:39 +000086 ]
Greg Ward13ae1c81999-03-22 14:55:25 +000087
Greg Ward9a337071999-06-08 02:04:36 +000088
Greg Warde01149c2000-02-18 00:35:22 +000089 def initialize_options (self):
Greg Ward13ae1c81999-03-22 14:55:25 +000090
Greg Ward13ae1c81999-03-22 14:55:25 +000091 # Don't define 'prefix' or 'exec_prefix' so we can know when the
92 # command is run whether the user supplied values
93 self.prefix = None
94 self.exec_prefix = None
Greg Warda233d862000-03-22 00:15:45 +000095 self.home = None
96
97 self.install_base = None
98 self.install_platbase = None
Greg Ward13ae1c81999-03-22 14:55:25 +000099
Greg Ward13ae1c81999-03-22 14:55:25 +0000100 # The actual installation directories are determined only at
101 # run-time, so the user can supply just prefix (and exec_prefix?)
102 # as a base for everything else
Greg Warda233d862000-03-22 00:15:45 +0000103 self.install_purelib = None
Greg Ward13ae1c81999-03-22 14:55:25 +0000104 self.install_platlib = None
Greg Warda233d862000-03-22 00:15:45 +0000105 self.install_lib = None
106
107 self.install_scripts = None
108 self.install_data = None
Greg Ward13ae1c81999-03-22 14:55:25 +0000109
Greg Warda233d862000-03-22 00:15:45 +0000110 self.extra_path = None
111 self.install_path_file = 0
112
Greg Ward066af102000-03-22 00:30:54 +0000113 # These are only here as a conduit from the 'build' command to the
114 # 'install_*' commands that do the real work. ('build_base' isn't
115 # actually used anywhere, but it might be useful in future.) They
116 # are not user options, because if the user told the install
117 # command where the build directory is, that wouldn't affect the
118 # build command.
119 self.build_base = None
120 self.build_lib = None
121
Greg Warda233d862000-03-22 00:15:45 +0000122 #self.install_man = None
123 #self.install_html = None
124 #self.install_info = None
Greg Ward13ae1c81999-03-22 14:55:25 +0000125
Greg Ward0f726951999-05-02 21:39:13 +0000126 self.compile_py = 1
127 self.optimize_py = 1
128
Greg Ward13ae1c81999-03-22 14:55:25 +0000129
Greg Warde01149c2000-02-18 00:35:22 +0000130 def finalize_options (self):
Greg Ward13ae1c81999-03-22 14:55:25 +0000131
Greg Ward9a337071999-06-08 02:04:36 +0000132 # XXX this method is where the default installation directories
133 # for modules and extension modules are determined. (Someday,
134 # the default installation directories for scripts,
135 # documentation, and whatever else the Distutils can build and
136 # install will also be determined here.) Thus, this is a pretty
137 # important place to fiddle with for anyone interested in
138 # installation schemes for the Python library. Issues that
139 # are not yet resolved to my satisfaction:
140 # * how much platform dependence should be here, and
141 # how much can be pushed off to sysconfig (or, better, the
142 # Makefiles parsed by sysconfig)?
143 # * how independent of Python version should this be -- ie.
144 # should we have special cases to handle Python 1.5 and
145 # older, and then do it "the right way" for 1.6? Or should
146 # we install a site.py along with Distutils under pre-1.6
147 # Python to take care of the current deficiencies in
148 # Python's library installation scheme?
149 #
150 # Currently, this method has hacks to distinguish POSIX from
151 # non-POSIX systems (for installation of site-local modules),
152 # and assumes the Python 1.5 installation tree with no site.py
153 # to fix things.
154
Greg Ward13ae1c81999-03-22 14:55:25 +0000155 # Figure out actual installation directories; the basic principle
Greg Ward4f08e4f2000-02-26 00:49:04 +0000156 # is: ...
Greg Ward13ae1c81999-03-22 14:55:25 +0000157
Greg Warda233d862000-03-22 00:15:45 +0000158
159
160 # Logic:
161 # * any: (prefix or exec-prefix or home) and (base or platbase)
162 # supplied: error
163 # * Windows/Mac OS: exec-prefix or home supplied: warn and ignore
164 #
165 # * Unix: home set
166 # (select the unix_home scheme)
167 # * Unix: neither prefix nor home set
168 # (set prefix=sys_prefix and carry on)
169 # * Unix: prefix set but not exec-prefix
170 # (set exec-prefix=prefix and carry on)
171 # * Unix: prefix set
172 # (select the unix_prefix scheme)
173 #
174 # * Windows/Mac OS: prefix not set
175 # (set prefix = sys_prefix and carry on)
176 # * Windows/Mac OS: prefix set
177 # (select the appropriate scheme)
178
179 # "select a scheme" means:
180 # - set install-base and install-platbase
181 # - subst. base/platbase/version into the values of the
182 # particular scheme dictionary
183 # - use the resultings strings to set install-lib, etc.
184
Greg Ward4f08e4f2000-02-26 00:49:04 +0000185 sys_prefix = os.path.normpath (sys.prefix)
186 sys_exec_prefix = os.path.normpath (sys.exec_prefix)
Greg Ward13ae1c81999-03-22 14:55:25 +0000187
Greg Warda233d862000-03-22 00:15:45 +0000188 # Check for errors/inconsistencies in the options
189 if ((self.prefix or self.exec_prefix or self.home) and
190 (self.install_base or self.install_platbase)):
191 raise DistutilsOptionError, \
192 ("must supply either prefix/exec-prefix/home or " +
193 "install-base/install-platbase -- not both")
Greg Ward4f08e4f2000-02-26 00:49:04 +0000194
195 if os.name == 'posix':
Greg Warda233d862000-03-22 00:15:45 +0000196 if self.home and (self.prefix or self.exec_prefix):
Greg Ward865de831999-09-21 18:31:14 +0000197 raise DistutilsOptionError, \
Greg Warda233d862000-03-22 00:15:45 +0000198 ("must supply either home or prefix/exec-prefix -- " +
199 "not both")
Greg Ward865de831999-09-21 18:31:14 +0000200 else:
Greg Warda233d862000-03-22 00:15:45 +0000201 if self.exec_prefix:
202 self.warn ("exec-prefix option ignored on this platform")
203 self.exec_prefix = None
204 if self.home:
205 self.warn ("home option ignored on this platform")
206 self.home = None
Greg Ward865de831999-09-21 18:31:14 +0000207
Greg Warda233d862000-03-22 00:15:45 +0000208 # Now the interesting logic -- so interesting that we farm it out
209 # to other methods. The goal of these methods is to set the final
210 # values for the install_{lib,scripts,data,...} options, using as
211 # input a heady brew of prefix, exec_prefix, home, install_base,
212 # install_platbase, user-supplied versions of
213 # install_{purelib,platlib,lib,scripts,data,...}, and the
214 # INSTALL_SCHEME dictionary above. Phew!
Greg Ward865de831999-09-21 18:31:14 +0000215
Greg Warda233d862000-03-22 00:15:45 +0000216 if os.name == 'posix':
217 self.finalize_unix ()
218 else:
219 self.finalize_other ()
220
221 # Expand "~" and configuration variables in the installation
222 # directories.
223 self.expand_dirs ()
224
225 # Pick the actual directory to install all modules to: either
226 # install_purelib or install_platlib, depending on whether this
227 # module distribution is pure or not. Of course, if the user
228 # already specified install_lib, use their selection.
229 if self.install_lib is None:
230 if self.distribution.ext_modules: # has extensions: non-pure
231 self.install_lib = self.install_platlib
232 else:
233 self.install_lib = self.install_purelib
234
235 # Well, we're not actually fully completely finalized yet: we still
236 # have to deal with 'extra_path', which is the hack for allowing
237 # non-packagized module distributions (hello, Numerical Python!) to
238 # get their own directories.
239 self.handle_extra_path ()
240 self.install_libbase = self.install_lib # needed for .pth file
241 self.install_lib = os.path.join (self.install_lib, self.extra_dirs)
Greg Ward865de831999-09-21 18:31:14 +0000242
Greg Ward4f08e4f2000-02-26 00:49:04 +0000243 # Figure out the build directories, ie. where to install from
Greg Ward4f08e4f2000-02-26 00:49:04 +0000244 self.set_undefined_options ('build',
245 ('build_base', 'build_base'),
Greg Ward066af102000-03-22 00:30:54 +0000246 ('build_lib', 'build_lib'))
Greg Ward13ae1c81999-03-22 14:55:25 +0000247
248 # Punt on doc directories for now -- after all, we're punting on
249 # documentation completely!
250
Greg Warde01149c2000-02-18 00:35:22 +0000251 # finalize_options ()
Greg Ward13ae1c81999-03-22 14:55:25 +0000252
253
Greg Warda233d862000-03-22 00:15:45 +0000254 def finalize_unix (self):
255
256 if self.install_base is not None or self.install_platbase is not None:
257 if ((self.install_lib is None and
258 self.install_purelib is None and
259 self.install_platlib is None) or
260 self.install_scripts is None or
261 self.install_data is None):
262 raise DistutilsOptionError, \
263 "install-base or install-platbase supplied, but " + \
264 "installation scheme is incomplete"
265
266 return
267
268 if self.home is not None:
269 self.install_base = self.install_platbase = self.home
270 self.select_scheme ("unix_home")
271 else:
272 if self.prefix is None:
273 if self.exec_prefix is not None:
274 raise DistutilsOptionError, \
275 "must not supply exec-prefix without prefix"
276
277 self.prefix = os.path.normpath (sys.prefix)
278 self.exec_prefix = os.path.normpath (sys.exec_prefix)
279 self.install_path_file = 1
280
281 else:
282 if self.exec_prefix is None:
283 self.exec_prefix = self.prefix
284
285
286 # XXX since we don't *know* that a user-supplied prefix really
287 # points to another Python installation, we can't be sure that
288 # writing a .pth file there will actually work -- so we don't
289 # try. That is, we only set 'install_path_file' if the user
290 # didn't supply prefix. There are certainly circumstances
291 # under which we *should* install a .pth file when the user
292 # supplies a prefix, namely when that prefix actually points to
293 # another Python installation. Hmmm.
294
295 self.install_base = self.prefix
296 self.install_platbase = self.exec_prefix
297 self.select_scheme ("unix_prefix")
298
299 # finalize_unix ()
300
301
302 def finalize_other (self): # Windows and Mac OS for now
303
304 if self.prefix is None:
305 self.prefix = os.path.normpath (sys.prefix)
306 self.install_path_file = 1
307
308 # XXX same caveat regarding 'install_path_file' as in
309 # 'finalize_unix()'.
310
311 self.install_base = self.install_platbase = self.prefix
312 try:
313 self.select_scheme (os.name)
314 except KeyError:
315 raise DistutilsPlatformError, \
316 "I don't know how to install stuff on '%s'" % os.name
317
318 # finalize_other ()
319
320
321 def select_scheme (self, name):
322
323 # it's the caller's problem if they supply a bad name!
324 scheme = INSTALL_SCHEMES[name]
325
326 vars = { 'base': self.install_base,
327 'platbase': self.install_platbase,
328 'py_version_short': sys.version[0:3],
329 }
330
331 for key in ('purelib', 'platlib', 'scripts', 'data'):
332 val = subst_vars (scheme[key], vars)
333 setattr (self, 'install_' + key, val)
334
335
336 def expand_dirs (self):
337
338 # XXX probably don't want to 'expanduser()' on Windows or Mac
339 # XXX should use 'util.subst_vars()' with our own set of
340 # configuration variables
341
342 for att in ('base', 'platbase',
343 'purelib', 'platlib', 'lib',
344 'scripts', 'data'):
345 fullname = "install_" + att
346 val = getattr (self, fullname)
347 if val is not None:
348 setattr (self, fullname,
349 os.path.expandvars (os.path.expanduser (val)))
350
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 Ward048ca7d2000-03-22 00:40:16 +0000391 # Now install all Python modules -- don't bother to make this
392 # conditional; why would someone distribute a Python module
393 # distribution without Python modules?
394 self.run_peer ('install_lib')
Greg Ward865de831999-09-21 18:31:14 +0000395
396 if self.path_file:
397 self.create_path_file ()
Greg Ward13ae1c81999-03-22 14:55:25 +0000398
Greg Warda233d862000-03-22 00:15:45 +0000399 normalized_path = map (os.path.normpath, sys.path)
400 if (not (self.path_file and self.install_path_file) and
401 os.path.normpath (self.install_lib) not in normalized_path):
402 self.warn (("modules installed to '%s', which is not in " +
403 "Python's module search path (sys.path) -- " +
404 "you'll have to change the search path yourself") %
405 self.install_lib)
406
Greg Ward13ae1c81999-03-22 14:55:25 +0000407 # run ()
408
Greg Ward865de831999-09-21 18:31:14 +0000409
410 def create_path_file (self):
Greg Warda233d862000-03-22 00:15:45 +0000411 filename = os.path.join (self.install_libbase,
412 self.path_file + ".pth")
413 if self.install_path_file:
414 self.execute (write_file,
415 (filename, [self.extra_dirs]),
416 "creating %s" % filename)
Greg Ward865de831999-09-21 18:31:14 +0000417 else:
Greg Warda233d862000-03-22 00:15:45 +0000418 self.warn (("path file '%s' not created for alternate or custom " +
419 "installation (path files only work with standard " +
420 "installations)") %
421 filename)
Greg Ward865de831999-09-21 18:31:14 +0000422
Greg Ward13ae1c81999-03-22 14:55:25 +0000423# class Install