[Python-checkins] distutils2: options moved to build.py; build_py.Mixin2to3 moved to _compat.Mixin2to3;

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


tarek.ziade pushed 60936af579fb to distutils2:

http://hg.python.org/distutils2/rev/60936af579fb
changeset:   568:60936af579fb
user:        Zubin Mithra <zubin.mithra at gmail.com>
date:        Thu Aug 12 23:15:51 2010 +0530
summary:     options moved to build.py; build_py.Mixin2to3 moved to _compat.Mixin2to3; DistutilsRefactoringTool removed, util.py changed accordingly
files:       src/distutils2/_compat.py, src/distutils2/command/build.py, src/distutils2/command/build_py.py, src/distutils2/command/build_scripts.py, src/distutils2/converter/refactor.py, src/distutils2/util.py

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,57 @@
+""" distutils2._compat
+
+Used to provide a layer of compatibility for support with Python2.x and Python3.x
+"""
+
+__all__ = ['Mixin2to3']
+
+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
diff --git a/src/distutils2/command/build.py b/src/distutils2/command/build.py
--- a/src/distutils2/command/build.py
+++ b/src/distutils2/command/build.py
@@ -43,6 +43,12 @@
          "forcibly build everything (ignore file timestamps)"),
         ('executable=', 'e',
          "specify final destination interpreter path (build.py)"),
+        ('use-2to3', None,
+          "use 2to3 to make source python 3.x compatible"),
+        ('convert-2to3-doctests', None,
+          "use 2to3 to convert doctests in seperate text files"),
+        ('use-2to3-fixers', None,
+          "list additional fixers opted for during 2to3 conversion"),
         ]
 
     boolean_options = ['debug', 'force']
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):
 
@@ -81,12 +30,6 @@
          "also compile with optimization: -O1 for \"python -O\", "
          "-O2 for \"python -OO\", and -O0 to disable [default: -O0]"),
         ('force', 'f', "forcibly build everything (ignore file timestamps)"),
-        ('use-2to3', None,
-         "use 2to3 to make source python 3.x compatible"),
-        ('convert-2to3-doctests', None,
-         "use 2to3 to convert doctests in seperate text files"),
-        ('use-2to3-fixers', None,
-         "list additional fixers opted for during 2to3 conversion"),
         ]
 
     boolean_options = ['compile', 'force']
@@ -108,7 +51,11 @@
         self.use_2to3_fixers = []
         
     def finalize_options(self):
-        self.set_undefined_options('build', 'build_lib', 'force')
+        self.set_undefined_options('build', 
+                                   ('build_py', 'use_2to3'),
+                                   ('build_py', 'convert_2to3_doctests')
+                                   ('build_py', 'use_2to3_fixers')
+                                    'build_py', 'force')
 
         # Get the distribution options that are aliases for build_py
         # options -- list of packages and list of modules.
diff --git a/src/distutils2/command/build_scripts.py b/src/distutils2/command/build_scripts.py
--- a/src/distutils2/command/build_scripts.py
+++ b/src/distutils2/command/build_scripts.py
@@ -13,11 +13,11 @@
     import sysconfig
 except ImportError:
     from distutils2._backport import sysconfig
-
+from distutils2._compat import Mixin2to3
 # check if Python is called on the first line with this expression
 first_line_re = re.compile('^#!.*python[0-9.]*([ \t].*)?$')
 
-class build_scripts (Command):
+class build_scripts (Command, Mixin2to3):
 
     description = "\"build\" scripts (copy and fixup #! line)"
 
@@ -36,10 +36,16 @@
         self.force = None
         self.executable = None
         self.outfiles = None
+        self.use_2to3 = False
+        self.convert_2to3_doctests = []
+        self.use_2to3_fixers = []
 
     def finalize_options (self):
         self.set_undefined_options('build',
                                    ('build_scripts', 'build_dir'),
+                                   ('build_scripts', 'use_2to3'),
+                                   ('build_scripts', 'convert_2to3_doctests')
+                                   ('build_scripts', 'use_2to3_fixers')
                                    'force', 'executable')
         self.scripts = self.distribution.scripts
 
@@ -49,8 +55,12 @@
     def run (self):
         if not self.scripts:
             return
-        self.copy_scripts()
+        _copied_files = []
+        _copied_files = self.copy_scripts()
 
+        if self.use_2to3 and _copied_files:
+            self.run_2to3(self._copied_files, None,
+                                            self.use_2to3_fixers)
 
     def copy_scripts (self):
         """Copy each script listed in 'self.scripts'; if it's marked as a
@@ -60,6 +70,7 @@
         """
         self.mkpath(self.build_dir)
         outfiles = []
+        copied_files = []
         for script in self.scripts:
             adjust = 0
             script = convert_path(script)
@@ -114,6 +125,7 @@
                 if f:
                     f.close()
                 self.copy_file(script, outfile)
+                copied_files.append(outfile)
 
         if os.name == 'posix':
             for file in outfiles:
@@ -126,7 +138,7 @@
                         log.info("changing mode of %s from %o to %o",
                                  file, oldmode, newmode)
                         os.chmod(file, newmode)
-
+        return copied_files
     # copy_scripts ()
 
 # class build_scripts
diff --git a/src/distutils2/converter/refactor.py b/src/distutils2/converter/refactor.py
--- a/src/distutils2/converter/refactor.py
+++ b/src/distutils2/converter/refactor.py
@@ -11,18 +11,4 @@
     _LIB2TO3 = False
 
 _DISTUTILS_FIXERS = ['distutils2.converter.fixers.fix_imports',
-                     'distutils2.converter.fixers.fix_setup_options']
-
-if _LIB2TO3:
-    class DistutilsRefactoringTool(RefactoringTool):
-
-        def __init__(self, fixer_names=_DISTUTILS_FIXERS, options=None,
-                    explicit=None):
-
-            super(DistutilsRefactoringTool, self).__init__(fixer_names, options,
-                                                            explicit)
-else:
-    class DistutilsRefactoringTool(object):
-        def __init__(self, *args, **kw):
-            raise NotImplementedError('Not available if run from Python < 2.6')
-
+                     'distutils2.converter.fixers.fix_setup_options']
\ No newline at end of file
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,49 @@
         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
+
+    if fixer_names is None:
+        fixer_names = get_fixers_from_package('lib2to3.fixes')
+
+    r = RefactoringTool(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)
\ No newline at end of file

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


More information about the Python-checkins mailing list