[Python-checkins] distutils2: [mq]: util.Mixin2to3, util.run_2to3 added(wrappers). tests added.

tarek.ziade python-checkins at python.org
Sun Jul 4 11:16:45 CEST 2010


tarek.ziade pushed bf6057787636 to distutils2:

http://hg.python.org/distutils2/rev/bf6057787636
changeset:   253:bf6057787636
user:        Zubin Mithra <zubin.mithra at gmail.com>
date:        Sun Jul 04 02:46:38 2010 +0530
summary:     [mq]: util.Mixin2to3, util.run_2to3 added(wrappers). tests added.
files:       src/distutils2/tests/test_util.py, src/distutils2/util.py

diff --git a/src/distutils2/tests/test_util.py b/src/distutils2/tests/test_util.py
--- a/src/distutils2/tests/test_util.py
+++ b/src/distutils2/tests/test_util.py
@@ -296,6 +296,35 @@
         res = find_packages([root], ['pkg1.pkg2'])
         self.assertEqual(set(res), set(['pkg1', 'pkg5', 'pkg1.pkg3', 'pkg1.pkg3.pkg6']))
 
+    def test_run_2to3_on_code(self):
+        content = "print 'test'"
+        converted_content = "print('test')"
+        file_handle = tempfile.NamedTemporaryFile(delete=True)
+        file_name = file_handle.name
+        file_handle.write(content)
+        file_handle.flush()
+        file_handle.seek(0)
+        from distutils2.util import run_2to3
+        run_2to3([file_name])
+        new_content = "".join(file_handle.read())
+        file_handle.close()
+        self.assertEquals(new_content, converted_content)
+
+    def test_run_2to3_on_doctests(self):
+        # to check if text files containing doctests only get converted.
+        content = ">>> print 'test'\ntest\n"
+        converted_content = ">>> print('test')\ntest\n\n"
+        file_handle = tempfile.NamedTemporaryFile(delete=True)
+        file_name = file_handle.name
+        file_handle.write(content)
+        file_handle.flush()
+        file_handle.seek(0)
+        from distutils2.util import run_2to3
+        run_2to3([file_name], doctests_only=True)
+        new_content = "".join(file_handle.readlines())
+        file_handle.close()
+        self.assertEquals(new_content, converted_content)
+
 
 def test_suite():
     return unittest.makeSuite(UtilTestCase)
diff --git a/src/distutils2/util.py b/src/distutils2/util.py
--- a/src/distutils2/util.py
+++ b/src/distutils2/util.py
@@ -616,3 +616,48 @@
                 packages.append(package_name)
     return packages
 
+
+# utility functions for 2to3 support
+
+def run_2to3(files, doctests_only=False, fixer_names=None, options=None, 
+                                                            explicit=None):
+    """ Wrapper function around the refactor() class which
+	performs the conversions on a list of python files.
+	Invoke 2to3 on a list of Python files. The files should all come 
+	from the build area, as the modification is done in-place."""
+
+    if not files:
+        return
+
+    # Make this class local, to delay import of 2to3
+    from lib2to3.refactor import get_fixers_from_package
+    from distutils2.converter.refactor import DistutilsRefactoringTool
+
+    if fixer_names is None:
+        fixer_names = get_fixers_from_package('lib2to3.fixes')
+
+    r = DistutilsRefactoringTool(fixer_names, options=options)
+    if doctests_only:
+        r.refactor(files, doctests_only=True, write=True)
+    else:
+        r.refactor(files, write=True)
+
+class Mixin2to3:
+    """ Wrapper class for commands that run 2to3.
+    To configure 2to3, setup scripts may either change
+    the class variables, or inherit from this class
+    to override how 2to3 is invoked.
+    """
+    # provide list of fixers to run.
+    # defaults to all from lib2to3.fixers
+    fixer_names = None
+
+    # options dictionary
+    options = None
+
+    # list of fixers to invoke even though they are marked as explicit
+    explicit = None
+
+    def run_2to3(self, files, doctests_only=False):
+        """ Issues a call to util.run_2to3. """
+        return run_2to3(files, doctests_only, self.fixer_names, self.options, self.explicit)

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


More information about the Python-checkins mailing list