Quick update to the README file. For intros and books we now point to
[python/dscho.git] / Lib / distutils / command / install_lib.py
blob5740c5eed2f5fdb912be46988e66e77e03142fe8
1 # created 1999/03/13, Greg Ward
3 __revision__ = "$Id$"
5 import sys, os, string
6 from distutils.core import Command
7 from distutils.util import copy_tree
9 class install_lib (Command):
11 description = "install pure Python modules"
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)"),
21 def initialize_options (self):
22 # let the 'install' command dictate our installation directory
23 self.install_dir = None
24 self.build_dir = None
25 self.compile = 1
26 self.optimize = 1
28 def finalize_options (self):
30 # 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.
33 self.set_undefined_options ('install',
34 ('build_lib', 'build_dir'),
35 ('install_lib', 'install_dir'),
36 ('compile_py', 'compile'),
37 ('optimize_py', 'optimize'))
40 def run (self):
42 # 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')
48 # 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!)
51 outfiles = self.copy_tree (self.build_dir, self.install_dir)
53 # (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.
58 if self.compile:
59 from py_compile import compile
61 for f in outfiles:
62 # only compile the file if it is actually a .py file
63 if f[-3:] == '.py':
64 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
68 self.make_file (f, out_fn, compile, (f,),
69 compile_msg, skip_msg)
72 # run ()
75 def _mutate_outputs (self, has_any, build_cmd, cmd_option, output_dir):
77 if not has_any:
78 return []
80 build_cmd = self.find_peer (build_cmd)
81 build_files = build_cmd.get_outputs()
82 build_dir = build_cmd.get_option (cmd_option)
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:]))
89 return outputs
91 # _mutate_outputs ()
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."""
98 pure_outputs = \
99 self._mutate_outputs (self.distribution.has_pure_modules(),
100 'build_py', 'build_lib',
101 self.install_dir)
104 ext_outputs = \
105 self._mutate_outputs (self.distribution.has_ext_modules(),
106 'build_ext', 'build_lib',
107 self.install_dir)
109 return pure_outputs + ext_outputs
111 # get_outputs ()
113 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()'."""
119 inputs = []
121 if self.distribution.has_pure_modules():
122 build_py = self.find_peer ('build_py')
123 inputs.extend (build_py.get_outputs())
125 if self.distribution.has_ext_modules():
126 build_ext = self.find_peer ('build_ext')
127 inputs.extend (build_ext.get_outputs())
129 return inputs
133 # class install_lib