blob: eaaa1c7da2763422cf48d2528592022b72d74ca6 [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 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 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 Ward7478a482000-01-30 15:07:56 +000042 # Make sure we have "built" all pure Python modules first
43 self.run_peer ('build_py')
44
Greg Ward4f08e4f2000-02-26 00:49:04 +000045 # Install everything: simply dump the entire contents of the build
46 # directory to the installation directory (that's the beauty of
47 # having a build directory!)
Greg Warde6ac2fc1999-09-29 12:38:18 +000048 outfiles = self.copy_tree (self.build_dir, self.install_dir)
Greg Ward13ae1c81999-03-22 14:55:25 +000049
Greg Ward0f726951999-05-02 21:39:13 +000050 # (Optionally) compile .py to .pyc
51 # XXX hey! we can't control whether we optimize or not; that's up
52 # to the invocation of the current Python interpreter (at least
53 # according to the py_compile docs). That sucks.
54
55 if self.compile:
56 from py_compile import compile
57
58 for f in outfiles:
Greg Ward4f08e4f2000-02-26 00:49:04 +000059 # XXX can't assume this filename mapping! (what if
60 # we're running under "python -O"?)
Greg Ward0f726951999-05-02 21:39:13 +000061
Greg Ward440e2f51999-08-29 18:19:37 +000062 # only compile the file if it is actually a .py file
63 if f[-3:] == '.py':
64 out_fn = string.replace (f, '.py', '.pyc')
65
66 self.make_file (f, out_fn, compile, (f,),
Greg Wardef9ad6d1999-10-03 21:03:26 +000067 "byte-compiling %s" % f,
68 "byte-compilation of %s skipped" % f)
Greg Ward440e2f51999-08-29 18:19:37 +000069
Greg Ward0f726951999-05-02 21:39:13 +000070 # XXX ignore self.optimize for now, since we don't really know if
71 # we're compiling optimally or not, and couldn't pick what to do
72 # even if we did know. ;-(
73
Greg Ward13ae1c81999-03-22 14:55:25 +000074 # run ()
75
76# class InstallPy