blob: 6fb853618b562031acaf5678c7bbd81325e1e4de [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
10from distutils import sysconfig
11from distutils.core import Command
12
13
14class Install (Command):
15
16 options = [('prefix=', None, "installation prefix"),
17 ('execprefix=', None,
18 "prefix for platform-specific files"),
19
20 # Build directories: where to install from
21 ('build-base=', None,
22 "base build directory"),
23 ('build-lib=', None,
24 "build directory for non-platform-specific library files"),
25 ('build-platlib=', None,
26 "build directory for platform-specific library files"),
27
28 # Installation directories: where to put modules and packages
29 ('install-lib=', None,
30 "base Python library directory"),
31 ('install-platlib=', None,
32 "platform-specific Python library directory"),
33 ('install-site-lib=', None,
34 "directory for site-specific packages and modules"),
35 ('install-site-platlib=', None,
36 "platform-specific site directory"),
37 ('install-scheme=', None,
38 "install to 'system' or 'site' library directory?"),
39
40 # Where to install documentation (eventually!)
41 ('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"),
45
Greg Ward0f726951999-05-02 21:39:13 +000046 # Flags for 'build_py'
47 ('compile-py', None, "compile .py to .pyc"),
48 ('optimize-py', None, "compile .py to .pyo (optimized)"),
Greg Ward13ae1c81999-03-22 14:55:25 +000049 ]
50
Greg Ward9a337071999-06-08 02:04:36 +000051
Greg Ward13ae1c81999-03-22 14:55:25 +000052 def set_default_options (self):
53
54 self.build_base = None
55 self.build_lib = None
56 self.build_platlib = None
57
58 # Don't define 'prefix' or 'exec_prefix' so we can know when the
59 # command is run whether the user supplied values
60 self.prefix = None
61 self.exec_prefix = None
62
63 # These two, we can supply real values for! (because they're
64 # not directories, and don't have a confusing multitude of
65 # possible derivations)
66 #self.install_scheme = 'site'
67 self.doc_format = None
68
69 # The actual installation directories are determined only at
70 # run-time, so the user can supply just prefix (and exec_prefix?)
71 # as a base for everything else
72 self.install_lib = None
73 self.install_platlib = None
74 self.install_site_lib = None
75 self.install_site_platlib = None
76
77 self.install_man = None
78 self.install_html = None
79 self.install_info = None
80
Greg Ward0f726951999-05-02 21:39:13 +000081 self.compile_py = 1
82 self.optimize_py = 1
83
Greg Ward13ae1c81999-03-22 14:55:25 +000084
85 def set_final_options (self):
86
Greg Ward9a337071999-06-08 02:04:36 +000087 # XXX this method is where the default installation directories
88 # for modules and extension modules are determined. (Someday,
89 # the default installation directories for scripts,
90 # documentation, and whatever else the Distutils can build and
91 # install will also be determined here.) Thus, this is a pretty
92 # important place to fiddle with for anyone interested in
93 # installation schemes for the Python library. Issues that
94 # are not yet resolved to my satisfaction:
95 # * how much platform dependence should be here, and
96 # how much can be pushed off to sysconfig (or, better, the
97 # Makefiles parsed by sysconfig)?
98 # * how independent of Python version should this be -- ie.
99 # should we have special cases to handle Python 1.5 and
100 # older, and then do it "the right way" for 1.6? Or should
101 # we install a site.py along with Distutils under pre-1.6
102 # Python to take care of the current deficiencies in
103 # Python's library installation scheme?
104 #
105 # Currently, this method has hacks to distinguish POSIX from
106 # non-POSIX systems (for installation of site-local modules),
107 # and assumes the Python 1.5 installation tree with no site.py
108 # to fix things.
109
Greg Ward13ae1c81999-03-22 14:55:25 +0000110 # Figure out the build directories, ie. where to install from
111 self.set_peer_option ('build', 'basedir', self.build_base)
112 self.set_undefined_options ('build',
113 ('basedir', 'build_base'),
114 ('libdir', 'build_lib'),
115 ('platdir', 'build_platlib'))
116
117 # Figure out actual installation directories; the basic principle
118 # is: if the user supplied nothing, then use the directories that
119 # Python was built and installed with (ie. the compiled-in prefix
120 # and exec_prefix, and the actual installation directories gleaned
121 # by sysconfig). If the user supplied a prefix (and possibly
122 # exec_prefix), then we generate our own installation directories,
123 # following any pattern gleaned from sysconfig's findings. If no
124 # such pattern can be gleaned, then we'll just make do and try to
125 # ape the behaviour of Python's configure script.
126
127 if self.prefix is None: # user didn't override
128 self.prefix = sys.prefix
129 if self.exec_prefix is None:
130 self.exec_prefix = sys.exec_prefix
131
132 if self.install_lib is None:
133 self.install_lib = \
134 self.replace_sys_prefix ('LIBDEST', ('lib','python1.5'))
135 if self.install_platlib is None:
136 # XXX this should probably be DESTSHARED -- but why is there no
137 # equivalent to DESTSHARED for the "site-packages" dir"?
138 self.install_platlib = \
139 self.replace_sys_prefix ('BINLIBDEST', ('lib','python1.5'), 1)
140
Greg Ward9a337071999-06-08 02:04:36 +0000141
Greg Wardc9c011c1999-09-13 13:57:26 +0000142 # Here is where we decide where to install most library files: on
143 # POSIX systems, they go to 'site-packages' under the install_lib
144 # (determined above -- typically /usr/local/lib/python1.x). Note
145 # that on POSIX systems, platform-specific files belong in
146 # 'site-packages' under install_platlib. (The actual rule is that
147 # a module distribution that includes *any* platform-specific files
148 # -- ie. extension modules -- goes under install_platlib. This
149 # solves the "can't find extension module in a package" problem.)
150 # On non-POSIX systems, install_lib and install_platlib are the
151 # same (eg. "C:\Program Files\Python\Lib" on Windows), as are
152 # install_site_lib and install_site_platlib (eg.
153 # "C:\Program Files\Python" on Windows) -- everything will be dumped
154 # right into one of the install_site directories. (It doesn't
155 # really matter *which* one, of course, but I'll observe decorum
156 # and do it properly.)
Greg Ward9a337071999-06-08 02:04:36 +0000157
Greg Ward13ae1c81999-03-22 14:55:25 +0000158 if self.install_site_lib is None:
Greg Ward9a337071999-06-08 02:04:36 +0000159 if os.name == 'posix':
160 self.install_site_lib = \
161 os.path.join (self.install_lib, 'site-packages')
162 else:
Greg Wardc9c011c1999-09-13 13:57:26 +0000163 self.install_site_lib = self.prefix
Greg Ward9a337071999-06-08 02:04:36 +0000164
Greg Ward13ae1c81999-03-22 14:55:25 +0000165 if self.install_site_platlib is None:
Greg Ward9a337071999-06-08 02:04:36 +0000166 if os.name == 'posix':
Greg Ward9a337071999-06-08 02:04:36 +0000167 self.install_site_platlib = \
Greg Wardc9c011c1999-09-13 13:57:26 +0000168 os.path.join (self.install_platlib, 'site-packages')
Greg Ward9a337071999-06-08 02:04:36 +0000169 else:
170 self.install_site_platlib = self.exec_prefix
Greg Ward13ae1c81999-03-22 14:55:25 +0000171
172 #if self.install_scheme == 'site':
173 # install_lib = self.install_site_lib
174 # install_platlib = self.install_site_platlib
175 #elif self.install_scheme == 'system':
176 # install_lib = self.install_lib
177 # install_platlib = self.install_platlib
178 #else:
179 # # XXX new exception for this kind of misbehaviour?
180 # raise DistutilsArgError, \
181 # "invalid install scheme '%s'" % self.install_scheme
182
183
184 # Punt on doc directories for now -- after all, we're punting on
185 # documentation completely!
186
187 # set_final_options ()
188
189
190 def replace_sys_prefix (self, config_attr, fallback_postfix, use_exec=0):
191 """Attempts to glean a simple pattern from an installation
192 directory available as a 'sysconfig' attribute: if the
193 directory name starts with the "system prefix" (the one
194 hard-coded in the Makefile and compiled into Python),
195 then replace it with the current installation prefix and
196 return the "relocated" installation directory."""
197
198 if use_exec:
199 sys_prefix = sys.exec_prefix
200 my_prefix = self.exec_prefix
201 else:
202 sys_prefix = sys.prefix
203 my_prefix = self.prefix
204
205 val = getattr (sysconfig, config_attr)
206 if string.find (val, sys_prefix) == 0:
207 # If the sysconfig directory starts with the system prefix,
208 # then we can "relocate" it to the user-supplied prefix --
209 # assuming, of course, it is different from the system prefix.
210
211 if sys_prefix == my_prefix:
212 return val
213 else:
214 return my_prefix + val[len(sys_prefix):]
215
216 else:
217 # Otherwise, just tack the "fallback postfix" onto the
218 # user-specified prefix.
219
Greg Ward1016af91999-08-19 20:02:10 +0000220 return apply (os.path.join, (my_prefix,) + fallback_postfix)
Greg Ward13ae1c81999-03-22 14:55:25 +0000221
222 # replace_sys_prefix ()
223
224
225 def run (self):
226
227 self.set_final_options ()
228
Greg Ward9a337071999-06-08 02:04:36 +0000229 # Obviously have to build before we can install
230 self.run_peer ('build')
231
Greg Ward13ae1c81999-03-22 14:55:25 +0000232 # Install modules in two steps: "platform-shared" files (ie. pure
233 # python modules) and platform-specific files (compiled C
Greg Wardc9c011c1999-09-13 13:57:26 +0000234 # extensions). Note that 'install_py' is smart enough to install
235 # pure Python modules in the "platlib" directory if we built any
236 # extensions.
Greg Ward13ae1c81999-03-22 14:55:25 +0000237 self.run_peer ('install_py')
Greg Wardc9c011c1999-09-13 13:57:26 +0000238 self.run_peer ('install_ext')
Greg Ward13ae1c81999-03-22 14:55:25 +0000239
240 # run ()
241
242# class Install