[Python-checkins] cpython: Fix assorted bugs in packaging.util.cfg_to_args (#11595).

eric.araujo python-checkins at python.org
Sat Jun 11 20:01:43 CEST 2011


http://hg.python.org/cpython/rev/06670bd0e59e
changeset:   70790:06670bd0e59e
user:        Éric Araujo <merwok at netwok.org>
date:        Fri Jun 10 23:52:26 2011 +0200
summary:
  Fix assorted bugs in packaging.util.cfg_to_args (#11595).

Original patch by Erik Bray.

files:
  Lib/packaging/tests/test_util.py |  48 +++++++++++++++++--
  Lib/packaging/util.py            |  31 ++++++++----
  Misc/ACKS                        |   1 +
  Misc/NEWS                        |   4 +
  4 files changed, 67 insertions(+), 17 deletions(-)


diff --git a/Lib/packaging/tests/test_util.py b/Lib/packaging/tests/test_util.py
--- a/Lib/packaging/tests/test_util.py
+++ b/Lib/packaging/tests/test_util.py
@@ -8,16 +8,18 @@
 from io import StringIO
 
 from packaging.tests import support, unittest
+from packaging.tests.test_config import SETUP_CFG
 from packaging.errors import (
     PackagingPlatformError, PackagingByteCompileError, PackagingFileError,
     PackagingExecError, InstallationException)
 from packaging import util
+from packaging.dist import Distribution
 from packaging.util import (
     convert_path, change_root, split_quoted, strtobool, rfc822_escape,
     get_compiler_versions, _MAC_OS_X_LD_VERSION, byte_compile, find_packages,
     spawn, get_pypirc_path, generate_pypirc, read_pypirc, resolve_name, iglob,
     RICH_GLOB, egginfo_to_distinfo, is_setuptools, is_distutils, is_packaging,
-    get_install_method)
+    get_install_method, cfg_to_args)
 
 
 PYPIRC = """\
@@ -88,13 +90,15 @@
                    support.LoggingCatcher,
                    unittest.TestCase):
 
-    restore_environ = ['HOME']
+    restore_environ = ['HOME', 'PLAT']
 
     def setUp(self):
         super(UtilTestCase, self).setUp()
-        self.tmp_dir = self.mkdtemp()
-        self.rc = os.path.join(self.tmp_dir, '.pypirc')
-        os.environ['HOME'] = self.tmp_dir
+        self.addCleanup(os.chdir, os.getcwd())
+        tempdir = self.mkdtemp()
+        self.rc = os.path.join(tempdir, '.pypirc')
+        os.environ['HOME'] = tempdir
+        os.chdir(tempdir)
         # saving the environment
         self.name = os.name
         self.platform = sys.platform
@@ -103,7 +107,6 @@
         self.join = os.path.join
         self.isabs = os.path.isabs
         self.splitdrive = os.path.splitdrive
-        #self._config_vars = copy(sysconfig._config_vars)
 
         # patching os.uname
         if hasattr(os, 'uname'):
@@ -137,7 +140,6 @@
             os.uname = self.uname
         else:
             del os.uname
-        #sysconfig._config_vars = copy(self._config_vars)
         util.find_executable = self.old_find_executable
         subprocess.Popen = self.old_popen
         sys.old_stdout = self.old_stdout
@@ -491,6 +493,38 @@
             content = f.read()
         self.assertEqual(content, WANTED)
 
+    def test_cfg_to_args(self):
+        opts = {'description-file': 'README', 'extra-files': '',
+                'setup-hook': 'packaging.tests.test_config.hook'}
+        self.write_file('setup.cfg', SETUP_CFG % opts)
+        self.write_file('README', 'loooong description')
+
+        args = cfg_to_args()
+        # use Distribution to get the contents of the setup.cfg file
+        dist = Distribution()
+        dist.parse_config_files()
+        metadata = dist.metadata
+
+        self.assertEqual(args['name'], metadata['Name'])
+        # + .dev1 because the test SETUP_CFG also tests a hook function in
+        # test_config.py for appending to the version string
+        self.assertEqual(args['version'] + '.dev1', metadata['Version'])
+        self.assertEqual(args['author'], metadata['Author'])
+        self.assertEqual(args['author_email'], metadata['Author-Email'])
+        self.assertEqual(args['maintainer'], metadata['Maintainer'])
+        self.assertEqual(args['maintainer_email'],
+                         metadata['Maintainer-Email'])
+        self.assertEqual(args['description'], metadata['Summary'])
+        self.assertEqual(args['long_description'], metadata['Description'])
+        self.assertEqual(args['classifiers'], metadata['Classifier'])
+        self.assertEqual(args['requires'], metadata['Requires-Dist'])
+        self.assertEqual(args['provides'], metadata['Provides-Dist'])
+
+        self.assertEqual(args['package_dir'].get(''), dist.package_dir)
+        self.assertEqual(args['packages'], dist.packages)
+        self.assertEqual(args['scripts'], dist.scripts)
+        self.assertEqual(args['py_modules'], dist.py_modules)
+
 
 class GlobTestCaseBase(support.TempdirManager,
                        support.LoggingCatcher,
diff --git a/Lib/packaging/util.py b/Lib/packaging/util.py
--- a/Lib/packaging/util.py
+++ b/Lib/packaging/util.py
@@ -1015,16 +1015,20 @@
                         "requires": ("metadata", "requires_dist"),
                         "provides": ("metadata", "provides_dist"),  # **
                         "obsoletes": ("metadata", "obsoletes_dist"),  # **
+                        "package_dir": ("files", 'packages_root'),
                         "packages": ("files",),
                         "scripts": ("files",),
                         "py_modules": ("files", "modules"),  # **
                         }
 
     MULTI_FIELDS = ("classifiers",
+                    "platforms",
                     "requires",
-                    "platforms",
+                    "provides",
+                    "obsoletes",
                     "packages",
-                    "scripts")
+                    "scripts",
+                    "py_modules")
 
     def has_get_option(config, section, option):
         if config.has_option(section, option):
@@ -1036,9 +1040,9 @@
 
     # The real code starts here
     config = RawConfigParser()
-    if not os.path.exists(file):
+    if not os.path.exists(path):
         raise PackagingFileError("file '%s' does not exist" %
-                                 os.path.abspath(file))
+                                 os.path.abspath(path))
     config.read(path)
 
     kwargs = {}
@@ -1055,17 +1059,24 @@
         in_cfg_value = has_get_option(config, section, option)
         if not in_cfg_value:
             # There is no such option in the setup.cfg
-            if arg == "long_description":
-                filename = has_get_option(config, section, "description_file")
-                if filename:
-                    with open(filename) as fp:
-                        in_cfg_value = fp.read()
+            if arg == 'long_description':
+                filenames = has_get_option(config, section, 'description-file')
+                if filenames:
+                    filenames = split_multiline(filenames)
+                    in_cfg_value = []
+                    for filename in filenames:
+                        with open(filename) as fp:
+                            in_cfg_value.append(fp.read())
+                    in_cfg_value = '\n\n'.join(in_cfg_value)
             else:
                 continue
 
+        if arg == 'package_dir' and in_cfg_value:
+            in_cfg_value = {'': in_cfg_value}
+
         if arg in MULTI_FIELDS:
             # support multiline options
-            in_cfg_value = in_cfg_value.strip().split('\n')
+            in_cfg_value = split_multiline(in_cfg_value)
 
         kwargs[arg] = in_cfg_value
 
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -116,6 +116,7 @@
 Georg Brandl
 Christopher Brannon
 Terrence Brannon
+Erik Bray
 Brian Brazil
 Dave Brennan
 Tom Bridgman
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -187,6 +187,10 @@
 Library
 -------
 
+- Issue #11595: Fix assorted bugs in packaging.util.cfg_to_args, a
+  compatibility helper for the distutils-packaging transition.  Original patch
+  by Erik Bray.
+
 - Issue #12246: Warn and fail when trying to install a third-party project from
   an uninstalled Python (built in a source checkout).  Original patch by
   Tshepang Lekhonkhobe.

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


More information about the Python-checkins mailing list