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