[Python-checkins] distutils2: Mixin2to3 moved from build_py to compat module.

tarek.ziade python-checkins at python.org
Thu Aug 19 08:34:14 CEST 2010


tarek.ziade pushed 80676c211465 to distutils2:

http://hg.python.org/distutils2/rev/80676c211465
changeset:   572:80676c211465
user:        Zubin Mithra <zubin.mithra at gmail.com>
date:        Fri Aug 13 18:22:51 2010 +0530
summary:     Mixin2to3 moved from build_py to compat module. test_Mixin2to3.test_additional_fixers issue resolved.
files:       src/distutils2/command/build_py.py, src/distutils2/compat.py, src/distutils2/tests/test_Mixin2to3.py, src/distutils2/util.py

diff --git a/src/distutils2/command/build_py.py b/src/distutils2/command/build_py.py
--- a/src/distutils2/command/build_py.py
+++ b/src/distutils2/command/build_py.py
@@ -13,61 +13,10 @@
 from distutils2.core import Command
 from distutils2.errors import DistutilsOptionError, DistutilsFileError
 from distutils2.util import convert_path
-from distutils2.converter.refactor import DistutilsRefactoringTool
+from distutils2.compat import Mixin2to3
 
 # marking public APIs
-__all__ = ['Mixin2to3', 'build_py']
-
-try:
-    from distutils2.util import Mixin2to3 as _Mixin2to3
-    from distutils2 import run_2to3_on_doctests
-    from lib2to3.refactor import get_fixers_from_package
-    _CONVERT = True
-    _KLASS = _Mixin2to3
-except ImportError:
-    _CONVERT = False
-    _KLASS = object
-
-class Mixin2to3(_KLASS):
-    """ The base class which can be used for refactoring. When run under
-    Python 3.0, the run_2to3 method provided by Mixin2to3 is overridden.
-    When run on Python 2.x, it merely creates a class which overrides run_2to3,
-    yet does nothing in particular with it.
-    """
-    if _CONVERT:
-        def _run_2to3(self, files, doctests=[], fixers=[]):
-            """ Takes a list of files and doctests, and performs conversion
-            on those.
-              - First, the files which contain the code(`files`) are converted.
-              - Second, the doctests in `files` are converted.
-              - Thirdly, the doctests in `doctests` are converted.
-            """
-            # if additional fixers are present, use them
-            if fixers:
-                self.fixer_names = fixers
-
-            # Convert the ".py" files.
-            logging.info("Converting Python code")
-            _KLASS.run_2to3(self, files)
-
-            # Convert the doctests in the ".py" files.
-            logging.info("Converting doctests with '.py' files")
-            _KLASS.run_2to3(self, files, doctests_only=True)
-
-            # If the following conditions are met, then convert:-
-            # 1. User has specified the 'convert_2to3_doctests' option. So, we
-            #    can expect that the list 'doctests' is not empty.
-            # 2. The default is allow distutils2 to allow conversion of text files
-            #    containing doctests. It is set as
-            #    distutils2.run_2to3_on_doctests
-
-            if doctests != [] and run_2to3_on_doctests:
-                logging.info("Converting text files which contain doctests")
-                _KLASS.run_2to3(self, doctests, doctests_only=True)
-    else:
-        # If run on Python 2.x, there is nothing to do.
-        def _run_2to3(self, files, doctests=[], fixers=[]):
-            pass
+__all__ = ['build_py']
 
 class build_py(Command, Mixin2to3):
 
diff --git a/src/distutils2/compat.py b/src/distutils2/compat.py
new file mode 100644
--- /dev/null
+++ b/src/distutils2/compat.py
@@ -0,0 +1,64 @@
+""" distutils2.compat
+
+Used to provide classes, variables and imports which can be used to
+support distutils2 across versions(2.x and 3.x)
+"""
+
+import logging
+
+
+try:
+    from distutils2.util import Mixin2to3 as _Mixin2to3
+    from distutils2 import run_2to3_on_doctests
+    from lib2to3.refactor import get_fixers_from_package
+    _CONVERT = True
+    _KLASS = _Mixin2to3
+except ImportError:
+    _CONVERT = False
+    _KLASS = object
+
+# marking public APIs
+__all__ = ['Mixin2to3']
+
+class Mixin2to3(_KLASS):
+    """ The base class which can be used for refactoring. When run under
+    Python 3.0, the run_2to3 method provided by Mixin2to3 is overridden.
+    When run on Python 2.x, it merely creates a class which overrides run_2to3,
+    yet does nothing in particular with it.
+    """
+    if _CONVERT:
+        def _run_2to3(self, files, doctests=[], fixers=[]):
+            """ Takes a list of files and doctests, and performs conversion
+            on those.
+              - First, the files which contain the code(`files`) are converted.
+              - Second, the doctests in `files` are converted.
+              - Thirdly, the doctests in `doctests` are converted.
+            """
+            # if additional fixers are present, use them
+            if fixers:
+                self.fixer_names = fixers
+
+            # Convert the ".py" files.
+            logging.info("Converting Python code")
+            _KLASS.run_2to3(self, files)
+
+            # Convert the doctests in the ".py" files.
+            logging.info("Converting doctests with '.py' files")
+            _KLASS.run_2to3(self, files, doctests_only=True)
+
+            # If the following conditions are met, then convert:-
+            # 1. User has specified the 'convert_2to3_doctests' option. So, we
+            #    can expect that the list 'doctests' is not empty.
+            # 2. The default is allow distutils2 to allow conversion of text files
+            #    containing doctests. It is set as
+            #    distutils2.run_2to3_on_doctests
+
+            if doctests != [] and run_2to3_on_doctests:
+                logging.info("Converting text files which contain doctests")
+                _KLASS.run_2to3(self, doctests, doctests_only=True)
+    else:
+        # If run on Python 2.x, there is nothing to do.
+        def _run_2to3(self, files, doctests=[], fixers=[]):
+            pass
+
+
diff --git a/src/distutils2/tests/test_Mixin2to3.py b/src/distutils2/tests/test_Mixin2to3.py
--- a/src/distutils2/tests/test_Mixin2to3.py
+++ b/src/distutils2/tests/test_Mixin2to3.py
@@ -1,10 +1,11 @@
 """Tests for distutils.command.build_py."""
 import sys
+import logging
 
 import distutils2
 from distutils2.tests import support
 from distutils2.tests.support import unittest
-from distutils2.command.build_py import Mixin2to3
+from distutils2.compat import Mixin2to3
 
 
 class Mixin2to3TestCase(support.TempdirManager, unittest.TestCase):
@@ -59,7 +60,8 @@
 
         mixin2to3 = Mixin2to3()
 
-        mixin2to3._run_2to3([code_name], None, ['distutils2.tests.fixer'])
+        mixin2to3._run_2to3(files=[code_name], 
+                            fixers=['distutils2.tests.fixer'])
         converted_code_content = "isinstance(x, T)"
         new_code_content = "".join(open(code_name).readlines())
         self.assertEquals(new_code_content, converted_code_content)
diff --git a/src/distutils2/util.py b/src/distutils2/util.py
--- a/src/distutils2/util.py
+++ b/src/distutils2/util.py
@@ -655,54 +655,6 @@
             raise ImportError
     return ret
 
-# 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)
-
-
 def splitext(path):
     """Like os.path.splitext, but take off .tar too"""
     base, ext = posixpath.splitext(path)
@@ -1117,3 +1069,51 @@
         data['obsoletes'] = meta['Obsoletes']
 
     return data
+
+# 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, RefactoringTool
+    fixers = [] 
+    fixers = get_fixers_from_package('lib2to3.fixes')
+    
+    
+    if fixer_names:
+        for fixername in fixer_names:
+            fixers.extend([fixer for fixer in get_fixers_from_package(fixername)])
+    r = RefactoringTool(fixers, 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)
\ No newline at end of file

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


More information about the Python-checkins mailing list