Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 1 | # created 1999/03/13, Greg Ward |
| 2 | |
Greg Ward | 3ce77fd | 2000-03-02 01:49:45 +0000 | [diff] [blame] | 3 | __revision__ = "$Id$" |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 4 | |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 5 | import sys, os, string |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 6 | from distutils.core import Command |
Greg Ward | ab3a0f3 | 2000-08-05 01:31:54 +0000 | [diff] [blame] | 7 | from distutils.dir_util import copy_tree |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 8 | |
Greg Ward | 297dd9f | 2000-03-23 04:37:11 +0000 | [diff] [blame] | 9 | class install_lib (Command): |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 10 | |
Greg Ward | d7ad503 | 2000-05-23 01:55:16 +0000 | [diff] [blame] | 11 | description = "install all Python modules (extensions and pure Python)" |
Greg Ward | 37bc815 | 2000-01-30 18:34:15 +0000 | [diff] [blame] | 12 | |
Greg Ward | bbeceea | 2000-02-18 00:25:39 +0000 | [diff] [blame] | 13 | user_options = [ |
| 14 | ('install-dir=', 'd', "directory to install to"), |
| 15 | ('build-dir=','b', "build directory (where to install from)"), |
Greg Ward | 3a0310a | 2000-09-13 01:02:25 +0000 | [diff] [blame] | 16 | ('force', 'f', "force installation (overwrite existing files)"), |
Greg Ward | bbeceea | 2000-02-18 00:25:39 +0000 | [diff] [blame] | 17 | ('compile', 'c', "compile .py to .pyc"), |
| 18 | ('optimize', 'o', "compile .py to .pyo (optimized)"), |
Gregory P. Smith | 74ead8f | 2000-05-12 01:46:47 +0000 | [diff] [blame] | 19 | ('skip-build', None, "skip the build steps"), |
Greg Ward | bbeceea | 2000-02-18 00:25:39 +0000 | [diff] [blame] | 20 | ] |
Greg Ward | 0f72695 | 1999-05-02 21:39:13 +0000 | [diff] [blame] | 21 | |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 22 | |
Greg Ward | e01149c | 2000-02-18 00:35:22 +0000 | [diff] [blame] | 23 | def initialize_options (self): |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 24 | # let the 'install' command dictate our installation directory |
Greg Ward | e6ac2fc | 1999-09-29 12:38:18 +0000 | [diff] [blame] | 25 | self.install_dir = None |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 26 | self.build_dir = None |
Greg Ward | 3a0310a | 2000-09-13 01:02:25 +0000 | [diff] [blame] | 27 | self.force = 0 |
Greg Ward | 0f72695 | 1999-05-02 21:39:13 +0000 | [diff] [blame] | 28 | self.compile = 1 |
| 29 | self.optimize = 1 |
Gregory P. Smith | 74ead8f | 2000-05-12 01:46:47 +0000 | [diff] [blame] | 30 | self.skip_build = None |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 31 | |
Greg Ward | e01149c | 2000-02-18 00:35:22 +0000 | [diff] [blame] | 32 | def finalize_options (self): |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 33 | |
Greg Ward | fa4eb18 | 1999-09-13 13:58:34 +0000 | [diff] [blame] | 34 | # Get all the information we need to install pure Python modules |
| 35 | # from the umbrella 'install' command -- build (source) directory, |
| 36 | # install (target) directory, and whether to compile .py files. |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 37 | self.set_undefined_options('install', |
| 38 | ('build_lib', 'build_dir'), |
| 39 | ('install_lib', 'install_dir'), |
| 40 | ('force', 'force'), |
| 41 | ('compile_py', 'compile'), |
| 42 | ('optimize_py', 'optimize'), |
| 43 | ('skip_build', 'skip_build'), |
| 44 | ) |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 45 | |
| 46 | def run (self): |
| 47 | |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 48 | # Make sure we have built everything we need first |
Greg Ward | f84fb66 | 2000-09-23 01:20:19 +0000 | [diff] [blame^] | 49 | self.build() |
| 50 | |
| 51 | # Install everything: simply dump the entire contents of the build |
| 52 | # directory to the installation directory (that's the beauty of |
| 53 | # having a build directory!) |
| 54 | outfiles = self.install() |
| 55 | |
| 56 | # (Optionally) compile .py to .pyc |
| 57 | self.bytecompile(outfiles) |
| 58 | |
| 59 | # run () |
| 60 | |
| 61 | |
| 62 | # -- Top-level worker functions ------------------------------------ |
| 63 | # (called from 'run()') |
| 64 | |
| 65 | def build (self): |
Gregory P. Smith | 74ead8f | 2000-05-12 01:46:47 +0000 | [diff] [blame] | 66 | if not self.skip_build: |
| 67 | if self.distribution.has_pure_modules(): |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 68 | self.run_command('build_py') |
Gregory P. Smith | 74ead8f | 2000-05-12 01:46:47 +0000 | [diff] [blame] | 69 | if self.distribution.has_ext_modules(): |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 70 | self.run_command('build_ext') |
Greg Ward | f84fb66 | 2000-09-23 01:20:19 +0000 | [diff] [blame^] | 71 | |
| 72 | def install (self): |
Greg Ward | f355d47 | 2000-05-20 15:08:57 +0000 | [diff] [blame] | 73 | if os.path.isdir(self.build_dir): |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 74 | outfiles = self.copy_tree(self.build_dir, self.install_dir) |
Greg Ward | f355d47 | 2000-05-20 15:08:57 +0000 | [diff] [blame] | 75 | else: |
| 76 | self.warn("'%s' does not exist -- no Python modules to install" % |
| 77 | self.build_dir) |
| 78 | return |
Greg Ward | f84fb66 | 2000-09-23 01:20:19 +0000 | [diff] [blame^] | 79 | return outfiles |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 80 | |
Greg Ward | f84fb66 | 2000-09-23 01:20:19 +0000 | [diff] [blame^] | 81 | def bytecompile (self, files): |
Greg Ward | 0f72695 | 1999-05-02 21:39:13 +0000 | [diff] [blame] | 82 | # XXX hey! we can't control whether we optimize or not; that's up |
| 83 | # to the invocation of the current Python interpreter (at least |
| 84 | # according to the py_compile docs). That sucks. |
Greg Ward | 0f72695 | 1999-05-02 21:39:13 +0000 | [diff] [blame] | 85 | if self.compile: |
| 86 | from py_compile import compile |
| 87 | |
Greg Ward | f84fb66 | 2000-09-23 01:20:19 +0000 | [diff] [blame^] | 88 | for f in files: |
Greg Ward | 440e2f5 | 1999-08-29 18:19:37 +0000 | [diff] [blame] | 89 | # only compile the file if it is actually a .py file |
| 90 | if f[-3:] == '.py': |
Greg Ward | ed8a0e0 | 2000-03-29 03:29:34 +0000 | [diff] [blame] | 91 | out_fn = f + (__debug__ and "c" or "o") |
| 92 | compile_msg = "byte-compiling %s to %s" % \ |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 93 | (f, os.path.basename(out_fn)) |
Greg Ward | 90c74cc | 2000-08-02 01:34:18 +0000 | [diff] [blame] | 94 | skip_msg = "skipping byte-compilation of %s" % f |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 95 | self.make_file(f, out_fn, compile, (f,), |
| 96 | compile_msg, skip_msg) |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 97 | |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 98 | |
Greg Ward | f84fb66 | 2000-09-23 01:20:19 +0000 | [diff] [blame^] | 99 | # -- Utility methods ----------------------------------------------- |
| 100 | |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 101 | def _mutate_outputs (self, has_any, build_cmd, cmd_option, output_dir): |
| 102 | |
| 103 | if not has_any: |
| 104 | return [] |
| 105 | |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 106 | build_cmd = self.get_finalized_command(build_cmd) |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 107 | build_files = build_cmd.get_outputs() |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 108 | build_dir = getattr(build_cmd, cmd_option) |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 109 | |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 110 | prefix_len = len(build_dir) + len(os.sep) |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 111 | outputs = [] |
| 112 | for file in build_files: |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 113 | outputs.append(os.path.join(output_dir, file[prefix_len:])) |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 114 | |
| 115 | return outputs |
| 116 | |
| 117 | # _mutate_outputs () |
Gregory P. Smith | 11fb783 | 2000-05-13 02:11:10 +0000 | [diff] [blame] | 118 | |
| 119 | def _bytecode_filenames (self, py_filenames): |
| 120 | bytecode_files = [] |
| 121 | for py_file in py_filenames: |
| 122 | bytecode = py_file + (__debug__ and "c" or "o") |
| 123 | bytecode_files.append(bytecode) |
| 124 | |
| 125 | return bytecode_files |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 126 | |
Greg Ward | f84fb66 | 2000-09-23 01:20:19 +0000 | [diff] [blame^] | 127 | |
| 128 | # -- External interface -------------------------------------------- |
| 129 | # (called by outsiders) |
| 130 | |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 131 | def get_outputs (self): |
| 132 | """Return the list of files that would be installed if this command |
| 133 | were actually run. Not affected by the "dry-run" flag or whether |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 134 | modules have actually been built yet. |
| 135 | """ |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 136 | pure_outputs = \ |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 137 | self._mutate_outputs(self.distribution.has_pure_modules(), |
| 138 | 'build_py', 'build_lib', |
| 139 | self.install_dir) |
Gregory P. Smith | 11fb783 | 2000-05-13 02:11:10 +0000 | [diff] [blame] | 140 | if self.compile: |
| 141 | bytecode_outputs = self._bytecode_filenames(pure_outputs) |
| 142 | else: |
| 143 | bytecode_outputs = [] |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 144 | |
| 145 | ext_outputs = \ |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 146 | self._mutate_outputs(self.distribution.has_ext_modules(), |
| 147 | 'build_ext', 'build_lib', |
| 148 | self.install_dir) |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 149 | |
Gregory P. Smith | 11fb783 | 2000-05-13 02:11:10 +0000 | [diff] [blame] | 150 | return pure_outputs + bytecode_outputs + ext_outputs |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 151 | |
| 152 | # get_outputs () |
| 153 | |
Greg Ward | 1aab6e9 | 2000-03-31 02:53:07 +0000 | [diff] [blame] | 154 | def get_inputs (self): |
| 155 | """Get the list of files that are input to this command, ie. the |
| 156 | files that get installed as they are named in the build tree. |
| 157 | The files in this list correspond one-to-one to the output |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 158 | filenames returned by 'get_outputs()'. |
| 159 | """ |
Greg Ward | 1aab6e9 | 2000-03-31 02:53:07 +0000 | [diff] [blame] | 160 | inputs = [] |
| 161 | |
| 162 | if self.distribution.has_pure_modules(): |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 163 | build_py = self.get_finalized_command('build_py') |
| 164 | inputs.extend(build_py.get_outputs()) |
Greg Ward | 1aab6e9 | 2000-03-31 02:53:07 +0000 | [diff] [blame] | 165 | |
| 166 | if self.distribution.has_ext_modules(): |
Greg Ward | 7b87c0e | 2000-09-23 01:10:10 +0000 | [diff] [blame] | 167 | build_ext = self.get_finalized_command('build_ext') |
| 168 | inputs.extend(build_ext.get_outputs()) |
Greg Ward | 1aab6e9 | 2000-03-31 02:53:07 +0000 | [diff] [blame] | 169 | |
| 170 | return inputs |
Greg Ward | 1aab6e9 | 2000-03-31 02:53:07 +0000 | [diff] [blame] | 171 | |
Greg Ward | e5dfba5 | 2000-03-29 02:17:42 +0000 | [diff] [blame] | 172 | # class install_lib |