blob: 50939a3431894d27341fdf0a46def5174cb1fdef [file] [log] [blame]
Greg Ward13ae1c81999-03-22 14:55:25 +00001# created 1999/03/13, Greg Ward
2
3__rcsid__ = "$Id$"
4
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
9class InstallPy (Command):
10
Greg Warde6ac2fc1999-09-29 12:38:18 +000011 options = [('install-dir=', 'd', "directory to install to"),
Greg Ward440e2f51999-08-29 18:19:37 +000012 ('build-dir=','b', "build directory (where to install from)"),
Greg Ward0f726951999-05-02 21:39:13 +000013 ('compile', 'c', "compile .py to .pyc"),
14 ('optimize', 'o', "compile .py to .pyo (optimized)"),
15 ]
16
Greg Ward13ae1c81999-03-22 14:55:25 +000017
18 def set_default_options (self):
19 # let the 'install' command dictate our installation directory
Greg Warde6ac2fc1999-09-29 12:38:18 +000020 self.install_dir = None
Greg Ward13ae1c81999-03-22 14:55:25 +000021 self.build_dir = None
Greg Ward0f726951999-05-02 21:39:13 +000022 self.compile = 1
23 self.optimize = 1
Greg Ward13ae1c81999-03-22 14:55:25 +000024
25 def set_final_options (self):
Greg Ward13ae1c81999-03-22 14:55:25 +000026
Greg Wardfa4eb181999-09-13 13:58:34 +000027 # Find out from the 'build_ext' command if we were asked to build
28 # any extensions. If so, that means even pure-Python modules in
29 # this distribution have to be installed to the "platlib"
30 # directory.
31 extensions = self.get_peer_option ('build_ext', 'extensions')
32 if extensions:
33 dir_option = 'install_site_platlib'
34 else:
35 dir_option = 'install_site_lib'
36
37 # Get all the information we need to install pure Python modules
38 # from the umbrella 'install' command -- build (source) directory,
39 # install (target) directory, and whether to compile .py files.
Greg Ward13ae1c81999-03-22 14:55:25 +000040 self.set_undefined_options ('install',
41 ('build_lib', 'build_dir'),
Greg Warde6ac2fc1999-09-29 12:38:18 +000042 (dir_option, 'install_dir'),
Greg Ward0f726951999-05-02 21:39:13 +000043 ('compile_py', 'compile'),
44 ('optimize_py', 'optimize'))
45
Greg Ward13ae1c81999-03-22 14:55:25 +000046
47 def run (self):
48
Greg Ward7478a482000-01-30 15:07:56 +000049 # Make sure we have "built" all pure Python modules first
50 self.run_peer ('build_py')
51
Greg Ward13ae1c81999-03-22 14:55:25 +000052 # Dump entire contents of the build directory to the installation
53 # directory (that's the beauty of having a build directory!)
Greg Warde6ac2fc1999-09-29 12:38:18 +000054 outfiles = self.copy_tree (self.build_dir, self.install_dir)
Greg Ward13ae1c81999-03-22 14:55:25 +000055
Greg Ward0f726951999-05-02 21:39:13 +000056 # (Optionally) compile .py to .pyc
57 # XXX hey! we can't control whether we optimize or not; that's up
58 # to the invocation of the current Python interpreter (at least
59 # according to the py_compile docs). That sucks.
60
61 if self.compile:
62 from py_compile import compile
63
64 for f in outfiles:
65 # XXX can't assume this filename mapping!
Greg Ward0f726951999-05-02 21:39:13 +000066
Greg Ward440e2f51999-08-29 18:19:37 +000067 # only compile the file if it is actually a .py file
68 if f[-3:] == '.py':
69 out_fn = string.replace (f, '.py', '.pyc')
70
71 self.make_file (f, out_fn, compile, (f,),
Greg Wardef9ad6d1999-10-03 21:03:26 +000072 "byte-compiling %s" % f,
73 "byte-compilation of %s skipped" % f)
Greg Ward440e2f51999-08-29 18:19:37 +000074
Greg Ward0f726951999-05-02 21:39:13 +000075 # XXX ignore self.optimize for now, since we don't really know if
76 # we're compiling optimally or not, and couldn't pick what to do
77 # even if we did know. ;-(
78
Greg Ward13ae1c81999-03-22 14:55:25 +000079 # run ()
80
81# class InstallPy