[Python-checkins] python/dist/src/Lib/distutils dist.py,1.68,1.69

fdrake at users.sourceforge.net fdrake at users.sourceforge.net
Tue Aug 3 18:37:43 CEST 2004


Update of /cvsroot/python/python/dist/src/Lib/distutils
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23612/Lib/distutils

Modified Files:
	dist.py 
Log Message:
This allows additional commands to be provided for existing setup.py
scripts without modifying either the distutils installation or the
setup.py scripts of packages with which the new commands will be used.

Specifically, an option is added to distutils that allows additional
packages to be searched for command implementations in addition to
distutils.command.  The additional packages can be specified on the
command line or via the installation or personal configuration files
already loaded by distutils.

For discussion, see the thread starting with:

http://mail.python.org/pipermail/distutils-sig/2004-August/004112.html

This closes SF patch #102241.


Index: dist.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/distutils/dist.py,v
retrieving revision 1.68
retrieving revision 1.69
diff -C2 -d -r1.68 -r1.69
*** dist.py	18 Jul 2004 06:14:42 -0000	1.68
--- dist.py	3 Aug 2004 16:37:39 -0000	1.69
***************
*** 142,145 ****
--- 142,153 ----
          self.cmdclass = {}
  
+         # 'command_packages' is a list of packages in which commands
+         # are searched for.  The factory for command 'foo' is expected
+         # to be named 'foo' in the module 'foo' in one of the packages
+         # named here.  This list is searched from the left; an error
+         # is raised if no named package provides the command being
+         # searched for.  (Always access using get_command_packages().)
+         self.command_packages = None
+ 
          # 'script_name' and 'script_args' are usually set to sys.argv[0]
          # and sys.argv[1:], but they can be overridden when the caller is
***************
*** 407,410 ****
--- 415,420 ----
                      elif opt in ('verbose', 'dry_run'): # ugh!
                          setattr(self, opt, strtobool(val))
+                     else:
+                         setattr(self, opt, val)
                  except ValueError, msg:
                      raise DistutilsOptionError, msg
***************
*** 438,446 ****
          # that allows the user to interactively specify the "command line".
          #
          if sys.platform == 'mac':
              import EasyDialogs
              cmdlist = self.get_command_list()
              self.script_args = EasyDialogs.GetArgv(
!                 self.global_options + self.display_options, cmdlist)
  
          # We have to parse the command line a bit at a time -- global
--- 448,457 ----
          # that allows the user to interactively specify the "command line".
          #
+         toplevel_options = self._get_toplevel_options()
          if sys.platform == 'mac':
              import EasyDialogs
              cmdlist = self.get_command_list()
              self.script_args = EasyDialogs.GetArgv(
!                 toplevel_options + self.display_options, cmdlist)
  
          # We have to parse the command line a bit at a time -- global
***************
*** 452,456 ****
  
          self.commands = []
!         parser = FancyGetopt(self.global_options + self.display_options)
          parser.set_negative_aliases(self.negative_opt)
          parser.set_aliases({'licence': 'license'})
--- 463,467 ----
  
          self.commands = []
!         parser = FancyGetopt(toplevel_options + self.display_options)
          parser.set_negative_aliases(self.negative_opt)
          parser.set_aliases({'licence': 'license'})
***************
*** 489,492 ****
--- 500,514 ----
      # parse_command_line()
  
+     def _get_toplevel_options (self):
+         """Return the non-display options recognized at the top level.
+ 
+         This includes options that are recognized *only* at the top
+         level as well as options recognized for commands.
+         """
+         return self.global_options + [
+             ("command-packages=", None,
+              "list of packages that provide distutils commands"),
+             ]
+ 
      def _parse_command_opts (self, parser, args):
          """Parse the command-line options for a single command.
***************
*** 587,591 ****
      # _parse_command_opts ()
  
- 
      def finalize_options (self):
          """Set final values for all the options on the Distribution
--- 609,612 ----
***************
*** 628,632 ****
  
          if global_options:
!             parser.set_option_table(self.global_options)
              parser.print_help("Global options:")
              print
--- 649,657 ----
  
          if global_options:
!             if display_options:
!                 options = self._get_toplevel_options()
!             else:
!                 options = self.global_options
!             parser.set_option_table(options)
              parser.print_help("Global options:")
              print
***************
*** 792,795 ****
--- 817,833 ----
      # -- Command class/object methods ----------------------------------
  
+     def get_command_packages (self):
+         """Return a list of packages from which commands are loaded."""
+         pkgs = self.command_packages
+         if not isinstance(pkgs, type([])):
+             pkgs = string.split(pkgs or "", ",")
+             for i in range(len(pkgs)):
+                 pkgs[i] = string.strip(pkgs[i])
+             pkgs = filter(None, pkgs)
+             if "distutils.command" not in pkgs:
+                 pkgs.insert(0, "distutils.command")
+             self.command_packages = pkgs
+         return pkgs
+ 
      def get_command_class (self, command):
          """Return the class that implements the Distutils command named by
***************
*** 808,831 ****
              return klass
  
!         module_name = 'distutils.command.' + command
!         klass_name = command
  
!         try:
!             __import__ (module_name)
!             module = sys.modules[module_name]
!         except ImportError:
!             raise DistutilsModuleError, \
!                   "invalid command '%s' (no module named '%s')" % \
!                   (command, module_name)
  
!         try:
!             klass = getattr(module, klass_name)
!         except AttributeError:
!             raise DistutilsModuleError, \
!                   "invalid command '%s' (no class '%s' in module '%s')" \
!                   % (command, klass_name, module_name)
  
-         self.cmdclass[command] = klass
-         return klass
  
      # get_command_class ()
--- 846,871 ----
              return klass
  
!         for pkgname in self.get_command_packages():
!             module_name = "%s.%s" % (pkgname, command)
!             klass_name = command
  
!             try:
!                 __import__ (module_name)
!                 module = sys.modules[module_name]
!             except ImportError:
!                 continue
  
!             try:
!                 klass = getattr(module, klass_name)
!             except AttributeError:
!                 raise DistutilsModuleError, \
!                       "invalid command '%s' (no class '%s' in module '%s')" \
!                       % (command, klass_name, module_name)
! 
!             self.cmdclass[command] = klass
!             return klass
! 
!         raise DistutilsModuleError("invalid command '%s'" % command)
  
  
      # get_command_class ()



More information about the Python-checkins mailing list