1 # created 1999/03/13, Greg Ward
6 from distutils
.core
import Command
7 from distutils
.util
import copy_tree
9 class install_lib (Command
):
11 description
= "install pure Python modules"
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
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'))
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.
59 from py_compile
import compile
62 # only compile the file if it is actually a .py file
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
)
75 def _mutate_outputs (self
, has_any
, build_cmd
, cmd_option
, output_dir
):
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
)
86 for file in build_files
:
87 outputs
.append (os
.path
.join (output_dir
, file[prefix_len
:]))
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."""
99 self
._mutate
_outputs
(self
.distribution
.has_pure_modules(),
100 'build_py', 'build_lib',
105 self
._mutate
_outputs
(self
.distribution
.has_ext_modules(),
106 'build_ext', 'build_lib',
109 return pure_outputs
+ ext_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()'."""
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())