blob: 919873c6f285de80a761291a7db365d86e6c6c4b [file] [log] [blame]
Greg Ward13ae1c81999-03-22 14:55:25 +00001# created 1999/03/13, Greg Ward
2
3__rcsid__ = "$Id$"
4
Greg Ward0f726951999-05-02 21:39:13 +00005import sys, string
Greg Ward13ae1c81999-03-22 14:55:25 +00006from distutils.core import Command
7from distutils.util import copy_tree
8
Greg Ward1993f9a2000-02-18 00:13:53 +00009class install_py (Command):
Greg Ward13ae1c81999-03-22 14:55:25 +000010
Greg Ward37bc8152000-01-30 18:34:15 +000011 description = "install pure Python modules"
12
Greg Wardbbeceea2000-02-18 00:25:39 +000013 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 Ward0f726951999-05-02 21:39:13 +000019
Greg Ward13ae1c81999-03-22 14:55:25 +000020
Greg Warde01149c2000-02-18 00:35:22 +000021 def initialize_options (self):
Greg Ward13ae1c81999-03-22 14:55:25 +000022 # let the 'install' command dictate our installation directory
Greg Warde6ac2fc1999-09-29 12:38:18 +000023 self.install_dir = None
Greg Ward13ae1c81999-03-22 14:55:25 +000024 self.build_dir = None
Greg Ward0f726951999-05-02 21:39:13 +000025 self.compile = 1
26 self.optimize = 1
Greg Ward13ae1c81999-03-22 14:55:25 +000027
Greg Warde01149c2000-02-18 00:35:22 +000028 def finalize_options (self):
Greg Ward13ae1c81999-03-22 14:55:25 +000029
Greg Wardfa4eb181999-09-13 13:58:34 +000030 # 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 Ward13ae1c81999-03-22 14:55:25 +000043 self.set_undefined_options ('install',
44 ('build_lib', 'build_dir'),
Greg Warde6ac2fc1999-09-29 12:38:18 +000045 (dir_option, 'install_dir'),
Greg Ward0f726951999-05-02 21:39:13 +000046 ('compile_py', 'compile'),
47 ('optimize_py', 'optimize'))
48
Greg Ward13ae1c81999-03-22 14:55:25 +000049
50 def run (self):
51
Greg Ward7478a482000-01-30 15:07:56 +000052 # Make sure we have "built" all pure Python modules first
53 self.run_peer ('build_py')
54
Greg Ward13ae1c81999-03-22 14:55:25 +000055 # Dump entire contents of the build directory to the installation
56 # directory (that's the beauty of having a build directory!)
Greg Warde6ac2fc1999-09-29 12:38:18 +000057 outfiles = self.copy_tree (self.build_dir, self.install_dir)
Greg Ward13ae1c81999-03-22 14:55:25 +000058
Greg Ward0f726951999-05-02 21:39:13 +000059 # (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 Ward0f726951999-05-02 21:39:13 +000069
Greg Ward440e2f51999-08-29 18:19:37 +000070 # 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 Wardef9ad6d1999-10-03 21:03:26 +000075 "byte-compiling %s" % f,
76 "byte-compilation of %s skipped" % f)
Greg Ward440e2f51999-08-29 18:19:37 +000077
Greg Ward0f726951999-05-02 21:39:13 +000078 # 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 Ward13ae1c81999-03-22 14:55:25 +000082 # run ()
83
84# class InstallPy