[Python-checkins] distutils2: started distutils2.converter - a lib2to3 based refactoring tool that cna be
tarek.ziade
python-checkins at python.org
Tue May 11 18:35:53 CEST 2010
tarek.ziade pushed 1d2e757f5a7a to distutils2:
http://hg.python.org/distutils2/rev/1d2e757f5a7a
changeset: 134:1d2e757f5a7a
tag: tip
user: Tarek Ziade <tarek at ziade.org>
date: Tue May 11 18:35:45 2010 +0200
summary: started distutils2.converter - a lib2to3 based refactoring tool that cna be used to refactor a distutils or setuptools project into a distutils2 one
files: src/distutils2/converter/__init__.py, src/distutils2/converter/fixers/__init__.py, src/distutils2/converter/fixers/fix_imports.py, src/distutils2/converter/refactor.py, src/distutils2/tests/test_converter.py
diff --git a/src/distutils2/converter/__init__.py b/src/distutils2/converter/__init__.py
new file mode 100644
--- /dev/null
+++ b/src/distutils2/converter/__init__.py
@@ -0,0 +1,8 @@
+"""distutils2.converter
+
+This package provide a refactoring tool to transform a
+setuptools or distutils project into a distutils2 one.
+"""
+
+from distutils2.converter.refactor import DistutilsRefactoringTool
+
diff --git a/src/distutils2/converter/fixers/__init__.py b/src/distutils2/converter/fixers/__init__.py
new file mode 100644
--- /dev/null
+++ b/src/distutils2/converter/fixers/__init__.py
@@ -0,0 +1,4 @@
+"""distutils2.converter.fixers
+
+Contains all fixers for the converter.
+"""
diff --git a/src/distutils2/converter/fixers/fix_imports.py b/src/distutils2/converter/fixers/fix_imports.py
new file mode 100644
--- /dev/null
+++ b/src/distutils2/converter/fixers/fix_imports.py
@@ -0,0 +1,29 @@
+"""distutils2.converter.fixers.fix_imports
+
+Fixer for import statements in setup.py
+"""
+from lib2to3.fixer_base import BaseFix
+from lib2to3.fixer_util import FromImport, syms, token
+
+class FixImports(BaseFix):
+ """Makes sure all import in setup.py are translated"""
+
+ PATTERN = """
+ import_from< 'from' imp=any 'import' ['('] any [')'] >
+ |
+ import_name< 'import' imp=any >
+ """
+
+ def transform(self, node, results):
+ imp = results['imp']
+ if node.type != syms.import_from:
+ return
+
+ while not hasattr(imp, 'value'):
+ imp = imp.children[0]
+
+ if imp.value == 'distutils':
+ imp.value = 'distutils2'
+ imp.changed()
+ return node
+
diff --git a/src/distutils2/converter/refactor.py b/src/distutils2/converter/refactor.py
new file mode 100644
--- /dev/null
+++ b/src/distutils2/converter/refactor.py
@@ -0,0 +1,17 @@
+"""distutils2.converter.refactor
+
+Provides DistutilsRefactoringTool, a class that register fixers used
+to refactor distutils or setuptools packages into distutils2 ones.
+"""
+from lib2to3.refactor import RefactoringTool
+
+_DISTUTILS_FIXERS = ['distutils2.converter.fixers.fix_imports']
+
+class DistutilsRefactoringTool(RefactoringTool):
+
+ def __init__(self, fixer_names=_DISTUTILS_FIXERS, options=None,
+ explicit=None):
+
+ super(DistutilsRefactoringTool, self).__init__(fixer_names, options,
+ explicit)
+
diff --git a/src/distutils2/tests/test_converter.py b/src/distutils2/tests/test_converter.py
new file mode 100644
--- /dev/null
+++ b/src/distutils2/tests/test_converter.py
@@ -0,0 +1,28 @@
+"""Tests for distutils.converter."""
+import unittest2
+from distutils2.converter import DistutilsRefactoringTool
+
+_ORIGINAL = """\
+from distutils.core import setup
+
+setup(name='Foo')
+"""
+
+_WANTED = """\
+from distutils2.core import setup
+
+setup(name='Foo')
+"""
+class ConverterTestCase(unittest2.TestCase):
+
+ def test_import(self):
+ # simplest case: renaming distutils import in setup.py
+ ref = DistutilsRefactoringTool()
+ res = ref.refactor_string(_ORIGINAL, 'setup.py')
+ self.assertEquals(str(res), _WANTED)
+
+def test_suite():
+ return unittest2.makeSuite(ConverterTestCase)
+
+if __name__ == '__main__':
+ run_unittest(test_suite())
--
Repository URL: http://hg.python.org/distutils2
More information about the Python-checkins
mailing list