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 | |
| 9 | class InstallPy (Command): |
| 10 | |
| 11 | options = [('dir=', 'd', "directory to install to"), |
Greg Ward | 440e2f5 | 1999-08-29 18:19:37 +0000 | [diff] [blame^] | 12 | ('build-dir=','b', "build directory (where to install from)"), |
Greg Ward | 0f72695 | 1999-05-02 21:39:13 +0000 | [diff] [blame] | 13 | ('compile', 'c', "compile .py to .pyc"), |
| 14 | ('optimize', 'o', "compile .py to .pyo (optimized)"), |
| 15 | ] |
| 16 | |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 17 | |
| 18 | def set_default_options (self): |
| 19 | # let the 'install' command dictate our installation directory |
| 20 | self.dir = None |
| 21 | self.build_dir = None |
Greg Ward | 0f72695 | 1999-05-02 21:39:13 +0000 | [diff] [blame] | 22 | self.compile = 1 |
| 23 | self.optimize = 1 |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 24 | |
| 25 | def set_final_options (self): |
| 26 | # If we don't have a 'dir' value, we'll have to ask the 'install' |
| 27 | # command for one. (This usually means the user ran 'install_py' |
| 28 | # directly, rather than going through 'install' -- so in reality, |
| 29 | # 'find_command_obj()' will create an 'install' command object, |
| 30 | # which we then query. |
| 31 | |
| 32 | self.set_undefined_options ('install', |
| 33 | ('build_lib', 'build_dir'), |
Greg Ward | 0f72695 | 1999-05-02 21:39:13 +0000 | [diff] [blame] | 34 | ('install_site_lib', 'dir'), |
| 35 | ('compile_py', 'compile'), |
| 36 | ('optimize_py', 'optimize')) |
| 37 | |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 38 | |
| 39 | def run (self): |
| 40 | |
| 41 | self.set_final_options () |
| 42 | |
| 43 | # Dump entire contents of the build directory to the installation |
| 44 | # directory (that's the beauty of having a build directory!) |
Greg Ward | 0f72695 | 1999-05-02 21:39:13 +0000 | [diff] [blame] | 45 | outfiles = self.copy_tree (self.build_dir, self.dir) |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 46 | |
Greg Ward | 0f72695 | 1999-05-02 21:39:13 +0000 | [diff] [blame] | 47 | # (Optionally) compile .py to .pyc |
| 48 | # XXX hey! we can't control whether we optimize or not; that's up |
| 49 | # to the invocation of the current Python interpreter (at least |
| 50 | # according to the py_compile docs). That sucks. |
| 51 | |
| 52 | if self.compile: |
| 53 | from py_compile import compile |
| 54 | |
| 55 | for f in outfiles: |
| 56 | # XXX can't assume this filename mapping! |
Greg Ward | 0f72695 | 1999-05-02 21:39:13 +0000 | [diff] [blame] | 57 | |
Greg Ward | 440e2f5 | 1999-08-29 18:19:37 +0000 | [diff] [blame^] | 58 | # only compile the file if it is actually a .py file |
| 59 | if f[-3:] == '.py': |
| 60 | out_fn = string.replace (f, '.py', '.pyc') |
| 61 | |
| 62 | self.make_file (f, out_fn, compile, (f,), |
| 63 | "compiling %s -> %s" % (f, out_fn), |
| 64 | "compilation of %s skipped" % f) |
| 65 | |
Greg Ward | 0f72695 | 1999-05-02 21:39:13 +0000 | [diff] [blame] | 66 | # XXX ignore self.optimize for now, since we don't really know if |
| 67 | # we're compiling optimally or not, and couldn't pick what to do |
| 68 | # even if we did know. ;-( |
| 69 | |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 70 | # run () |
| 71 | |
| 72 | # class InstallPy |