Benjamin Peterson | 1010bf3 | 2009-01-30 04:00:29 +0000 | [diff] [blame] | 1 | """distutils.command.install_lib |
| 2 | |
| 3 | Implements the Distutils 'install_lib' command |
| 4 | (install all Python modules).""" |
| 5 | |
Tarek Ziadé | c27aca7 | 2009-05-10 11:45:41 +0000 | [diff] [blame] | 6 | import os |
Brett Cannon | 61c3556 | 2013-06-15 12:59:53 -0400 | [diff] [blame] | 7 | import importlib.util |
Tarek Ziadé | 04fe7c0 | 2009-10-25 23:08:47 +0000 | [diff] [blame] | 8 | import sys |
| 9 | |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 10 | from distutils.core import Command |
Greg Ward | cb9c9ae | 2000-10-02 02:15:08 +0000 | [diff] [blame] | 11 | from distutils.errors import DistutilsOptionError |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 12 | |
Fred Drake | 25d8892 | 2004-07-28 14:55:10 +0000 | [diff] [blame] | 13 | |
| 14 | # Extension for Python source files. |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 15 | PYTHON_SOURCE_EXTENSION = ".py" |
Fred Drake | 25d8892 | 2004-07-28 14:55:10 +0000 | [diff] [blame] | 16 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 17 | class install_lib(Command): |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 18 | |
Greg Ward | d7ad503 | 2000-05-23 01:55:16 +0000 | [diff] [blame] | 19 | description = "install all Python modules (extensions and pure Python)" |
Greg Ward | 37bc815 | 2000-01-30 18:34:15 +0000 | [diff] [blame] | 20 | |
Greg Ward | cb9c9ae | 2000-10-02 02:15:08 +0000 | [diff] [blame] | 21 | # The byte-compilation options are a tad confusing. Here are the |
| 22 | # possible scenarios: |
| 23 | # 1) no compilation at all (--no-compile --no-optimize) |
| 24 | # 2) compile .pyc only (--compile --no-optimize; default) |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 25 | # 3) compile .pyc and "opt-1" .pyc (--compile --optimize) |
| 26 | # 4) compile "opt-1" .pyc only (--no-compile --optimize) |
| 27 | # 5) compile .pyc and "opt-2" .pyc (--compile --optimize-more) |
| 28 | # 6) compile "opt-2" .pyc only (--no-compile --optimize-more) |
Fred Drake | 21d4535 | 2001-12-06 21:01:19 +0000 | [diff] [blame] | 29 | # |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 30 | # The UI for this is two options, 'compile' and 'optimize'. |
Greg Ward | cb9c9ae | 2000-10-02 02:15:08 +0000 | [diff] [blame] | 31 | # 'compile' is strictly boolean, and only decides whether to |
| 32 | # generate .pyc files. 'optimize' is three-way (0, 1, or 2), and |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 33 | # decides both whether to generate .pyc files and what level of |
Greg Ward | cb9c9ae | 2000-10-02 02:15:08 +0000 | [diff] [blame] | 34 | # optimization to use. |
| 35 | |
Greg Ward | bbeceea | 2000-02-18 00:25:39 +0000 | [diff] [blame] | 36 | user_options = [ |
| 37 | ('install-dir=', 'd', "directory to install to"), |
| 38 | ('build-dir=','b', "build directory (where to install from)"), |
Greg Ward | 3a0310a | 2000-09-13 01:02:25 +0000 | [diff] [blame] | 39 | ('force', 'f', "force installation (overwrite existing files)"), |
Greg Ward | cb9c9ae | 2000-10-02 02:15:08 +0000 | [diff] [blame] | 40 | ('compile', 'c', "compile .py to .pyc [default]"), |
| 41 | ('no-compile', None, "don't compile .py files"), |
| 42 | ('optimize=', 'O', |
| 43 | "also compile with optimization: -O1 for \"python -O\", " |
| 44 | "-O2 for \"python -OO\", and -O0 to disable [default: -O0]"), |
Gregory P. Smith | 74ead8f | 2000-05-12 01:46:47 +0000 | [diff] [blame] | 45 | ('skip-build', None, "skip the build steps"), |
Greg Ward | bbeceea | 2000-02-18 00:25:39 +0000 | [diff] [blame] | 46 | ] |
Fred Drake | 21d4535 | 2001-12-06 21:01:19 +0000 | [diff] [blame] | 47 | |
Greg Ward | cb9c9ae | 2000-10-02 02:15:08 +0000 | [diff] [blame] | 48 | boolean_options = ['force', 'compile', 'skip-build'] |
| 49 | negative_opt = {'no-compile' : 'compile'} |
Greg Ward | 99b032e | 2000-09-25 01:41:15 +0000 | [diff] [blame] | 50 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 51 | def initialize_options(self): |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 52 | # let the 'install' command dictate our installation directory |
Greg Ward | e6ac2fc | 1999-09-29 12:38:18 +0000 | [diff] [blame] | 53 | self.install_dir = None |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 54 | self.build_dir = None |
Greg Ward | 3a0310a | 2000-09-13 01:02:25 +0000 | [diff] [blame] | 55 | self.force = 0 |
Greg Ward | cb9c9ae | 2000-10-02 02:15:08 +0000 | [diff] [blame] | 56 | self.compile = None |
| 57 | self.optimize = None |
Gregory P. Smith | 74ead8f | 2000-05-12 01:46:47 +0000 | [diff] [blame] | 58 | self.skip_build = None |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 59 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 60 | def finalize_options(self): |
Greg Ward | fa4eb18 | 1999-09-13 13:58:34 +0000 | [diff] [blame] | 61 | # Get all the information we need to install pure Python modules |
| 62 | # from the umbrella 'install' command -- build (source) directory, |
| 63 | # install (target) directory, and whether to compile .py files. |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 64 | self.set_undefined_options('install', |
| 65 | ('build_lib', 'build_dir'), |
| 66 | ('install_lib', 'install_dir'), |
| 67 | ('force', 'force'), |
Greg Ward | cb9c9ae | 2000-10-02 02:15:08 +0000 | [diff] [blame] | 68 | ('compile', 'compile'), |
| 69 | ('optimize', 'optimize'), |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 70 | ('skip_build', 'skip_build'), |
| 71 | ) |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 72 | |
Greg Ward | cb9c9ae | 2000-10-02 02:15:08 +0000 | [diff] [blame] | 73 | if self.compile is None: |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 74 | self.compile = True |
Greg Ward | cb9c9ae | 2000-10-02 02:15:08 +0000 | [diff] [blame] | 75 | if self.optimize is None: |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 76 | self.optimize = False |
Greg Ward | cb9c9ae | 2000-10-02 02:15:08 +0000 | [diff] [blame] | 77 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 78 | if not isinstance(self.optimize, int): |
Greg Ward | cb9c9ae | 2000-10-02 02:15:08 +0000 | [diff] [blame] | 79 | try: |
| 80 | self.optimize = int(self.optimize) |
Tarek Ziadé | da31a3f | 2009-05-12 07:06:42 +0000 | [diff] [blame] | 81 | if self.optimize not in (0, 1, 2): |
| 82 | raise AssertionError |
Greg Ward | cb9c9ae | 2000-10-02 02:15:08 +0000 | [diff] [blame] | 83 | except (ValueError, AssertionError): |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 84 | raise DistutilsOptionError("optimize must be 0, 1, or 2") |
Greg Ward | cb9c9ae | 2000-10-02 02:15:08 +0000 | [diff] [blame] | 85 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 86 | def run(self): |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 87 | # Make sure we have built everything we need first |
Greg Ward | f84fb66 | 2000-09-23 01:20:19 +0000 | [diff] [blame] | 88 | self.build() |
Fred Drake | 21d4535 | 2001-12-06 21:01:19 +0000 | [diff] [blame] | 89 | |
Greg Ward | f84fb66 | 2000-09-23 01:20:19 +0000 | [diff] [blame] | 90 | # Install everything: simply dump the entire contents of the build |
| 91 | # directory to the installation directory (that's the beauty of |
| 92 | # having a build directory!) |
| 93 | outfiles = self.install() |
| 94 | |
| 95 | # (Optionally) compile .py to .pyc |
Greg Ward | c1acc69 | 2000-10-01 23:50:13 +0000 | [diff] [blame] | 96 | if outfiles is not None and self.distribution.has_pure_modules(): |
Greg Ward | cb9c9ae | 2000-10-02 02:15:08 +0000 | [diff] [blame] | 97 | self.byte_compile(outfiles) |
Greg Ward | f84fb66 | 2000-09-23 01:20:19 +0000 | [diff] [blame] | 98 | |
Greg Ward | f84fb66 | 2000-09-23 01:20:19 +0000 | [diff] [blame] | 99 | # -- Top-level worker functions ------------------------------------ |
| 100 | # (called from 'run()') |
| 101 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 102 | def build(self): |
Gregory P. Smith | 74ead8f | 2000-05-12 01:46:47 +0000 | [diff] [blame] | 103 | if not self.skip_build: |
| 104 | if self.distribution.has_pure_modules(): |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 105 | self.run_command('build_py') |
Gregory P. Smith | 74ead8f | 2000-05-12 01:46:47 +0000 | [diff] [blame] | 106 | if self.distribution.has_ext_modules(): |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 107 | self.run_command('build_ext') |
Fred Drake | 21d4535 | 2001-12-06 21:01:19 +0000 | [diff] [blame] | 108 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 109 | def install(self): |
Greg Ward | f355d47 | 2000-05-20 15:08:57 +0000 | [diff] [blame] | 110 | if os.path.isdir(self.build_dir): |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 111 | outfiles = self.copy_tree(self.build_dir, self.install_dir) |
Greg Ward | f355d47 | 2000-05-20 15:08:57 +0000 | [diff] [blame] | 112 | else: |
| 113 | self.warn("'%s' does not exist -- no Python modules to install" % |
| 114 | self.build_dir) |
| 115 | return |
Greg Ward | f84fb66 | 2000-09-23 01:20:19 +0000 | [diff] [blame] | 116 | return outfiles |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 117 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 118 | def byte_compile(self, files): |
Tarek Ziadé | 04fe7c0 | 2009-10-25 23:08:47 +0000 | [diff] [blame] | 119 | if sys.dont_write_bytecode: |
| 120 | self.warn('byte-compiling is disabled, skipping.') |
| 121 | return |
| 122 | |
Greg Ward | cb9c9ae | 2000-10-02 02:15:08 +0000 | [diff] [blame] | 123 | from distutils.util import byte_compile |
| 124 | |
| 125 | # Get the "--root" directory supplied to the "install" command, |
| 126 | # and use it as a prefix to strip off the purported filename |
| 127 | # encoded in bytecode files. This is far from complete, but it |
| 128 | # should at least generate usable bytecode in RPM distributions. |
| 129 | install_root = self.get_finalized_command('install').root |
| 130 | |
| 131 | if self.compile: |
| 132 | byte_compile(files, optimize=0, |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 133 | force=self.force, prefix=install_root, |
| 134 | dry_run=self.dry_run) |
Greg Ward | cb9c9ae | 2000-10-02 02:15:08 +0000 | [diff] [blame] | 135 | if self.optimize > 0: |
| 136 | byte_compile(files, optimize=self.optimize, |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 137 | force=self.force, prefix=install_root, |
Greg Ward | cb9c9ae | 2000-10-02 02:15:08 +0000 | [diff] [blame] | 138 | verbose=self.verbose, dry_run=self.dry_run) |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 139 | |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 140 | |
Greg Ward | f84fb66 | 2000-09-23 01:20:19 +0000 | [diff] [blame] | 141 | # -- Utility methods ----------------------------------------------- |
| 142 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 143 | def _mutate_outputs(self, has_any, build_cmd, cmd_option, output_dir): |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 144 | if not has_any: |
| 145 | return [] |
| 146 | |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 147 | build_cmd = self.get_finalized_command(build_cmd) |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 148 | build_files = build_cmd.get_outputs() |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 149 | build_dir = getattr(build_cmd, cmd_option) |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 150 | |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 151 | prefix_len = len(build_dir) + len(os.sep) |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 152 | outputs = [] |
| 153 | for file in build_files: |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 154 | outputs.append(os.path.join(output_dir, file[prefix_len:])) |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 155 | |
| 156 | return outputs |
| 157 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 158 | def _bytecode_filenames(self, py_filenames): |
Gregory P. Smith | 11fb783 | 2000-05-13 02:11:10 +0000 | [diff] [blame] | 159 | bytecode_files = [] |
| 160 | for py_file in py_filenames: |
Fred Drake | 25d8892 | 2004-07-28 14:55:10 +0000 | [diff] [blame] | 161 | # Since build_py handles package data installation, the |
| 162 | # list of outputs can contain more than just .py files. |
| 163 | # Make sure we only report bytecode for the .py files. |
| 164 | ext = os.path.splitext(os.path.normcase(py_file))[1] |
| 165 | if ext != PYTHON_SOURCE_EXTENSION: |
| 166 | continue |
Greg Ward | cb9c9ae | 2000-10-02 02:15:08 +0000 | [diff] [blame] | 167 | if self.compile: |
Brett Cannon | 61c3556 | 2013-06-15 12:59:53 -0400 | [diff] [blame] | 168 | bytecode_files.append(importlib.util.cache_from_source( |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 169 | py_file, optimization='')) |
Greg Ward | 0295181 | 2000-10-02 02:25:51 +0000 | [diff] [blame] | 170 | if self.optimize > 0: |
Brett Cannon | 61c3556 | 2013-06-15 12:59:53 -0400 | [diff] [blame] | 171 | bytecode_files.append(importlib.util.cache_from_source( |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 172 | py_file, optimization=self.optimize)) |
Gregory P. Smith | 11fb783 | 2000-05-13 02:11:10 +0000 | [diff] [blame] | 173 | |
| 174 | return bytecode_files |
Fred Drake | 21d4535 | 2001-12-06 21:01:19 +0000 | [diff] [blame] | 175 | |
Greg Ward | f84fb66 | 2000-09-23 01:20:19 +0000 | [diff] [blame] | 176 | |
| 177 | # -- External interface -------------------------------------------- |
| 178 | # (called by outsiders) |
| 179 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 180 | def get_outputs(self): |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 181 | """Return the list of files that would be installed if this command |
| 182 | were actually run. Not affected by the "dry-run" flag or whether |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 183 | modules have actually been built yet. |
| 184 | """ |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 185 | pure_outputs = \ |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 186 | self._mutate_outputs(self.distribution.has_pure_modules(), |
| 187 | 'build_py', 'build_lib', |
| 188 | self.install_dir) |
Gregory P. Smith | 11fb783 | 2000-05-13 02:11:10 +0000 | [diff] [blame] | 189 | if self.compile: |
| 190 | bytecode_outputs = self._bytecode_filenames(pure_outputs) |
| 191 | else: |
| 192 | bytecode_outputs = [] |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 193 | |
| 194 | ext_outputs = \ |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 195 | self._mutate_outputs(self.distribution.has_ext_modules(), |
| 196 | 'build_ext', 'build_lib', |
| 197 | self.install_dir) |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 198 | |
Gregory P. Smith | 11fb783 | 2000-05-13 02:11:10 +0000 | [diff] [blame] | 199 | return pure_outputs + bytecode_outputs + ext_outputs |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 200 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 201 | def get_inputs(self): |
Greg Ward | 1aab6e9 | 2000-03-31 02:53:07 +0000 | [diff] [blame] | 202 | """Get the list of files that are input to this command, ie. the |
| 203 | files that get installed as they are named in the build tree. |
| 204 | The files in this list correspond one-to-one to the output |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 205 | filenames returned by 'get_outputs()'. |
| 206 | """ |
Greg Ward | 1aab6e9 | 2000-03-31 02:53:07 +0000 | [diff] [blame] | 207 | inputs = [] |
Fred Drake | 21d4535 | 2001-12-06 21:01:19 +0000 | [diff] [blame] | 208 | |
Greg Ward | 1aab6e9 | 2000-03-31 02:53:07 +0000 | [diff] [blame] | 209 | if self.distribution.has_pure_modules(): |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 210 | build_py = self.get_finalized_command('build_py') |
| 211 | inputs.extend(build_py.get_outputs()) |
Greg Ward | 1aab6e9 | 2000-03-31 02:53:07 +0000 | [diff] [blame] | 212 | |
| 213 | if self.distribution.has_ext_modules(): |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 214 | build_ext = self.get_finalized_command('build_ext') |
| 215 | inputs.extend(build_ext.get_outputs()) |
Greg Ward | 1aab6e9 | 2000-03-31 02:53:07 +0000 | [diff] [blame] | 216 | |
| 217 | return inputs |