blob: 1df558434a8689cf9e8c529a895d3f1fdc49f59d [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 Ward865de831999-09-21 18:31:14 +000012from distutils.util import write_file
Greg Ward4f08e4f2000-02-26 00:49:04 +000013from distutils.errors import DistutilsOptionError
Greg Ward13ae1c81999-03-22 14:55:25 +000014
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 Ward4f08e4f2000-02-26 00:49:04 +000024 # Installation directories: where to put modules and packages
25 ('install-lib=', None,
26 "base Python library directory"),
27 ('install-platlib=', None,
28 "platform-specific Python library directory"),
29 ('install-path=', None,
30 "extra intervening directories to put below install-lib"),
31
32 # Build directories: where to find the files to install
Greg Wardbbeceea2000-02-18 00:25:39 +000033 ('build-base=', None,
34 "base build directory"),
35 ('build-lib=', None,
36 "build directory for pure Python modules"),
37 ('build-platlib=', None,
38 "build directory for extension modules"),
Greg Ward13ae1c81999-03-22 14:55:25 +000039
Greg Wardbbeceea2000-02-18 00:25:39 +000040 # Where to install documentation (eventually!)
Greg Ward4f08e4f2000-02-26 00:49:04 +000041 #('doc-format=', None, "format of documentation to generate"),
42 #('install-man=', None, "directory for Unix man pages"),
43 #('install-html=', None, "directory for HTML documentation"),
44 #('install-info=', None, "directory for GNU info files"),
Greg Wardbbeceea2000-02-18 00:25:39 +000045
46 # Flags for 'build_py'
Greg Ward4f08e4f2000-02-26 00:49:04 +000047 #('compile-py', None, "compile .py to .pyc"),
48 #('optimize-py', None, "compile .py to .pyo (optimized)"),
Greg Wardbbeceea2000-02-18 00:25:39 +000049 ]
Greg Ward13ae1c81999-03-22 14:55:25 +000050
Greg Ward9a337071999-06-08 02:04:36 +000051
Greg Warde01149c2000-02-18 00:35:22 +000052 def initialize_options (self):
Greg Ward13ae1c81999-03-22 14:55:25 +000053
Greg Ward13ae1c81999-03-22 14:55:25 +000054 # Don't define 'prefix' or 'exec_prefix' so we can know when the
55 # command is run whether the user supplied values
56 self.prefix = None
57 self.exec_prefix = None
58
Greg Ward13ae1c81999-03-22 14:55:25 +000059 # The actual installation directories are determined only at
60 # run-time, so the user can supply just prefix (and exec_prefix?)
61 # as a base for everything else
62 self.install_lib = None
63 self.install_platlib = None
Greg Ward865de831999-09-21 18:31:14 +000064 self.install_path = None
Greg Ward13ae1c81999-03-22 14:55:25 +000065
Greg Ward4f08e4f2000-02-26 00:49:04 +000066 self.build_base = None
67 self.build_lib = None
68 self.build_platlib = None
69
Greg Ward13ae1c81999-03-22 14:55:25 +000070 self.install_man = None
71 self.install_html = None
72 self.install_info = None
73
Greg Ward0f726951999-05-02 21:39:13 +000074 self.compile_py = 1
75 self.optimize_py = 1
76
Greg Ward13ae1c81999-03-22 14:55:25 +000077
Greg Warde01149c2000-02-18 00:35:22 +000078 def finalize_options (self):
Greg Ward13ae1c81999-03-22 14:55:25 +000079
Greg Ward9a337071999-06-08 02:04:36 +000080 # XXX this method is where the default installation directories
81 # for modules and extension modules are determined. (Someday,
82 # the default installation directories for scripts,
83 # documentation, and whatever else the Distutils can build and
84 # install will also be determined here.) Thus, this is a pretty
85 # important place to fiddle with for anyone interested in
86 # installation schemes for the Python library. Issues that
87 # are not yet resolved to my satisfaction:
88 # * how much platform dependence should be here, and
89 # how much can be pushed off to sysconfig (or, better, the
90 # Makefiles parsed by sysconfig)?
91 # * how independent of Python version should this be -- ie.
92 # should we have special cases to handle Python 1.5 and
93 # older, and then do it "the right way" for 1.6? Or should
94 # we install a site.py along with Distutils under pre-1.6
95 # Python to take care of the current deficiencies in
96 # Python's library installation scheme?
97 #
98 # Currently, this method has hacks to distinguish POSIX from
99 # non-POSIX systems (for installation of site-local modules),
100 # and assumes the Python 1.5 installation tree with no site.py
101 # to fix things.
102
Greg Ward13ae1c81999-03-22 14:55:25 +0000103 # Figure out actual installation directories; the basic principle
Greg Ward4f08e4f2000-02-26 00:49:04 +0000104 # is: ...
Greg Ward13ae1c81999-03-22 14:55:25 +0000105
Greg Ward4f08e4f2000-02-26 00:49:04 +0000106 sys_prefix = os.path.normpath (sys.prefix)
107 sys_exec_prefix = os.path.normpath (sys.exec_prefix)
Greg Ward13ae1c81999-03-22 14:55:25 +0000108
Greg Ward4f08e4f2000-02-26 00:49:04 +0000109 if self.prefix is None:
110 if self.exec_prefix is not None:
111 raise DistutilsOptionError, \
112 "you may not supply exec_prefix without prefix"
113 self.prefix = sys_prefix
Greg Ward865de831999-09-21 18:31:14 +0000114 else:
Greg Ward4f08e4f2000-02-26 00:49:04 +0000115 # This is handy to guarantee that self.prefix is normalized --
116 # but it could be construed as rude to go normalizing a
117 # user-supplied path (they might like to see their "../" or
118 # symlinks in the installation feedback).
119 self.prefix = os.path.normpath (self.prefix)
120
121 if self.exec_prefix is None:
122 if self.prefix == sys_prefix:
123 self.exec_prefix = sys_exec_prefix
124 else:
125 self.exec_prefix = self.prefix
126 else:
127 # Same as above about handy versus rude to normalize user's
128 # exec_prefix.
129 self.exec_prefix = os.path.normpath (self.exec_prefix)
130
131 if self.distribution.ext_modules: # any extensions to install?
132 effective_prefix = self.exec_prefix
133 else:
134 effective_prefix = self.prefix
135
136 if os.name == 'posix':
137 if self.install_lib is None:
138 if self.prefix == sys_prefix:
139 self.install_lib = \
140 os.path.join (effective_prefix,
141 "lib",
142 "python" + sys.version[:3],
143 "site-packages")
144 else:
145 self.install_lib = \
146 os.path.join (effective_prefix,
147 "lib",
148 "python") # + sys.version[:3] ???
149 # end if self.install_lib ...
150
151 if self.install_platlib is None:
152 if self.exec_prefix == sys_exec_prefix:
153 self.install_platlib = \
154 os.path.join (effective_prefix,
155 "lib",
156 "python" + sys.version[:3],
157 "site-packages")
158 else:
159 self.install_platlib = \
160 os.path.join (effective_prefix,
161 "lib",
162 "python") # + sys.version[:3] ???
163 # end if self.install_platlib ...
164
165 else:
166 raise DistutilsPlatformError, \
167 "duh, I'm clueless (for now) about installing on %s" % os.name
168
169 # end if/else on os.name
170
171
172 # 'path_file' and 'extra_dirs' are how we handle distributions that
173 # want to be installed to their own directory, but aren't
Greg Ward865de831999-09-21 18:31:14 +0000174 # package-ized yet. 'extra_dirs' is just a directory under
Greg Ward4f08e4f2000-02-26 00:49:04 +0000175 # 'install_lib' or 'install_platlib' where top-level modules will
176 # actually be installed; 'path_file' is the basename of a .pth file
177 # to drop in 'install_lib' or 'install_platlib' (depending on the
178 # distribution). Very often they will be the same, which is why we
179 # allow them to be supplied as a string or 1-tuple as well as a
180 # 2-element comma-separated string or a 2-tuple.
181
182 # XXX this will drop a .pth file in install_{lib,platlib} even if
183 # they're not one of the site-packages directories: this is wrong!
184 # we need to suppress path_file in those cases, and warn if
185 # "install_lib/extra_dirs" is not in sys.path.
186
Greg Ward865de831999-09-21 18:31:14 +0000187 if self.install_path is None:
188 self.install_path = self.distribution.install_path
189
190 if self.install_path is not None:
191 if type (self.install_path) is StringType:
192 self.install_path = string.split (self.install_path, ',')
193
194 if len (self.install_path) == 1:
195 path_file = extra_dirs = self.install_path[0]
196 elif len (self.install_path) == 2:
197 (path_file, extra_dirs) = self.install_path
Greg Ward9a337071999-06-08 02:04:36 +0000198 else:
Greg Ward865de831999-09-21 18:31:14 +0000199 raise DistutilsOptionError, \
200 "'install_path' option must be a list, tuple, or " + \
201 "comma-separated string with 1 or 2 elements"
202
203 # install path has slashes in it -- might need to convert to
204 # local form
205 if string.find (extra_dirs, '/') and os.name != "posix":
206 extra_dirs = string.split (extra_dirs, '/')
207 extra_dirs = apply (os.path.join, extra_dirs)
208 else:
209 path_file = None
210 extra_dirs = ''
211
212 # XXX should we warn if path_file and not extra_dirs (in which case
213 # the path file would be harmless but pointless)
214 self.path_file = path_file
215 self.extra_dirs = extra_dirs
216
217
Greg Ward4f08e4f2000-02-26 00:49:04 +0000218 # Figure out the build directories, ie. where to install from
219 self.set_peer_option ('build', 'build_base', self.build_base)
220 self.set_undefined_options ('build',
221 ('build_base', 'build_base'),
222 ('build_lib', 'build_lib'),
223 ('build_platlib', 'build_platlib'))
Greg Ward13ae1c81999-03-22 14:55:25 +0000224
225 # Punt on doc directories for now -- after all, we're punting on
226 # documentation completely!
227
Greg Warde01149c2000-02-18 00:35:22 +0000228 # finalize_options ()
Greg Ward13ae1c81999-03-22 14:55:25 +0000229
230
Greg Ward13ae1c81999-03-22 14:55:25 +0000231 def run (self):
232
Greg Ward9a337071999-06-08 02:04:36 +0000233 # Obviously have to build before we can install
234 self.run_peer ('build')
235
Greg Ward13ae1c81999-03-22 14:55:25 +0000236 # Install modules in two steps: "platform-shared" files (ie. pure
Greg Ward4f08e4f2000-02-26 00:49:04 +0000237 # Python modules) and platform-specific files (compiled C
Greg Wardc9c011c1999-09-13 13:57:26 +0000238 # extensions). Note that 'install_py' is smart enough to install
239 # pure Python modules in the "platlib" directory if we built any
240 # extensions.
Greg Ward865de831999-09-21 18:31:14 +0000241 if self.distribution.packages or self.distribution.py_modules:
242 self.run_peer ('install_py')
243 if self.distribution.ext_modules:
244 self.run_peer ('install_ext')
245
246 if self.path_file:
247 self.create_path_file ()
Greg Ward13ae1c81999-03-22 14:55:25 +0000248
249 # run ()
250
Greg Ward865de831999-09-21 18:31:14 +0000251
252 def create_path_file (self):
253
254 if self.distribution.ext_modules:
255 base = self.platbase
256 else:
257 base = self.base
258
259 filename = os.path.join (base, self.path_file + ".pth")
260 self.execute (write_file,
261 (filename, [self.extra_dirs]),
262 "creating %s" % filename)
263
264
Greg Ward13ae1c81999-03-22 14:55:25 +0000265# class Install