diff options
author | Greg Ward <gward@python.net> | 2000-10-02 02:15:08 (GMT) |
---|---|---|
committer | Greg Ward <gward@python.net> | 2000-10-02 02:15:08 (GMT) |
commit | cb9c9aed01abc99cad2fbadd0b407d226bbbb2ba (patch) | |
tree | fbd48b443553d50a93878964fcb48b73a3e22b60 /Lib | |
parent | 04cc88df05d85d0c8908d912b37c86108e55eccd (diff) | |
download | cpython-cb9c9aed01abc99cad2fbadd0b407d226bbbb2ba.zip cpython-cb9c9aed01abc99cad2fbadd0b407d226bbbb2ba.tar.gz cpython-cb9c9aed01abc99cad2fbadd0b407d226bbbb2ba.tar.bz2 |
Finished the overhaul of byte-compilation options: there's now a 6-way
choice between (compile, no-compile) * (optimize=0, optimize=1,
optimize=2). Details:
- added --no-compile option to complement --compile, which has
been there for ages
- changed --optimize (which never worked) to a value option, which
expects 0, 1, or 2
- renamed 'bytecompile()' method to 'byte_compile()', and beefed
it up to handle both 'compile' and 'optimize' options
- fix '_bytecode_filenames()' to respect the new options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/distutils/command/install_lib.py | 81 |
1 files changed, 66 insertions, 15 deletions
diff --git a/Lib/distutils/command/install_lib.py b/Lib/distutils/command/install_lib.py index 2396eed..80da3ac 100644 --- a/Lib/distutils/command/install_lib.py +++ b/Lib/distutils/command/install_lib.py @@ -3,24 +3,44 @@ __revision__ = "$Id$" import sys, os, string +from types import IntType from distutils.core import Command +from distutils.errors import DistutilsOptionError from distutils.dir_util import copy_tree -from distutils.util import byte_compile class install_lib (Command): description = "install all Python modules (extensions and pure Python)" + # The byte-compilation options are a tad confusing. Here are the + # possible scenarios: + # 1) no compilation at all (--no-compile --no-optimize) + # 2) compile .pyc only (--compile --no-optimize; default) + # 3) compile .pyc and "level 1" .pyo (--compile --optimize) + # 4) compile "level 1" .pyo only (--no-compile --optimize) + # 5) compile .pyc and "level 2" .pyo (--compile --optimize-more) + # 6) compile "level 2" .pyo only (--no-compile --optimize-more) + # + # The UI for this is two option, 'compile' and 'optimize'. + # 'compile' is strictly boolean, and only decides whether to + # generate .pyc files. 'optimize' is three-way (0, 1, or 2), and + # decides both whether to generate .pyo files and what level of + # optimization to use. + user_options = [ ('install-dir=', 'd', "directory to install to"), ('build-dir=','b', "build directory (where to install from)"), ('force', 'f', "force installation (overwrite existing files)"), - ('compile', 'c', "compile .py to .pyc"), - ('optimize', 'o', "compile .py to .pyo (optimized)"), + ('compile', 'c', "compile .py to .pyc [default]"), + ('no-compile', None, "don't compile .py files"), + ('optimize=', 'O', + "also compile with optimization: -O1 for \"python -O\", " + "-O2 for \"python -OO\", and -O0 to disable [default: -O0]"), ('skip-build', None, "skip the build steps"), ] - boolean_options = ['force', 'compile', 'optimize', 'skip-build'] + boolean_options = ['force', 'compile', 'skip-build'] + negative_opt = {'no-compile' : 'compile'} def initialize_options (self): @@ -28,8 +48,8 @@ class install_lib (Command): self.install_dir = None self.build_dir = None self.force = 0 - self.compile = 1 - self.optimize = 1 + self.compile = None + self.optimize = None self.skip_build = None def finalize_options (self): @@ -41,11 +61,25 @@ class install_lib (Command): ('build_lib', 'build_dir'), ('install_lib', 'install_dir'), ('force', 'force'), - ('compile_py', 'compile'), - ('optimize_py', 'optimize'), + ('compile', 'compile'), + ('optimize', 'optimize'), ('skip_build', 'skip_build'), ) + if self.compile is None: + self.compile = 1 + if self.optimize is None: + self.optimize = 0 + + print "install_lib: compile=%s, optimize=%s" % \ + (`self.compile`, `self.optimize`) + if type(self.optimize) is not IntType: + try: + self.optimize = int(self.optimize) + assert 0 <= self.optimize <= 2 + except (ValueError, AssertionError): + raise DistutilsOptionError, "optimize must be 0, 1, or 2" + def run (self): # Make sure we have built everything we need first @@ -58,7 +92,7 @@ class install_lib (Command): # (Optionally) compile .py to .pyc if outfiles is not None and self.distribution.has_pure_modules(): - self.bytecompile(outfiles) + self.byte_compile(outfiles) # run () @@ -82,10 +116,25 @@ class install_lib (Command): return return outfiles - def bytecompile (self, files): - byte_compile(files, - force=self.force, - verbose=self.verbose, dry_run=self.dry_run) + def byte_compile (self, files): + from distutils.util import byte_compile + + # Get the "--root" directory supplied to the "install" command, + # and use it as a prefix to strip off the purported filename + # encoded in bytecode files. This is far from complete, but it + # should at least generate usable bytecode in RPM distributions. + install_root = self.get_finalized_command('install').root + + if self.compile: + byte_compile(files, optimize=0, + force=self.force, + prefix=install_root, + verbose=self.verbose, dry_run=self.dry_run) + if self.optimize > 0: + byte_compile(files, optimize=self.optimize, + force=self.force, + prefix=install_root, + verbose=self.verbose, dry_run=self.dry_run) # -- Utility methods ----------------------------------------------- @@ -111,8 +160,10 @@ class install_lib (Command): def _bytecode_filenames (self, py_filenames): bytecode_files = [] for py_file in py_filenames: - bytecode = py_file + (__debug__ and "c" or "o") - bytecode_files.append(bytecode) + if self.compile: + bytecode_files.append(py_file + "c") + if self.optmize > 0: + bytecode_files.append(py_file + "o") return bytecode_files |