blob: e898ce0c187540a7fadc3899700cf114ede0d58b [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
Greg Ward1993f9a2000-02-18 00:13:53 +00009class install_py (Command):
Greg Ward13ae1c81999-03-22 14:55:25 +000010
Greg Ward37bc8152000-01-30 18:34:15 +000011 description = "install pure Python modules"
12
Greg Warde6ac2fc1999-09-29 12:38:18 +000013 options = [('install-dir=', 'd', "directory to install to"),
Greg Ward440e2f51999-08-29 18:19:37 +000014 ('build-dir=','b', "build directory (where to install from)"),
Greg Ward0f726951999-05-02 21:39:13 +000015 ('compile', 'c', "compile .py to .pyc"),
16 ('optimize', 'o', "compile .py to .pyo (optimized)"),
17 ]
18
Greg Ward13ae1c81999-03-22 14:55:25 +000019
20 def set_default_options (self):
21 # let the 'install' command dictate our installation directory
Greg Warde6ac2fc1999-09-29 12:38:18 +000022 self.install_dir = None
Greg Ward13ae1c81999-03-22 14:55:25 +000023 self.build_dir = None
Greg Ward0f726951999-05-02 21:39:13 +000024 self.compile = 1
25 self.optimize = 1
Greg Ward13ae1c81999-03-22 14:55:25 +000026
27 def set_final_options (self):
Greg Ward13ae1c81999-03-22 14:55:25 +000028
Greg Wardfa4eb181999-09-13 13:58:34 +000029 # Find out from the 'build_ext' command if we were asked to build
30 # any extensions. If so, that means even pure-Python modules in
31 # this distribution have to be installed to the "platlib"
32 # directory.
33 extensions = self.get_peer_option ('build_ext', 'extensions')
34 if extensions:
35 dir_option = 'install_site_platlib'
36 else:
37 dir_option = 'install_site_lib'
38
39 # Get all the information we need to install pure Python modules
40 # from the umbrella 'install' command -- build (source) directory,
41 # install (target) directory, and whether to compile .py files.
Greg Ward13ae1c81999-03-22 14:55:25 +000042 self.set_undefined_options ('install',
43 ('build_lib', 'build_dir'),
Greg Warde6ac2fc1999-09-29 12:38:18 +000044 (dir_option, 'install_dir'),
Greg Ward0f726951999-05-02 21:39:13 +000045 ('compile_py', 'compile'),
46 ('optimize_py', 'optimize'))
47
Greg Ward13ae1c81999-03-22 14:55:25 +000048
49 def run (self):
50
Greg Ward7478a482000-01-30 15:07:56 +000051 # Make sure we have "built" all pure Python modules first
52 self.run_peer ('build_py')
53
Greg Ward13ae1c81999-03-22 14:55:25 +000054 # Dump entire contents of the build directory to the installation
55 # directory (that's the beauty of having a build directory!)
Greg Warde6ac2fc1999-09-29 12:38:18 +000056 outfiles = self.copy_tree (self.build_dir, self.install_dir)
Greg Ward13ae1c81999-03-22 14:55:25 +000057
Greg Ward0f726951999-05-02 21:39:13 +000058 # (Optionally) compile .py to .pyc
59 # XXX hey! we can't control whether we optimize or not; that's up
60 # to the invocation of the current Python interpreter (at least
61 # according to the py_compile docs). That sucks.
62
63 if self.compile:
64 from py_compile import compile
65
66 for f in outfiles:
67 # XXX can't assume this filename mapping!
Greg Ward0f726951999-05-02 21:39:13 +000068
Greg Ward440e2f51999-08-29 18:19:37 +000069 # only compile the file if it is actually a .py file
70 if f[-3:] == '.py':
71 out_fn = string.replace (f, '.py', '.pyc')
72
73 self.make_file (f, out_fn, compile, (f,),
Greg Wardef9ad6d1999-10-03 21:03:26 +000074 "byte-compiling %s" % f,
75 "byte-compilation of %s skipped" % f)
Greg Ward440e2f51999-08-29 18:19:37 +000076
Greg Ward0f726951999-05-02 21:39:13 +000077 # XXX ignore self.optimize for now, since we don't really know if
78 # we're compiling optimally or not, and couldn't pick what to do
79 # even if we did know. ;-(
80
Greg Ward13ae1c81999-03-22 14:55:25 +000081 # run ()
82
83# class InstallPy