Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 1 | """distutils.command.install |
| 2 | |
| 3 | Implements the Distutils 'install' command.""" |
| 4 | |
| 5 | # created 1999/03/13, Greg Ward |
| 6 | |
| 7 | __rcsid__ = "$Id$" |
| 8 | |
| 9 | import sys, os, string |
Greg Ward | 865de83 | 1999-09-21 18:31:14 +0000 | [diff] [blame] | 10 | from types import * |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 11 | from distutils.core import Command |
Greg Ward | 865de83 | 1999-09-21 18:31:14 +0000 | [diff] [blame] | 12 | from distutils.util import write_file |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 13 | |
| 14 | |
Greg Ward | 1993f9a | 2000-02-18 00:13:53 +0000 | [diff] [blame] | 15 | class install (Command): |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 16 | |
Greg Ward | 37bc815 | 2000-01-30 18:34:15 +0000 | [diff] [blame] | 17 | description = "install everything from build directory" |
| 18 | |
Greg Ward | bbeceea | 2000-02-18 00:25:39 +0000 | [diff] [blame] | 19 | user_options = [ |
| 20 | ('prefix=', None, "installation prefix"), |
| 21 | ('exec-prefix=', None, |
| 22 | "prefix for platform-specific files"), |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 23 | |
Greg Ward | bbeceea | 2000-02-18 00:25:39 +0000 | [diff] [blame] | 24 | # 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 Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 31 | |
Greg Ward | bbeceea | 2000-02-18 00:25:39 +0000 | [diff] [blame] | 32 | # 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 Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 45 | |
Greg Ward | bbeceea | 2000-02-18 00:25:39 +0000 | [diff] [blame] | 46 | # 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 Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 56 | |
Greg Ward | 9a33707 | 1999-06-08 02:04:36 +0000 | [diff] [blame] | 57 | |
Greg Ward | e01149c | 2000-02-18 00:35:22 +0000 | [diff] [blame^] | 58 | def initialize_options (self): |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 59 | |
| 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 Ward | 865de83 | 1999-09-21 18:31:14 +0000 | [diff] [blame] | 82 | self.install_path = None |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 83 | |
| 84 | self.install_man = None |
| 85 | self.install_html = None |
| 86 | self.install_info = None |
| 87 | |
Greg Ward | 0f72695 | 1999-05-02 21:39:13 +0000 | [diff] [blame] | 88 | self.compile_py = 1 |
| 89 | self.optimize_py = 1 |
| 90 | |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 91 | |
Greg Ward | e01149c | 2000-02-18 00:35:22 +0000 | [diff] [blame^] | 92 | def finalize_options (self): |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 93 | |
Greg Ward | 9a33707 | 1999-06-08 02:04:36 +0000 | [diff] [blame] | 94 | # 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 Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 117 | # Figure out the build directories, ie. where to install from |
Greg Ward | e6ac2fc | 1999-09-29 12:38:18 +0000 | [diff] [blame] | 118 | self.set_peer_option ('build', 'build_base', self.build_base) |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 119 | self.set_undefined_options ('build', |
Greg Ward | e6ac2fc | 1999-09-29 12:38:18 +0000 | [diff] [blame] | 120 | ('build_base', 'build_base'), |
| 121 | ('build_lib', 'build_lib'), |
| 122 | ('build_platlib', 'build_platlib')) |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 123 | |
| 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 Ward | c27d800 | 2000-01-17 16:25:59 +0000 | [diff] [blame] | 135 | self.prefix = os.path.normpath (sys.prefix) |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 136 | if self.exec_prefix is None: |
Greg Ward | c27d800 | 2000-01-17 16:25:59 +0000 | [diff] [blame] | 137 | self.exec_prefix = os.path.normpath (sys.exec_prefix) |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 138 | |
| 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 Ward | 9a33707 | 1999-06-08 02:04:36 +0000 | [diff] [blame] | 148 | |
Greg Ward | c9c011c | 1999-09-13 13:57:26 +0000 | [diff] [blame] | 149 | # 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 Ward | 9a33707 | 1999-06-08 02:04:36 +0000 | [diff] [blame] | 164 | |
Greg Ward | 865de83 | 1999-09-21 18:31:14 +0000 | [diff] [blame] | 165 | # '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 Ward | 9a33707 | 1999-06-08 02:04:36 +0000 | [diff] [blame] | 197 | else: |
Greg Ward | 865de83 | 1999-09-21 18:31:14 +0000 | [diff] [blame] | 198 | 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 Ward | 9a33707 | 1999-06-08 02:04:36 +0000 | [diff] [blame] | 220 | |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 221 | if self.install_site_platlib is None: |
Greg Ward | 865de83 | 1999-09-21 18:31:14 +0000 | [diff] [blame] | 222 | self.install_site_platlib = os.path.join (self.platbase, |
| 223 | extra_dirs) |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 224 | |
| 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 Ward | e01149c | 2000-02-18 00:35:22 +0000 | [diff] [blame^] | 240 | # finalize_options () |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 241 | |
| 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 Ward | 37bc815 | 2000-01-30 18:34:15 +0000 | [diff] [blame] | 251 | from distutils import sysconfig |
| 252 | |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 253 | if use_exec: |
Greg Ward | c27d800 | 2000-01-17 16:25:59 +0000 | [diff] [blame] | 254 | sys_prefix = os.path.normpath (sys.exec_prefix) |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 255 | my_prefix = self.exec_prefix |
| 256 | else: |
Greg Ward | c27d800 | 2000-01-17 16:25:59 +0000 | [diff] [blame] | 257 | sys_prefix = os.path.normpath (sys.prefix) |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 258 | 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 Ward | 1016af9 | 1999-08-19 20:02:10 +0000 | [diff] [blame] | 275 | return apply (os.path.join, (my_prefix,) + fallback_postfix) |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 276 | |
| 277 | # replace_sys_prefix () |
| 278 | |
| 279 | |
| 280 | def run (self): |
| 281 | |
Greg Ward | 9a33707 | 1999-06-08 02:04:36 +0000 | [diff] [blame] | 282 | # Obviously have to build before we can install |
| 283 | self.run_peer ('build') |
| 284 | |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 285 | # Install modules in two steps: "platform-shared" files (ie. pure |
| 286 | # python modules) and platform-specific files (compiled C |
Greg Ward | c9c011c | 1999-09-13 13:57:26 +0000 | [diff] [blame] | 287 | # 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 Ward | 865de83 | 1999-09-21 18:31:14 +0000 | [diff] [blame] | 290 | 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 Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 297 | |
| 298 | # run () |
| 299 | |
Greg Ward | 865de83 | 1999-09-21 18:31:14 +0000 | [diff] [blame] | 300 | |
| 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 Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 314 | # class Install |