blob: 13baa1a7568ac4ecd928b7d2def7741cbb13e683 [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
77 # Build directories: where to find the files to install
Greg Wardbbeceea2000-02-18 00:25:39 +000078 ('build-base=', None,
79 "base build directory"),
80 ('build-lib=', None,
Greg Warda233d862000-03-22 00:15:45 +000081 "build directory for all Python modules"),
Greg Ward13ae1c81999-03-22 14:55:25 +000082
Greg Wardbbeceea2000-02-18 00:25:39 +000083 # Where to install documentation (eventually!)
Greg Ward4f08e4f2000-02-26 00:49:04 +000084 #('doc-format=', None, "format of documentation to generate"),
85 #('install-man=', None, "directory for Unix man pages"),
86 #('install-html=', None, "directory for HTML documentation"),
87 #('install-info=', None, "directory for GNU info files"),
Greg Wardbbeceea2000-02-18 00:25:39 +000088
89 # Flags for 'build_py'
Greg Ward4f08e4f2000-02-26 00:49:04 +000090 #('compile-py', None, "compile .py to .pyc"),
91 #('optimize-py', None, "compile .py to .pyo (optimized)"),
Greg Wardbbeceea2000-02-18 00:25:39 +000092 ]
Greg Ward13ae1c81999-03-22 14:55:25 +000093
Greg Ward9a337071999-06-08 02:04:36 +000094
Greg Warde01149c2000-02-18 00:35:22 +000095 def initialize_options (self):
Greg Ward13ae1c81999-03-22 14:55:25 +000096
Greg Ward13ae1c81999-03-22 14:55:25 +000097 # Don't define 'prefix' or 'exec_prefix' so we can know when the
98 # command is run whether the user supplied values
99 self.prefix = None
100 self.exec_prefix = None
Greg Warda233d862000-03-22 00:15:45 +0000101 self.home = None
102
103 self.install_base = None
104 self.install_platbase = None
Greg Ward13ae1c81999-03-22 14:55:25 +0000105
Greg Ward13ae1c81999-03-22 14:55:25 +0000106 # The actual installation directories are determined only at
107 # run-time, so the user can supply just prefix (and exec_prefix?)
108 # as a base for everything else
Greg Warda233d862000-03-22 00:15:45 +0000109 self.install_purelib = None
Greg Ward13ae1c81999-03-22 14:55:25 +0000110 self.install_platlib = None
Greg Warda233d862000-03-22 00:15:45 +0000111 self.install_lib = None
112
113 self.install_scripts = None
114 self.install_data = None
Greg Ward13ae1c81999-03-22 14:55:25 +0000115
Greg Ward4f08e4f2000-02-26 00:49:04 +0000116 self.build_base = None
117 self.build_lib = None
118 self.build_platlib = None
119
Greg Warda233d862000-03-22 00:15:45 +0000120 self.extra_path = None
121 self.install_path_file = 0
122
123 #self.install_man = None
124 #self.install_html = None
125 #self.install_info = None
Greg Ward13ae1c81999-03-22 14:55:25 +0000126
Greg Ward0f726951999-05-02 21:39:13 +0000127 self.compile_py = 1
128 self.optimize_py = 1
129
Greg Ward13ae1c81999-03-22 14:55:25 +0000130
Greg Warde01149c2000-02-18 00:35:22 +0000131 def finalize_options (self):
Greg Ward13ae1c81999-03-22 14:55:25 +0000132
Greg Ward9a337071999-06-08 02:04:36 +0000133 # XXX this method is where the default installation directories
134 # for modules and extension modules are determined. (Someday,
135 # the default installation directories for scripts,
136 # documentation, and whatever else the Distutils can build and
137 # install will also be determined here.) Thus, this is a pretty
138 # important place to fiddle with for anyone interested in
139 # installation schemes for the Python library. Issues that
140 # are not yet resolved to my satisfaction:
141 # * how much platform dependence should be here, and
142 # how much can be pushed off to sysconfig (or, better, the
143 # Makefiles parsed by sysconfig)?
144 # * how independent of Python version should this be -- ie.
145 # should we have special cases to handle Python 1.5 and
146 # older, and then do it "the right way" for 1.6? Or should
147 # we install a site.py along with Distutils under pre-1.6
148 # Python to take care of the current deficiencies in
149 # Python's library installation scheme?
150 #
151 # Currently, this method has hacks to distinguish POSIX from
152 # non-POSIX systems (for installation of site-local modules),
153 # and assumes the Python 1.5 installation tree with no site.py
154 # to fix things.
155
Greg Ward13ae1c81999-03-22 14:55:25 +0000156 # Figure out actual installation directories; the basic principle
Greg Ward4f08e4f2000-02-26 00:49:04 +0000157 # is: ...
Greg Ward13ae1c81999-03-22 14:55:25 +0000158
Greg Warda233d862000-03-22 00:15:45 +0000159
160
161 # Logic:
162 # * any: (prefix or exec-prefix or home) and (base or platbase)
163 # supplied: error
164 # * Windows/Mac OS: exec-prefix or home supplied: warn and ignore
165 #
166 # * Unix: home set
167 # (select the unix_home scheme)
168 # * Unix: neither prefix nor home set
169 # (set prefix=sys_prefix and carry on)
170 # * Unix: prefix set but not exec-prefix
171 # (set exec-prefix=prefix and carry on)
172 # * Unix: prefix set
173 # (select the unix_prefix scheme)
174 #
175 # * Windows/Mac OS: prefix not set
176 # (set prefix = sys_prefix and carry on)
177 # * Windows/Mac OS: prefix set
178 # (select the appropriate scheme)
179
180 # "select a scheme" means:
181 # - set install-base and install-platbase
182 # - subst. base/platbase/version into the values of the
183 # particular scheme dictionary
184 # - use the resultings strings to set install-lib, etc.
185
Greg Ward4f08e4f2000-02-26 00:49:04 +0000186 sys_prefix = os.path.normpath (sys.prefix)
187 sys_exec_prefix = os.path.normpath (sys.exec_prefix)
Greg Ward13ae1c81999-03-22 14:55:25 +0000188
Greg Warda233d862000-03-22 00:15:45 +0000189 # Check for errors/inconsistencies in the options
190 if ((self.prefix or self.exec_prefix or self.home) and
191 (self.install_base or self.install_platbase)):
192 raise DistutilsOptionError, \
193 ("must supply either prefix/exec-prefix/home or " +
194 "install-base/install-platbase -- not both")
Greg Ward4f08e4f2000-02-26 00:49:04 +0000195
196 if os.name == 'posix':
Greg Warda233d862000-03-22 00:15:45 +0000197 if self.home and (self.prefix or self.exec_prefix):
Greg Ward865de831999-09-21 18:31:14 +0000198 raise DistutilsOptionError, \
Greg Warda233d862000-03-22 00:15:45 +0000199 ("must supply either home or prefix/exec-prefix -- " +
200 "not both")
Greg Ward865de831999-09-21 18:31:14 +0000201 else:
Greg Warda233d862000-03-22 00:15:45 +0000202 if self.exec_prefix:
203 self.warn ("exec-prefix option ignored on this platform")
204 self.exec_prefix = None
205 if self.home:
206 self.warn ("home option ignored on this platform")
207 self.home = None
Greg Ward865de831999-09-21 18:31:14 +0000208
Greg Warda233d862000-03-22 00:15:45 +0000209 # Now the interesting logic -- so interesting that we farm it out
210 # to other methods. The goal of these methods is to set the final
211 # values for the install_{lib,scripts,data,...} options, using as
212 # input a heady brew of prefix, exec_prefix, home, install_base,
213 # install_platbase, user-supplied versions of
214 # install_{purelib,platlib,lib,scripts,data,...}, and the
215 # INSTALL_SCHEME dictionary above. Phew!
Greg Ward865de831999-09-21 18:31:14 +0000216
Greg Warda233d862000-03-22 00:15:45 +0000217 if os.name == 'posix':
218 self.finalize_unix ()
219 else:
220 self.finalize_other ()
221
222 # Expand "~" and configuration variables in the installation
223 # directories.
224 self.expand_dirs ()
225
226 # 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 Ward4f08e4f2000-02-26 00:49:04 +0000244 # Figure out the build directories, ie. where to install from
245 self.set_peer_option ('build', 'build_base', self.build_base)
246 self.set_undefined_options ('build',
247 ('build_base', 'build_base'),
248 ('build_lib', 'build_lib'),
249 ('build_platlib', 'build_platlib'))
Greg Ward13ae1c81999-03-22 14:55:25 +0000250
251 # Punt on doc directories for now -- after all, we're punting on
252 # documentation completely!
253
Greg Warde01149c2000-02-18 00:35:22 +0000254 # finalize_options ()
Greg Ward13ae1c81999-03-22 14:55:25 +0000255
256
Greg Warda233d862000-03-22 00:15:45 +0000257 def finalize_unix (self):
258
259 if self.install_base is not None or self.install_platbase is not None:
260 if ((self.install_lib is None and
261 self.install_purelib is None and
262 self.install_platlib is None) or
263 self.install_scripts is None or
264 self.install_data is None):
265 raise DistutilsOptionError, \
266 "install-base or install-platbase supplied, but " + \
267 "installation scheme is incomplete"
268
269 return
270
271 if self.home is not None:
272 self.install_base = self.install_platbase = self.home
273 self.select_scheme ("unix_home")
274 else:
275 if self.prefix is None:
276 if self.exec_prefix is not None:
277 raise DistutilsOptionError, \
278 "must not supply exec-prefix without prefix"
279
280 self.prefix = os.path.normpath (sys.prefix)
281 self.exec_prefix = os.path.normpath (sys.exec_prefix)
282 self.install_path_file = 1
283
284 else:
285 if self.exec_prefix is None:
286 self.exec_prefix = self.prefix
287
288
289 # XXX since we don't *know* that a user-supplied prefix really
290 # points to another Python installation, we can't be sure that
291 # writing a .pth file there will actually work -- so we don't
292 # try. That is, we only set 'install_path_file' if the user
293 # didn't supply prefix. There are certainly circumstances
294 # under which we *should* install a .pth file when the user
295 # supplies a prefix, namely when that prefix actually points to
296 # another Python installation. Hmmm.
297
298 self.install_base = self.prefix
299 self.install_platbase = self.exec_prefix
300 self.select_scheme ("unix_prefix")
301
302 # finalize_unix ()
303
304
305 def finalize_other (self): # Windows and Mac OS for now
306
307 if self.prefix is None:
308 self.prefix = os.path.normpath (sys.prefix)
309 self.install_path_file = 1
310
311 # XXX same caveat regarding 'install_path_file' as in
312 # 'finalize_unix()'.
313
314 self.install_base = self.install_platbase = self.prefix
315 try:
316 self.select_scheme (os.name)
317 except KeyError:
318 raise DistutilsPlatformError, \
319 "I don't know how to install stuff on '%s'" % os.name
320
321 # finalize_other ()
322
323
324 def select_scheme (self, name):
325
326 # it's the caller's problem if they supply a bad name!
327 scheme = INSTALL_SCHEMES[name]
328
329 vars = { 'base': self.install_base,
330 'platbase': self.install_platbase,
331 'py_version_short': sys.version[0:3],
332 }
333
334 for key in ('purelib', 'platlib', 'scripts', 'data'):
335 val = subst_vars (scheme[key], vars)
336 setattr (self, 'install_' + key, val)
337
338
339 def expand_dirs (self):
340
341 # XXX probably don't want to 'expanduser()' on Windows or Mac
342 # XXX should use 'util.subst_vars()' with our own set of
343 # configuration variables
344
345 for att in ('base', 'platbase',
346 'purelib', 'platlib', 'lib',
347 'scripts', 'data'):
348 fullname = "install_" + att
349 val = getattr (self, fullname)
350 if val is not None:
351 setattr (self, fullname,
352 os.path.expandvars (os.path.expanduser (val)))
353
354
355 def handle_extra_path (self):
356
357 if self.extra_path is None:
358 self.extra_path = self.distribution.extra_path
359
360 if self.extra_path is not None:
361 if type (self.extra_path) is StringType:
362 self.extra_path = string.split (self.extra_path, ',')
363
364 if len (self.extra_path) == 1:
365 path_file = extra_dirs = self.extra_path[0]
366 elif len (self.extra_path) == 2:
367 (path_file, extra_dirs) = self.extra_path
368 else:
369 raise DistutilsOptionError, \
370 "'extra_path' option must be a list, tuple, or " + \
371 "comma-separated string with 1 or 2 elements"
372
373 # convert to local form in case Unix notation used (as it
374 # should be in setup scripts)
375 extra_dirs = native_path (extra_dirs)
376
377 else:
378 path_file = None
379 extra_dirs = ''
380
381 # XXX should we warn if path_file and not extra_dirs? (in which
382 # case the path file would be harmless but pointless)
383 self.path_file = path_file
384 self.extra_dirs = extra_dirs
385
386 # handle_extra_path ()
387
388
Greg Ward13ae1c81999-03-22 14:55:25 +0000389 def run (self):
390
Greg Ward9a337071999-06-08 02:04:36 +0000391 # Obviously have to build before we can install
392 self.run_peer ('build')
393
Greg Ward13ae1c81999-03-22 14:55:25 +0000394 # Install modules in two steps: "platform-shared" files (ie. pure
Greg Ward4f08e4f2000-02-26 00:49:04 +0000395 # Python modules) and platform-specific files (compiled C
Greg Wardc9c011c1999-09-13 13:57:26 +0000396 # extensions). Note that 'install_py' is smart enough to install
397 # pure Python modules in the "platlib" directory if we built any
398 # extensions.
Greg Warda233d862000-03-22 00:15:45 +0000399
400 # XXX this should become one command, 'install_lib', since
401 # all modules are "built" into the same directory now
402
Greg Ward865de831999-09-21 18:31:14 +0000403 if self.distribution.packages or self.distribution.py_modules:
404 self.run_peer ('install_py')
Greg Warda233d862000-03-22 00:15:45 +0000405 #if self.distribution.ext_modules:
406 # self.run_peer ('install_ext')
Greg Ward865de831999-09-21 18:31:14 +0000407
408 if self.path_file:
409 self.create_path_file ()
Greg Ward13ae1c81999-03-22 14:55:25 +0000410
Greg Warda233d862000-03-22 00:15:45 +0000411 normalized_path = map (os.path.normpath, sys.path)
412 if (not (self.path_file and self.install_path_file) and
413 os.path.normpath (self.install_lib) not in normalized_path):
414 self.warn (("modules installed to '%s', which is not in " +
415 "Python's module search path (sys.path) -- " +
416 "you'll have to change the search path yourself") %
417 self.install_lib)
418
Greg Ward13ae1c81999-03-22 14:55:25 +0000419 # run ()
420
Greg Ward865de831999-09-21 18:31:14 +0000421
422 def create_path_file (self):
Greg Warda233d862000-03-22 00:15:45 +0000423 filename = os.path.join (self.install_libbase,
424 self.path_file + ".pth")
425 if self.install_path_file:
426 self.execute (write_file,
427 (filename, [self.extra_dirs]),
428 "creating %s" % filename)
Greg Ward865de831999-09-21 18:31:14 +0000429 else:
Greg Warda233d862000-03-22 00:15:45 +0000430 self.warn (("path file '%s' not created for alternate or custom " +
431 "installation (path files only work with standard " +
432 "installations)") %
433 filename)
Greg Ward865de831999-09-21 18:31:14 +0000434
Greg Ward13ae1c81999-03-22 14:55:25 +0000435# class Install