diff options
| author | Zubin Mithra <zubin.mithra@gmail.com> | 2010-07-04 02:46:38 +0530 |
|---|---|---|
| committer | Zubin Mithra <zubin.mithra@gmail.com> | 2010-07-04 02:46:38 +0530 |
| commit | 91e424fad5db89c3977389a7b094c958cd9fd515 (patch) | |
| tree | 919112f889af522508f2f4203f091b68cfc08f06 | |
| parent | efc61559758a900d7c9a3a7ef7eae6ff26a8f705 (diff) | |
| download | disutils2-91e424fad5db89c3977389a7b094c958cd9fd515.tar.gz | |
[mq]: util.Mixin2to3, util.run_2to3 added(wrappers). tests added.
| -rw-r--r-- | src/distutils2/tests/test_util.py | 29 | ||||
| -rw-r--r-- | src/distutils2/util.py | 45 |
2 files changed, 74 insertions, 0 deletions
diff --git a/src/distutils2/tests/test_util.py b/src/distutils2/tests/test_util.py index 4fc120e..8e1d0bd 100644 --- a/src/distutils2/tests/test_util.py +++ b/src/distutils2/tests/test_util.py @@ -296,6 +296,35 @@ class UtilTestCase(support.EnvironGuard, 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 index 28f04b0..6e49b15 100644 --- a/src/distutils2/util.py +++ b/src/distutils2/util.py @@ -616,3 +616,48 @@ def find_packages(paths=('.',), exclude=()): 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) |
