lib2to3_ex.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """
  2. Customized Mixin2to3 support:
  3. - adds support for converting doctests
  4. """
  5. import warnings
  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. from ._deprecation_warning import SetuptoolsDeprecationWarning
  11. class DistutilsRefactoringTool(RefactoringTool):
  12. def log_error(self, msg, *args, **kw):
  13. log.error(msg, *args)
  14. def log_message(self, msg, *args):
  15. log.info(msg, *args)
  16. def log_debug(self, msg, *args):
  17. log.debug(msg, *args)
  18. class Mixin2to3(_Mixin2to3):
  19. def run_2to3(self, files, doctests=False):
  20. # See of the distribution option has been set, otherwise check the
  21. # setuptools default.
  22. if self.distribution.use_2to3 is not True:
  23. return
  24. if not files:
  25. return
  26. warnings.warn(
  27. "2to3 support is deprecated. If the project still "
  28. "requires Python 2 support, please migrate to "
  29. "a single-codebase solution or employ an "
  30. "independent conversion process.",
  31. SetuptoolsDeprecationWarning)
  32. log.info("Fixing " + " ".join(files))
  33. self.__build_fixer_names()
  34. self.__exclude_fixers()
  35. if doctests:
  36. if setuptools.run_2to3_on_doctests:
  37. r = DistutilsRefactoringTool(self.fixer_names)
  38. r.refactor(files, write=True, doctests_only=True)
  39. else:
  40. _Mixin2to3.run_2to3(self, files)
  41. def __build_fixer_names(self):
  42. if self.fixer_names:
  43. return
  44. self.fixer_names = []
  45. for p in setuptools.lib2to3_fixer_packages:
  46. self.fixer_names.extend(get_fixers_from_package(p))
  47. if self.distribution.use_2to3_fixers is not None:
  48. for p in self.distribution.use_2to3_fixers:
  49. self.fixer_names.extend(get_fixers_from_package(p))
  50. def __exclude_fixers(self):
  51. excluded_fixers = getattr(self, 'exclude_fixers', [])
  52. if self.distribution.use_2to3_exclude_fixers is not None:
  53. excluded_fixers.extend(self.distribution.use_2to3_exclude_fixers)
  54. for fixer_name in excluded_fixers:
  55. if fixer_name in self.fixer_names:
  56. self.fixer_names.remove(fixer_name)