[Python-checkins] distutils2: implemented setup_hook= and commands= in [global]

tarek.ziade python-checkins at python.org
Mon Oct 11 17:31:31 CEST 2010


tarek.ziade pushed 4fa39d95b991 to distutils2:

http://hg.python.org/distutils2/rev/4fa39d95b991
changeset:   764:4fa39d95b991
tag:         tip
user:        Tarek Ziade <tarek at ziade.org>
date:        Mon Oct 11 17:31:15 2010 +0200
summary:     implemented setup_hook= and commands= in [global]
files:       distutils2/config.py, distutils2/core.py, distutils2/tests/test_config.py, distutils2/util.py

diff --git a/distutils2/config.py b/distutils2/config.py
--- a/distutils2/config.py
+++ b/distutils2/config.py
@@ -7,7 +7,7 @@
 from ConfigParser import RawConfigParser
 
 from distutils2 import log
-from distutils2.util import check_environ
+from distutils2.util import check_environ, resolve_name
 
 
 class Config(object):
@@ -17,12 +17,11 @@
         self.dist = dist
         self.setup_hook = None
 
-    def run_hook(self):
+    def run_hook(self, config):
         if self.setup_hook is None:
             return
-        # transform the hook name into a callable
-        # then run it !
-        # XXX
+        # the hook gets only the config
+        self.setup_hook(config)
 
     def find_config_files(self):
         """Find as many configuration files as should be processed for this
@@ -82,15 +81,37 @@
                         if v != '']
         return value
 
-    def _read_metadata(self, parser):
-        if parser.has_option('global', 'setup_hook'):
-            self.setup_hook = parser.get('global', 'setup_hook')
+    def _read_setup_cfg(self, parser):
+        content = {}
+        for section in parser.sections():
+            content[section] = dict(parser.items(section))
+
+        # global:setup_hook is called *first*
+        if 'global' in content:
+            if 'setup_hook' in content['global']:
+                setup_hook = content['global']['setup_hook']
+                self.setup_hook = resolve_name(setup_hook)
+                self.run_hook(content)
+
+            if 'commands' in content['global']:
+                commands = self._multiline(content['global']['commands'])
+                if isinstance(commands, str):
+                    commands = [commands]
+
+                for command in commands:
+                    command = command.split('=')
+                    if len(command) != 2:
+                        # Issue XXX a warning
+                        continue
+                    name, location = command[0].strip(), command[1].strip()
+                    self.dist.cmdclass[name] = resolve_name(location)
+
 
         metadata = self.dist.metadata
 
         # setting the metadata values
-        if 'metadata' in parser.sections():
-            for key, value in parser.items('metadata'):
+        if 'metadata' in content:
+            for key, value in content['metadata'].items():
                 key = key.replace('_', '-')
                 value = self._multiline(value)
                 if key == 'project-url':
@@ -99,9 +120,10 @@
                              [v.split(',') for v in value]]
                 if metadata.is_metadata_field(key):
                     metadata[key] = self._convert_metadata(key, value)
-        if 'files' in parser.sections():
+
+        if 'files' in content:
             files = dict([(key, self._multiline(value))
-                          for key, value in parser.items('files')])
+                          for key, value in content['files'].items()])
             self.dist.packages = []
             self.dist.package_dir = {}
 
@@ -154,7 +176,7 @@
             parser.read(filename)
 
             if os.path.split(filename)[-1] == 'setup.cfg':
-                self._read_metadata(parser)
+                self._read_setup_cfg(parser)
 
             for section in parser.sections():
                 options = parser.options(section)
diff --git a/distutils2/core.py b/distutils2/core.py
--- a/distutils2/core.py
+++ b/distutils2/core.py
@@ -136,9 +136,6 @@
     if _setup_stop_after == "commandline":
         return dist
 
-    # run the hook if we have one
-    dist.config.run_hook()
-
     # And finally, run all the commands found on the command line.
     if ok:
         try:
diff --git a/distutils2/tests/test_config.py b/distutils2/tests/test_config.py
--- a/distutils2/tests/test_config.py
+++ b/distutils2/tests/test_config.py
@@ -71,8 +71,25 @@
   include THANKS HACKING
   recursive-include examples *.txt *.py
   prune examples/sample?/build
+
+[global]
+commands =
+    foo = distutils2.tests.test_config.Foo
+
+setup_hook = distutils2.tests.test_config.hook
 """
 
+
+def hook(content):
+    content['metadata']['version'] += '.dev1'
+
+
+class Foo(object):
+    def run(self):
+        pass
+    finalize_options = initialize_options = run
+
+
 class ConfigTestCase(support.TempdirManager,
                      unittest.TestCase):
 
@@ -97,12 +114,14 @@
             sys.argv[:] = old_sys
 
         # sanity check
-        self.assertEqual(sys.stdout.getvalue(), '0.6.4' + os.linesep)
+        self.assertEqual(sys.stdout.getvalue(), '0.6.4.dev1' + os.linesep)
 
         # check what was done
         self.assertEqual(dist.metadata['Author'], 'Carl Meyer')
         self.assertEqual(dist.metadata['Author-Email'], 'carl at oddbird.net')
-        self.assertEqual(dist.metadata['Version'], '0.6.4')
+
+        # the hook adds .dev1
+        self.assertEqual(dist.metadata['Version'], '0.6.4.dev1')
 
         wanted = ['Development Status :: 4 - Beta',
                 'Environment :: Console (Text Based)',
@@ -141,6 +160,9 @@
              ('/etc/init.d ', ['init-script'])])
         self.assertEqual(dist.package_dir['two'], 'src')
 
+        # make sure we get the foo command loaded !
+        self.assertEquals(dist.cmdclass['foo'], Foo)
+
 
 def test_suite():
     return unittest.makeSuite(ConfigTestCase)
diff --git a/distutils2/util.py b/distutils2/util.py
--- a/distutils2/util.py
+++ b/distutils2/util.py
@@ -635,6 +635,7 @@
                 packages.append(package_name)
     return packages
 
+
 def resolve_name(name):
     """Resolve a name like ``module.object`` to an object and return it.
 

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


More information about the Python-checkins mailing list