[Python-checkins] distutils2: Have pysetup read setup.cfg early to find custom commands (#14733).

eric.araujo python-checkins at python.org
Mon May 21 21:56:02 CEST 2012


http://hg.python.org/distutils2/rev/70831fde0ec4
changeset:   1344:70831fde0ec4
user:        Éric Araujo <merwok at netwok.org>
date:        Mon May 21 15:51:11 2012 -0400
summary:
  Have pysetup read setup.cfg early to find custom commands (#14733).

Independently found and fixed by me (with help from Luis Rojas) and
Janusz Lewandowski.

files:
  CHANGES.txt                  |   2 +
  CONTRIBUTORS.txt             |   1 +
  distutils2/run.py            |  23 +++++-------
  distutils2/tests/test_run.py |  42 +++++++++++++++++++----
  4 files changed, 47 insertions(+), 21 deletions(-)


diff --git a/CHANGES.txt b/CHANGES.txt
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -19,6 +19,8 @@
 - #13399: Display error message instead of unhandled traceback for missing
   actions, commands and options [patrice]
 - #10374: Recreate scripts everytime build_scripts is called [pierre paul]
+- #14733: Have pysetup read setup.cfg early enough to find custom commands
+  [éric, janusz]
 
 1.0a4 - 2012-03-13
 ------------------
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt
--- a/CONTRIBUTORS.txt
+++ b/CONTRIBUTORS.txt
@@ -48,6 +48,7 @@
 - Pierre Paul Lefebvre
 - Tshepang Lekhonkhobe
 - Alain Leufroy
+- Janusz Lewandowski
 - Martin von Löwis
 - Hugo Lopes Tavares
 - Guillermo López-Anglada
diff --git a/distutils2/run.py b/distutils2/run.py
--- a/distutils2/run.py
+++ b/distutils2/run.py
@@ -10,6 +10,7 @@
 from distutils2.dist import Distribution
 from distutils2.util import _is_archive_file, generate_setup_py
 from distutils2.command import get_command_class, STANDARD_COMMANDS
+from distutils2.command.cmd import Command
 from distutils2.install import install, install_local_project, remove
 from distutils2.database import get_distribution, get_distributions
 from distutils2.depgraph import generate_graph
@@ -254,6 +255,13 @@
     parser = dispatcher.parser
     args = args[1:]
 
+    # Find and parse the config file(s): they will override options from
+    # the setup script, but be overridden by the command line.
+    # XXX call the functions from config and kill the Distribution class
+    # (merging it into Dispatcher)
+    dist = Distribution()
+    dist.parse_config_files()
+
     commands = STANDARD_COMMANDS  # FIXME display extra commands
 
     if args == ['--list-commands']:
@@ -269,16 +277,6 @@
         if args is None:
             return
 
-    # create the Distribution class
-    # need to feed setup.cfg here !
-    dist = Distribution()
-
-    # Find and parse the config file(s): they will override options from
-    # the setup script, but be overridden by the command line.
-
-    # XXX still need to be extracted from Distribution
-    dist.parse_config_files()
-
     for cmd in dispatcher.commands:
         # FIXME need to catch MetadataMissingError here (from the check command
         # e.g.)--or catch any exception, print an error message and exit with 1
@@ -547,9 +545,8 @@
 
     def _show_help(self, parser, global_options_=True, display_options_=True,
                    commands=[]):
-        # late import because of mutual dependence between these modules
-        from distutils2.command.cmd import Command
-
+        # XXX want to print to stdout when help is requested (--help) but to
+        # stderr in case of error!
         print 'Usage: pysetup [options] action [action_options]'
         print
         if global_options_:
diff --git a/distutils2/tests/test_run.py b/distutils2/tests/test_run.py
--- a/distutils2/tests/test_run.py
+++ b/distutils2/tests/test_run.py
@@ -2,6 +2,7 @@
 
 import os
 import sys
+import textwrap
 from StringIO import StringIO
 
 from distutils2 import install
@@ -31,14 +32,7 @@
                   support.LoggingCatcher,
                   unittest.TestCase):
 
-    def setUp(self):
-        super(RunTestCase, self).setUp()
-        self.old_argv = sys.argv, sys.argv[:]
-
-    def tearDown(self):
-        sys.argv = self.old_argv[0]
-        sys.argv[:] = self.old_argv[1]
-        super(RunTestCase, self).tearDown()
+    maxDiff = None
 
     # TODO restore the tests removed six months ago and port them to pysetup
 
@@ -127,6 +121,38 @@
         self.assertEqual(err.splitlines(),
                          ["error: action 'invalid_action' not recognized"])
 
+    def test_setupcfg_parsing(self):
+        # #14733: pysetup used to parse setup.cfg too late
+        project_dir = self.mkdtemp()
+        os.chdir(project_dir)
+        custompy = textwrap.dedent(
+            """\
+            from distutils2.command.cmd import Command
+
+            class custom(Command):
+
+                user_options = []
+
+                def initialize_options(self):
+                    pass
+
+                def finalize_options(self):
+                    pass
+
+                def run(self):
+                    print 'custom: ok'
+            """)
+        setupcfg = textwrap.dedent(
+            """\
+            [global]
+            commands = custom.custom
+            """)
+        self.write_file('custom.py', custompy)
+        self.write_file('setup.cfg', setupcfg)
+
+        out, err = self.call_pysetup('run', 'custom')
+        self.assertEqual(out.splitlines(), ['custom: ok'])
+
 
 def test_suite():
     return unittest.makeSuite(RunTestCase)

-- 
Repository URL: http://hg.python.org/distutils2


More information about the Python-checkins mailing list