You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lib2to3_ex.py 2.0 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """
  2. Customized Mixin2to3 support:
  3. - adds support for converting doctests
  4. This module raises an ImportError on Python 2.
  5. """
  6. from distutils.util import Mixin2to3 as _Mixin2to3
  7. from distutils import log
  8. from lib2to3.refactor import RefactoringTool, get_fixers_from_package
  9. import setuptools
  10. class DistutilsRefactoringTool(RefactoringTool):
  11. def log_error(self, msg, *args, **kw):
  12. log.error(msg, *args)
  13. def log_message(self, msg, *args):
  14. log.info(msg, *args)
  15. def log_debug(self, msg, *args):
  16. log.debug(msg, *args)
  17. class Mixin2to3(_Mixin2to3):
  18. def run_2to3(self, files, doctests = False):
  19. # See of the distribution option has been set, otherwise check the
  20. # setuptools default.
  21. if self.distribution.use_2to3 is not True:
  22. return
  23. if not files:
  24. return
  25. log.info("Fixing "+" ".join(files))
  26. self.__build_fixer_names()
  27. self.__exclude_fixers()
  28. if doctests:
  29. if setuptools.run_2to3_on_doctests:
  30. r = DistutilsRefactoringTool(self.fixer_names)
  31. r.refactor(files, write=True, doctests_only=True)
  32. else:
  33. _Mixin2to3.run_2to3(self, files)
  34. def __build_fixer_names(self):
  35. if self.fixer_names: return
  36. self.fixer_names = []
  37. for p in setuptools.lib2to3_fixer_packages:
  38. self.fixer_names.extend(get_fixers_from_package(p))
  39. if self.distribution.use_2to3_fixers is not None:
  40. for p in self.distribution.use_2to3_fixers:
  41. self.fixer_names.extend(get_fixers_from_package(p))
  42. def __exclude_fixers(self):
  43. excluded_fixers = getattr(self, 'exclude_fixers', [])
  44. if self.distribution.use_2to3_exclude_fixers is not None:
  45. excluded_fixers.extend(self.distribution.use_2to3_exclude_fixers)
  46. for fixer_name in excluded_fixers:
  47. if fixer_name in self.fixer_names:
  48. self.fixer_names.remove(fixer_name)