blob: 7e1f2e2a4dbbf082235a364df118a2eb887f091a [file] [log] [blame]
Greg Ward13ae1c81999-03-22 14:55:25 +00001# created 1999/03/13, Greg Ward
2
Greg Ward3ce77fd2000-03-02 01:49:45 +00003__revision__ = "$Id$"
Greg Ward13ae1c81999-03-22 14:55:25 +00004
Greg Warde5dfba52000-03-29 02:17:42 +00005import sys, os, string
Greg Ward13ae1c81999-03-22 14:55:25 +00006from distutils.core import Command
7from distutils.util import copy_tree
8
Greg Ward297dd9f2000-03-23 04:37:11 +00009class install_lib (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 # Get all the information we need to install pure Python modules
31 # from the umbrella 'install' command -- build (source) directory,
32 # install (target) directory, and whether to compile .py files.
Greg Ward13ae1c81999-03-22 14:55:25 +000033 self.set_undefined_options ('install',
34 ('build_lib', 'build_dir'),
Greg Ward4f08e4f2000-02-26 00:49:04 +000035 ('install_lib', 'install_dir'),
Greg Ward0f726951999-05-02 21:39:13 +000036 ('compile_py', 'compile'),
37 ('optimize_py', 'optimize'))
38
Greg Ward13ae1c81999-03-22 14:55:25 +000039
40 def run (self):
41
Greg Warde5dfba52000-03-29 02:17:42 +000042 # Make sure we have built everything we need first
43 if self.distribution.has_pure_modules():
44 self.run_peer ('build_py')
45 if self.distribution.has_ext_modules():
46 self.run_peer ('build_ext')
Greg Ward7478a482000-01-30 15:07:56 +000047
Greg Ward4f08e4f2000-02-26 00:49:04 +000048 # Install everything: simply dump the entire contents of the build
49 # directory to the installation directory (that's the beauty of
50 # having a build directory!)
Greg Warde6ac2fc1999-09-29 12:38:18 +000051 outfiles = self.copy_tree (self.build_dir, self.install_dir)
Greg Warde5dfba52000-03-29 02:17:42 +000052
Greg Ward0f726951999-05-02 21:39:13 +000053 # (Optionally) compile .py to .pyc
54 # XXX hey! we can't control whether we optimize or not; that's up
55 # to the invocation of the current Python interpreter (at least
56 # according to the py_compile docs). That sucks.
57
58 if self.compile:
59 from py_compile import compile
60
61 for f in outfiles:
Greg Ward4f08e4f2000-02-26 00:49:04 +000062 # XXX can't assume this filename mapping! (what if
63 # we're running under "python -O"?)
Greg Ward0f726951999-05-02 21:39:13 +000064
Greg Ward440e2f51999-08-29 18:19:37 +000065 # only compile the file if it is actually a .py file
66 if f[-3:] == '.py':
67 out_fn = string.replace (f, '.py', '.pyc')
68
69 self.make_file (f, out_fn, compile, (f,),
Greg Wardef9ad6d1999-10-03 21:03:26 +000070 "byte-compiling %s" % f,
71 "byte-compilation of %s skipped" % f)
Greg Ward440e2f51999-08-29 18:19:37 +000072
Greg Ward0f726951999-05-02 21:39:13 +000073 # XXX ignore self.optimize for now, since we don't really know if
74 # we're compiling optimally or not, and couldn't pick what to do
75 # even if we did know. ;-(
76
Greg Ward13ae1c81999-03-22 14:55:25 +000077 # run ()
78
Greg Warde5dfba52000-03-29 02:17:42 +000079
80 def _mutate_outputs (self, has_any, build_cmd, cmd_option, output_dir):
81
82 if not has_any:
83 return []
84
85 build_cmd = self.find_peer (build_cmd)
86 build_files = build_cmd.get_outputs()
87 build_dir = build_cmd.get_option (cmd_option)
88
89 prefix_len = len (build_dir) + len (os.sep)
90 outputs = []
91 for file in build_files:
92 outputs.append (os.path.join (output_dir, file[prefix_len:]))
93
94 return outputs
95
96 # _mutate_outputs ()
97
98 def get_outputs (self):
99 """Return the list of files that would be installed if this command
100 were actually run. Not affected by the "dry-run" flag or whether
101 modules have actually been built yet."""
102
103 pure_outputs = \
104 self._mutate_outputs (self.distribution.has_pure_modules(),
105 'build_py', 'build_lib',
106 self.install_dir)
107
108
109 ext_outputs = \
110 self._mutate_outputs (self.distribution.has_ext_modules(),
111 'build_ext', 'build_lib',
112 self.install_dir)
113
114 return pure_outputs + ext_outputs
115
116 # get_outputs ()
117
118# class install_lib