blob: 0e5b01cc98c5142d12702b15047a04ff2f4e9187 [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
7__rcsid__ = "$Id$"
8
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 Ward865de831999-09-21 18:31:14 +000012from distutils.util import write_file
Greg Ward13ae1c81999-03-22 14:55:25 +000013
14
15class Install (Command):
16
Greg Ward37bc8152000-01-30 18:34:15 +000017 description = "install everything from build directory"
18
Greg Ward13ae1c81999-03-22 14:55:25 +000019 options = [('prefix=', None, "installation prefix"),
Greg Warde6ac2fc1999-09-29 12:38:18 +000020 ('exec-prefix=', None,
Greg Ward13ae1c81999-03-22 14:55:25 +000021 "prefix for platform-specific files"),
22
23 # Build directories: where to install from
24 ('build-base=', None,
25 "base build directory"),
26 ('build-lib=', None,
Greg Warde6ac2fc1999-09-29 12:38:18 +000027 "build directory for pure Python modules"),
Greg Ward13ae1c81999-03-22 14:55:25 +000028 ('build-platlib=', None,
Greg Warde6ac2fc1999-09-29 12:38:18 +000029 "build directory for extension modules"),
Greg Ward13ae1c81999-03-22 14:55:25 +000030
31 # Installation directories: where to put modules and packages
32 ('install-lib=', None,
33 "base Python library directory"),
34 ('install-platlib=', None,
35 "platform-specific Python library directory"),
36 ('install-site-lib=', None,
37 "directory for site-specific packages and modules"),
38 ('install-site-platlib=', None,
39 "platform-specific site directory"),
40 ('install-scheme=', None,
41 "install to 'system' or 'site' library directory?"),
Greg Ward865de831999-09-21 18:31:14 +000042 ('install-path=', None,
43 "extra intervening directories to put below install-lib"),
Greg Ward13ae1c81999-03-22 14:55:25 +000044
45 # Where to install documentation (eventually!)
46 ('doc-format=', None, "format of documentation to generate"),
47 ('install-man=', None, "directory for Unix man pages"),
48 ('install-html=', None, "directory for HTML documentation"),
49 ('install-info=', None, "directory for GNU info files"),
50
Greg Ward0f726951999-05-02 21:39:13 +000051 # Flags for 'build_py'
52 ('compile-py', None, "compile .py to .pyc"),
53 ('optimize-py', None, "compile .py to .pyo (optimized)"),
Greg Ward13ae1c81999-03-22 14:55:25 +000054 ]
55
Greg Ward9a337071999-06-08 02:04:36 +000056
Greg Ward13ae1c81999-03-22 14:55:25 +000057 def set_default_options (self):
58
59 self.build_base = None
60 self.build_lib = None
61 self.build_platlib = None
62
63 # Don't define 'prefix' or 'exec_prefix' so we can know when the
64 # command is run whether the user supplied values
65 self.prefix = None
66 self.exec_prefix = None
67
68 # These two, we can supply real values for! (because they're
69 # not directories, and don't have a confusing multitude of
70 # possible derivations)
71 #self.install_scheme = 'site'
72 self.doc_format = None
73
74 # The actual installation directories are determined only at
75 # run-time, so the user can supply just prefix (and exec_prefix?)
76 # as a base for everything else
77 self.install_lib = None
78 self.install_platlib = None
79 self.install_site_lib = None
80 self.install_site_platlib = None
Greg Ward865de831999-09-21 18:31:14 +000081 self.install_path = None
Greg Ward13ae1c81999-03-22 14:55:25 +000082
83 self.install_man = None
84 self.install_html = None
85 self.install_info = None
86
Greg Ward0f726951999-05-02 21:39:13 +000087 self.compile_py = 1
88 self.optimize_py = 1
89
Greg Ward13ae1c81999-03-22 14:55:25 +000090
91 def set_final_options (self):
92
Greg Ward9a337071999-06-08 02:04:36 +000093 # XXX this method is where the default installation directories
94 # for modules and extension modules are determined. (Someday,
95 # the default installation directories for scripts,
96 # documentation, and whatever else the Distutils can build and
97 # install will also be determined here.) Thus, this is a pretty
98 # important place to fiddle with for anyone interested in
99 # installation schemes for the Python library. Issues that
100 # are not yet resolved to my satisfaction:
101 # * how much platform dependence should be here, and
102 # how much can be pushed off to sysconfig (or, better, the
103 # Makefiles parsed by sysconfig)?
104 # * how independent of Python version should this be -- ie.
105 # should we have special cases to handle Python 1.5 and
106 # older, and then do it "the right way" for 1.6? Or should
107 # we install a site.py along with Distutils under pre-1.6
108 # Python to take care of the current deficiencies in
109 # Python's library installation scheme?
110 #
111 # Currently, this method has hacks to distinguish POSIX from
112 # non-POSIX systems (for installation of site-local modules),
113 # and assumes the Python 1.5 installation tree with no site.py
114 # to fix things.
115
Greg Ward13ae1c81999-03-22 14:55:25 +0000116 # Figure out the build directories, ie. where to install from
Greg Warde6ac2fc1999-09-29 12:38:18 +0000117 self.set_peer_option ('build', 'build_base', self.build_base)
Greg Ward13ae1c81999-03-22 14:55:25 +0000118 self.set_undefined_options ('build',
Greg Warde6ac2fc1999-09-29 12:38:18 +0000119 ('build_base', 'build_base'),
120 ('build_lib', 'build_lib'),
121 ('build_platlib', 'build_platlib'))
Greg Ward13ae1c81999-03-22 14:55:25 +0000122
123 # Figure out actual installation directories; the basic principle
124 # is: if the user supplied nothing, then use the directories that
125 # Python was built and installed with (ie. the compiled-in prefix
126 # and exec_prefix, and the actual installation directories gleaned
127 # by sysconfig). If the user supplied a prefix (and possibly
128 # exec_prefix), then we generate our own installation directories,
129 # following any pattern gleaned from sysconfig's findings. If no
130 # such pattern can be gleaned, then we'll just make do and try to
131 # ape the behaviour of Python's configure script.
132
133 if self.prefix is None: # user didn't override
Greg Wardc27d8002000-01-17 16:25:59 +0000134 self.prefix = os.path.normpath (sys.prefix)
Greg Ward13ae1c81999-03-22 14:55:25 +0000135 if self.exec_prefix is None:
Greg Wardc27d8002000-01-17 16:25:59 +0000136 self.exec_prefix = os.path.normpath (sys.exec_prefix)
Greg Ward13ae1c81999-03-22 14:55:25 +0000137
138 if self.install_lib is None:
139 self.install_lib = \
140 self.replace_sys_prefix ('LIBDEST', ('lib','python1.5'))
141 if self.install_platlib is None:
142 # XXX this should probably be DESTSHARED -- but why is there no
143 # equivalent to DESTSHARED for the "site-packages" dir"?
144 self.install_platlib = \
145 self.replace_sys_prefix ('BINLIBDEST', ('lib','python1.5'), 1)
146
Greg Ward9a337071999-06-08 02:04:36 +0000147
Greg Wardc9c011c1999-09-13 13:57:26 +0000148 # Here is where we decide where to install most library files: on
149 # POSIX systems, they go to 'site-packages' under the install_lib
150 # (determined above -- typically /usr/local/lib/python1.x). Note
151 # that on POSIX systems, platform-specific files belong in
152 # 'site-packages' under install_platlib. (The actual rule is that
153 # a module distribution that includes *any* platform-specific files
154 # -- ie. extension modules -- goes under install_platlib. This
155 # solves the "can't find extension module in a package" problem.)
156 # On non-POSIX systems, install_lib and install_platlib are the
157 # same (eg. "C:\Program Files\Python\Lib" on Windows), as are
158 # install_site_lib and install_site_platlib (eg.
159 # "C:\Program Files\Python" on Windows) -- everything will be dumped
160 # right into one of the install_site directories. (It doesn't
161 # really matter *which* one, of course, but I'll observe decorum
162 # and do it properly.)
Greg Ward9a337071999-06-08 02:04:36 +0000163
Greg Ward865de831999-09-21 18:31:14 +0000164 # 'base' and 'platbase' are the base directories for installing
165 # site-local files, eg. "/usr/local/lib/python1.5/site-packages"
166 # or "C:\Program Files\Python"
167 if os.name == 'posix':
168 self.base = os.path.join (self.install_lib,
169 'site-packages')
170 self.platbase = os.path.join (self.install_platlib,
171 'site-packages')
172 else:
173 self.base = self.prefix
174 self.platbase = self.exec_prefix
175
176 # 'path_file' and 'extra_dirs' are how we handle distributions
177 # that need to be installed to their own directory, but aren't
178 # package-ized yet. 'extra_dirs' is just a directory under
179 # 'base' or 'platbase' where toplevel modules will actually be
180 # installed; 'path_file' is the basename of a .pth file to drop
181 # in 'base' or 'platbase' (depending on the distribution). Very
182 # often they will be the same, which is why we allow them to be
183 # supplied as a string or 1-tuple as well as a 2-element
184 # comma-separated string or a 2-tuple.
185 if self.install_path is None:
186 self.install_path = self.distribution.install_path
187
188 if self.install_path is not None:
189 if type (self.install_path) is StringType:
190 self.install_path = string.split (self.install_path, ',')
191
192 if len (self.install_path) == 1:
193 path_file = extra_dirs = self.install_path[0]
194 elif len (self.install_path) == 2:
195 (path_file, extra_dirs) = self.install_path
Greg Ward9a337071999-06-08 02:04:36 +0000196 else:
Greg Ward865de831999-09-21 18:31:14 +0000197 raise DistutilsOptionError, \
198 "'install_path' option must be a list, tuple, or " + \
199 "comma-separated string with 1 or 2 elements"
200
201 # install path has slashes in it -- might need to convert to
202 # local form
203 if string.find (extra_dirs, '/') and os.name != "posix":
204 extra_dirs = string.split (extra_dirs, '/')
205 extra_dirs = apply (os.path.join, extra_dirs)
206 else:
207 path_file = None
208 extra_dirs = ''
209
210 # XXX should we warn if path_file and not extra_dirs (in which case
211 # the path file would be harmless but pointless)
212 self.path_file = path_file
213 self.extra_dirs = extra_dirs
214
215
216 if self.install_site_lib is None:
217 self.install_site_lib = os.path.join (self.base,
218 extra_dirs)
Greg Ward9a337071999-06-08 02:04:36 +0000219
Greg Ward13ae1c81999-03-22 14:55:25 +0000220 if self.install_site_platlib is None:
Greg Ward865de831999-09-21 18:31:14 +0000221 self.install_site_platlib = os.path.join (self.platbase,
222 extra_dirs)
Greg Ward13ae1c81999-03-22 14:55:25 +0000223
224 #if self.install_scheme == 'site':
225 # install_lib = self.install_site_lib
226 # install_platlib = self.install_site_platlib
227 #elif self.install_scheme == 'system':
228 # install_lib = self.install_lib
229 # install_platlib = self.install_platlib
230 #else:
231 # # XXX new exception for this kind of misbehaviour?
232 # raise DistutilsArgError, \
233 # "invalid install scheme '%s'" % self.install_scheme
234
235
236 # Punt on doc directories for now -- after all, we're punting on
237 # documentation completely!
238
239 # set_final_options ()
240
241
242 def replace_sys_prefix (self, config_attr, fallback_postfix, use_exec=0):
243 """Attempts to glean a simple pattern from an installation
244 directory available as a 'sysconfig' attribute: if the
245 directory name starts with the "system prefix" (the one
246 hard-coded in the Makefile and compiled into Python),
247 then replace it with the current installation prefix and
248 return the "relocated" installation directory."""
249
Greg Ward37bc8152000-01-30 18:34:15 +0000250 from distutils import sysconfig
251
Greg Ward13ae1c81999-03-22 14:55:25 +0000252 if use_exec:
Greg Wardc27d8002000-01-17 16:25:59 +0000253 sys_prefix = os.path.normpath (sys.exec_prefix)
Greg Ward13ae1c81999-03-22 14:55:25 +0000254 my_prefix = self.exec_prefix
255 else:
Greg Wardc27d8002000-01-17 16:25:59 +0000256 sys_prefix = os.path.normpath (sys.prefix)
Greg Ward13ae1c81999-03-22 14:55:25 +0000257 my_prefix = self.prefix
258
259 val = getattr (sysconfig, config_attr)
260 if string.find (val, sys_prefix) == 0:
261 # If the sysconfig directory starts with the system prefix,
262 # then we can "relocate" it to the user-supplied prefix --
263 # assuming, of course, it is different from the system prefix.
264
265 if sys_prefix == my_prefix:
266 return val
267 else:
268 return my_prefix + val[len(sys_prefix):]
269
270 else:
271 # Otherwise, just tack the "fallback postfix" onto the
272 # user-specified prefix.
273
Greg Ward1016af91999-08-19 20:02:10 +0000274 return apply (os.path.join, (my_prefix,) + fallback_postfix)
Greg Ward13ae1c81999-03-22 14:55:25 +0000275
276 # replace_sys_prefix ()
277
278
279 def run (self):
280
Greg Ward9a337071999-06-08 02:04:36 +0000281 # Obviously have to build before we can install
282 self.run_peer ('build')
283
Greg Ward13ae1c81999-03-22 14:55:25 +0000284 # Install modules in two steps: "platform-shared" files (ie. pure
285 # python modules) and platform-specific files (compiled C
Greg Wardc9c011c1999-09-13 13:57:26 +0000286 # extensions). Note that 'install_py' is smart enough to install
287 # pure Python modules in the "platlib" directory if we built any
288 # extensions.
Greg Ward865de831999-09-21 18:31:14 +0000289 if self.distribution.packages or self.distribution.py_modules:
290 self.run_peer ('install_py')
291 if self.distribution.ext_modules:
292 self.run_peer ('install_ext')
293
294 if self.path_file:
295 self.create_path_file ()
Greg Ward13ae1c81999-03-22 14:55:25 +0000296
297 # run ()
298
Greg Ward865de831999-09-21 18:31:14 +0000299
300 def create_path_file (self):
301
302 if self.distribution.ext_modules:
303 base = self.platbase
304 else:
305 base = self.base
306
307 filename = os.path.join (base, self.path_file + ".pth")
308 self.execute (write_file,
309 (filename, [self.extra_dirs]),
310 "creating %s" % filename)
311
312
Greg Ward13ae1c81999-03-22 14:55:25 +0000313# class Install