blob: 852e3f63a2cbc3e302cca39b58b500ac0a380b21 [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 Ward440e2f51999-08-29 18:19:37 +000062 # only compile the file if it is actually a .py file
63 if f[-3:] == '.py':
Greg Warded8a0e02000-03-29 03:29:34 +000064 out_fn = f + (__debug__ and "c" or "o")
65 compile_msg = "byte-compiling %s to %s" % \
66 (f, os.path.basename (out_fn))
67 skip_msg = "byte-compilation of %s skipped" % f
Greg Ward440e2f51999-08-29 18:19:37 +000068 self.make_file (f, out_fn, compile, (f,),
Greg Warded8a0e02000-03-29 03:29:34 +000069 compile_msg, skip_msg)
70
71
Greg Ward13ae1c81999-03-22 14:55:25 +000072 # run ()
73
Greg Warde5dfba52000-03-29 02:17:42 +000074
75 def _mutate_outputs (self, has_any, build_cmd, cmd_option, output_dir):
76
77 if not has_any:
78 return []
79
80 build_cmd = self.find_peer (build_cmd)
81 build_files = build_cmd.get_outputs()
Greg Ward4779cdf2000-05-07 15:32:13 +000082 build_dir = getattr (build_cmd, cmd_option)
Greg Warde5dfba52000-03-29 02:17:42 +000083
84 prefix_len = len (build_dir) + len (os.sep)
85 outputs = []
86 for file in build_files:
87 outputs.append (os.path.join (output_dir, file[prefix_len:]))
88
89 return outputs
90
91 # _mutate_outputs ()
92
93 def get_outputs (self):
94 """Return the list of files that would be installed if this command
95 were actually run. Not affected by the "dry-run" flag or whether
96 modules have actually been built yet."""
97
98 pure_outputs = \
99 self._mutate_outputs (self.distribution.has_pure_modules(),
100 'build_py', 'build_lib',
101 self.install_dir)
102
103
104 ext_outputs = \
105 self._mutate_outputs (self.distribution.has_ext_modules(),
106 'build_ext', 'build_lib',
107 self.install_dir)
108
109 return pure_outputs + ext_outputs
110
111 # get_outputs ()
112
Greg Ward1aab6e92000-03-31 02:53:07 +0000113 def get_inputs (self):
114 """Get the list of files that are input to this command, ie. the
115 files that get installed as they are named in the build tree.
116 The files in this list correspond one-to-one to the output
117 filenames returned by 'get_outputs()'."""
118
119 inputs = []
120
121 if self.distribution.has_pure_modules():
122 build_py = self.find_peer ('build_py')
123 inputs.extend (build_py.get_outputs())
124
125 if self.distribution.has_ext_modules():
126 build_ext = self.find_peer ('build_ext')
127 inputs.extend (build_ext.get_outputs())
128
129 return inputs
130
131
132
Greg Warde5dfba52000-03-29 02:17:42 +0000133# class install_lib