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