Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 1 | # created 1999/03/13, Greg Ward |
| 2 | |
| 3 | __rcsid__ = "$Id$" |
| 4 | |
Greg Ward | 0f72695 | 1999-05-02 21:39:13 +0000 | [diff] [blame] | 5 | import sys, string |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 6 | from distutils.core import Command |
| 7 | from distutils.util import copy_tree |
| 8 | |
Greg Ward | 1993f9a | 2000-02-18 00:13:53 +0000 | [diff] [blame] | 9 | class install_py (Command): |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 10 | |
Greg Ward | 37bc815 | 2000-01-30 18:34:15 +0000 | [diff] [blame] | 11 | description = "install pure Python modules" |
| 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)"), |
| 16 | ('compile', 'c', "compile .py to .pyc"), |
| 17 | ('optimize', 'o', "compile .py to .pyo (optimized)"), |
| 18 | ] |
Greg Ward | 0f72695 | 1999-05-02 21:39:13 +0000 | [diff] [blame] | 19 | |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 20 | |
Greg Ward | e01149c | 2000-02-18 00:35:22 +0000 | [diff] [blame^] | 21 | def initialize_options (self): |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 22 | # let the 'install' command dictate our installation directory |
Greg Ward | e6ac2fc | 1999-09-29 12:38:18 +0000 | [diff] [blame] | 23 | self.install_dir = None |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 24 | self.build_dir = None |
Greg Ward | 0f72695 | 1999-05-02 21:39:13 +0000 | [diff] [blame] | 25 | self.compile = 1 |
| 26 | self.optimize = 1 |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 27 | |
Greg Ward | e01149c | 2000-02-18 00:35:22 +0000 | [diff] [blame^] | 28 | def finalize_options (self): |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 29 | |
Greg Ward | fa4eb18 | 1999-09-13 13:58:34 +0000 | [diff] [blame] | 30 | # Find out from the 'build_ext' command if we were asked to build |
| 31 | # any extensions. If so, that means even pure-Python modules in |
| 32 | # this distribution have to be installed to the "platlib" |
| 33 | # directory. |
| 34 | extensions = self.get_peer_option ('build_ext', 'extensions') |
| 35 | if extensions: |
| 36 | dir_option = 'install_site_platlib' |
| 37 | else: |
| 38 | dir_option = 'install_site_lib' |
| 39 | |
| 40 | # Get all the information we need to install pure Python modules |
| 41 | # from the umbrella 'install' command -- build (source) directory, |
| 42 | # install (target) directory, and whether to compile .py files. |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 43 | self.set_undefined_options ('install', |
| 44 | ('build_lib', 'build_dir'), |
Greg Ward | e6ac2fc | 1999-09-29 12:38:18 +0000 | [diff] [blame] | 45 | (dir_option, 'install_dir'), |
Greg Ward | 0f72695 | 1999-05-02 21:39:13 +0000 | [diff] [blame] | 46 | ('compile_py', 'compile'), |
| 47 | ('optimize_py', 'optimize')) |
| 48 | |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 49 | |
| 50 | def run (self): |
| 51 | |
Greg Ward | 7478a48 | 2000-01-30 15:07:56 +0000 | [diff] [blame] | 52 | # Make sure we have "built" all pure Python modules first |
| 53 | self.run_peer ('build_py') |
| 54 | |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 55 | # Dump entire contents of the build directory to the installation |
| 56 | # directory (that's the beauty of having a build directory!) |
Greg Ward | e6ac2fc | 1999-09-29 12:38:18 +0000 | [diff] [blame] | 57 | outfiles = self.copy_tree (self.build_dir, self.install_dir) |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 58 | |
Greg Ward | 0f72695 | 1999-05-02 21:39:13 +0000 | [diff] [blame] | 59 | # (Optionally) compile .py to .pyc |
| 60 | # XXX hey! we can't control whether we optimize or not; that's up |
| 61 | # to the invocation of the current Python interpreter (at least |
| 62 | # according to the py_compile docs). That sucks. |
| 63 | |
| 64 | if self.compile: |
| 65 | from py_compile import compile |
| 66 | |
| 67 | for f in outfiles: |
| 68 | # XXX can't assume this filename mapping! |
Greg Ward | 0f72695 | 1999-05-02 21:39:13 +0000 | [diff] [blame] | 69 | |
Greg Ward | 440e2f5 | 1999-08-29 18:19:37 +0000 | [diff] [blame] | 70 | # only compile the file if it is actually a .py file |
| 71 | if f[-3:] == '.py': |
| 72 | out_fn = string.replace (f, '.py', '.pyc') |
| 73 | |
| 74 | self.make_file (f, out_fn, compile, (f,), |
Greg Ward | ef9ad6d | 1999-10-03 21:03:26 +0000 | [diff] [blame] | 75 | "byte-compiling %s" % f, |
| 76 | "byte-compilation of %s skipped" % f) |
Greg Ward | 440e2f5 | 1999-08-29 18:19:37 +0000 | [diff] [blame] | 77 | |
Greg Ward | 0f72695 | 1999-05-02 21:39:13 +0000 | [diff] [blame] | 78 | # XXX ignore self.optimize for now, since we don't really know if |
| 79 | # we're compiling optimally or not, and couldn't pick what to do |
| 80 | # even if we did know. ;-( |
| 81 | |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 82 | # run () |
| 83 | |
| 84 | # class InstallPy |