[Python-checkins] CVS: distutils/distutils dist.py,1.20,1.21

Greg Ward python-dev@python.org
Sun, 28 May 2000 16:53:09 -0700


Update of /cvsroot/python/distutils/distutils
In directory slayer.i.sourceforge.net:/tmp/cvs-serv10408

Modified Files:
	dist.py 
Log Message:
Factored '_set_command_options()' out of 'get_command_obj()'.
Added 'reinitialize_command()' -- lets us "push" option values in
  a controlled, safe way; this is a small change to the code, but
  a big change to the Distutils philosophy of passing option values
  around.  The preferred mode is still definitely to "pull" options
  from another command (eg. "install" fetches the base build directory
  from "build"), but it is now feasible to "push" options onto another
  command, when you know what's best for it.  One possible application
  will be a "config" command, which pokes around the system and pushes
  values (eg. include and library directories) onto the "build" command.
Added 'dump_option_dicts()' method (for debugging output).


Index: dist.py
===================================================================
RCS file: /cvsroot/python/distutils/distutils/dist.py,v
retrieving revision 1.20
retrieving revision 1.21
diff -C2 -r1.20 -r1.21
*** dist.py	2000/05/27 17:27:22	1.20
--- dist.py	2000/05/28 23:53:06	1.21
***************
*** 7,11 ****
  # (extricated from core.py; actually dates back to the beginning)
  
! __revision__ = "$Id: dist.py,v 1.20 2000/05/27 17:27:22 gward Exp $"
  
  import sys, os, string, re
--- 7,11 ----
  # (extricated from core.py; actually dates back to the beginning)
  
! __revision__ = "$Id: dist.py,v 1.21 2000/05/28 23:53:06 gward Exp $"
  
  import sys, os, string, re
***************
*** 218,221 ****
--- 218,250 ----
  
  
+     def dump_option_dicts (self, header=None, commands=None, indent=""):
+         from pprint import pformat
+ 
+         if commands is None:             # dump all command option dicts
+             commands = self.command_options.keys()
+             commands.sort()
+ 
+         if header is not None:
+             print indent + header
+             indent = indent + "  "
+ 
+         if not commands:
+             print indent + "no commands known yet"
+             return
+ 
+         for cmd_name in commands:
+             opt_dict = self.command_options.get(cmd_name)
+             if opt_dict is None:
+                 print indent + "no option dict for '%s' command" % cmd_name
+             else:
+                 print indent + "option dict for '%s' command:" % cmd_name
+                 out = pformat(opt_dict)
+                 for line in string.split(out, "\n"):
+                     print indent + "  " + line
+ 
+     # dump_option_dicts ()
+             
+ 
+ 
      # -- Config file finding/parsing methods ---------------------------
  
***************
*** 633,646 ****
              options = self.command_options.get(command)
              if options:
!                 print "  setting options:"
!                 for (option, (source, value)) in options.items():
!                     print "    %s = %s (from %s)" % (option, value, source)
!                     if not hasattr(cmd_obj, option):
!                         raise DistutilsOptionError, \
!                               ("%s: command '%s' has no such option '%s'") % \
!                               (source, command, option)
!                     setattr(cmd_obj, option, value)
  
          return cmd_obj
  
          
--- 662,720 ----
              options = self.command_options.get(command)
              if options:
!                 self._set_command_options(cmd_obj, options)
  
          return cmd_obj
+ 
+     def _set_command_options (self, command_obj, option_dict=None):
+ 
+         """Set the options for 'command_obj' from 'option_dict'.  Basically
+         this means copying elements of a dictionary ('option_dict') to
+         attributes of an instance ('command').
+ 
+         'command_obj' must be a Commnd instance.  If 'option_dict' is not
+         supplied, uses the standard option dictionary for this command
+         (from 'self.command_options').
+         """
+         from distutils.core import DEBUG
+         
+         command_name = command_obj.get_command_name()
+         if option_dict is None:
+             option_dict = self.get_option_dict(command_name)
+ 
+         if DEBUG: print "  setting options for '%s' command:" % command_name
+         for (option, (source, value)) in option_dict.items():
+             if DEBUG: print "    %s = %s (from %s)" % (option, value, source)
+             if not hasattr(command_obj, option):
+                 raise DistutilsOptionError, \
+                       ("error in %s: command '%s' has no such option '%s'") % \
+                       (source, command_name, option)
+             setattr(command_obj, option, value)
+ 
+     def reinitialize_command (self, command):
+         """Reinitializes a command to the state it was in when first
+         returned by 'get_command_obj()': ie., initialized but not yet
+         finalized.  This gives provides the opportunity to sneak option
+         values in programmatically, overriding or supplementing
+         user-supplied values from the config files and command line.
+         You'll have to re-finalize the command object (by calling
+         'finalize_options()' or 'ensure_finalized()') before using it for
+         real.  
+ 
+         'command' should be a command name (string) or command object.
+         Returns the reinitialized command object.
+         """
+         from distutils.cmd import Command
+         if not isinstance(command, Command):
+             command_name = command
+             command = self.get_command_obj(command_name)
+         else:
+             command_name = command.get_command_name()
+ 
+         if not command.finalized:
+             return
+         command.initialize_options()
+         command.finalized = 0
+         self._set_command_options(command)
+         return command