main.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. """
  2. pasteurize: automatic conversion of Python 3 code to clean 2/3 code
  3. ===================================================================
  4. ``pasteurize`` attempts to convert existing Python 3 code into source-compatible
  5. Python 2 and 3 code.
  6. Use it like this on Python 3 code:
  7. $ pasteurize --verbose mypython3script.py
  8. This removes any Py3-only syntax (e.g. new metaclasses) and adds these
  9. import lines:
  10. from __future__ import absolute_import
  11. from __future__ import division
  12. from __future__ import print_function
  13. from __future__ import unicode_literals
  14. from future import standard_library
  15. standard_library.install_hooks()
  16. from builtins import *
  17. To write changes to the files, use the -w flag.
  18. It also adds any other wrappers needed for Py2/3 compatibility.
  19. Note that separate stages are not available (or needed) when converting from
  20. Python 3 with ``pasteurize`` as they are when converting from Python 2 with
  21. ``futurize``.
  22. The --all-imports option forces adding all ``__future__`` imports,
  23. ``builtins`` imports, and standard library aliases, even if they don't
  24. seem necessary for the current state of each module. (This can simplify
  25. testing, and can reduce the need to think about Py2 compatibility when editing
  26. the code further.)
  27. """
  28. from __future__ import (absolute_import, print_function, unicode_literals)
  29. import sys
  30. import logging
  31. import optparse
  32. from lib2to3.main import main, warn, StdoutRefactoringTool
  33. from lib2to3 import refactor
  34. from future import __version__
  35. from libpasteurize.fixes import fix_names
  36. def main(args=None):
  37. """Main program.
  38. Returns a suggested exit status (0, 1, 2).
  39. """
  40. # Set up option parser
  41. parser = optparse.OptionParser(usage="pasteurize [options] file|dir ...")
  42. parser.add_option("-V", "--version", action="store_true",
  43. help="Report the version number of pasteurize")
  44. parser.add_option("-a", "--all-imports", action="store_true",
  45. help="Adds all __future__ and future imports to each module")
  46. parser.add_option("-f", "--fix", action="append", default=[],
  47. help="Each FIX specifies a transformation; default: all")
  48. parser.add_option("-j", "--processes", action="store", default=1,
  49. type="int", help="Run 2to3 concurrently")
  50. parser.add_option("-x", "--nofix", action="append", default=[],
  51. help="Prevent a fixer from being run.")
  52. parser.add_option("-l", "--list-fixes", action="store_true",
  53. help="List available transformations")
  54. # parser.add_option("-p", "--print-function", action="store_true",
  55. # help="Modify the grammar so that print() is a function")
  56. parser.add_option("-v", "--verbose", action="store_true",
  57. help="More verbose logging")
  58. parser.add_option("--no-diffs", action="store_true",
  59. help="Don't show diffs of the refactoring")
  60. parser.add_option("-w", "--write", action="store_true",
  61. help="Write back modified files")
  62. parser.add_option("-n", "--nobackups", action="store_true", default=False,
  63. help="Don't write backups for modified files.")
  64. # Parse command line arguments
  65. refactor_stdin = False
  66. flags = {}
  67. options, args = parser.parse_args(args)
  68. fixer_pkg = 'libpasteurize.fixes'
  69. avail_fixes = fix_names
  70. flags["print_function"] = True
  71. if not options.write and options.no_diffs:
  72. warn("not writing files and not printing diffs; that's not very useful")
  73. if not options.write and options.nobackups:
  74. parser.error("Can't use -n without -w")
  75. if options.version:
  76. print(__version__)
  77. return 0
  78. if options.list_fixes:
  79. print("Available transformations for the -f/--fix option:")
  80. for fixname in sorted(avail_fixes):
  81. print(fixname)
  82. if not args:
  83. return 0
  84. if not args:
  85. print("At least one file or directory argument required.",
  86. file=sys.stderr)
  87. print("Use --help to show usage.", file=sys.stderr)
  88. return 2
  89. if "-" in args:
  90. refactor_stdin = True
  91. if options.write:
  92. print("Can't write to stdin.", file=sys.stderr)
  93. return 2
  94. # Set up logging handler
  95. level = logging.DEBUG if options.verbose else logging.INFO
  96. logging.basicConfig(format='%(name)s: %(message)s', level=level)
  97. unwanted_fixes = set()
  98. for fix in options.nofix:
  99. if ".fix_" in fix:
  100. unwanted_fixes.add(fix)
  101. else:
  102. # Infer the full module name for the fixer.
  103. # First ensure that no names clash (e.g.
  104. # lib2to3.fixes.fix_blah and libfuturize.fixes.fix_blah):
  105. found = [f for f in avail_fixes
  106. if f.endswith('fix_{0}'.format(fix))]
  107. if len(found) > 1:
  108. print("Ambiguous fixer name. Choose a fully qualified "
  109. "module name instead from these:\n" +
  110. "\n".join(" " + myf for myf in found),
  111. file=sys.stderr)
  112. return 2
  113. elif len(found) == 0:
  114. print("Unknown fixer. Use --list-fixes or -l for a list.",
  115. file=sys.stderr)
  116. return 2
  117. unwanted_fixes.add(found[0])
  118. extra_fixes = set()
  119. if options.all_imports:
  120. prefix = 'libpasteurize.fixes.'
  121. extra_fixes.add(prefix + 'fix_add_all__future__imports')
  122. extra_fixes.add(prefix + 'fix_add_future_standard_library_import')
  123. extra_fixes.add(prefix + 'fix_add_all_future_builtins')
  124. explicit = set()
  125. if options.fix:
  126. all_present = False
  127. for fix in options.fix:
  128. if fix == 'all':
  129. all_present = True
  130. else:
  131. if ".fix_" in fix:
  132. explicit.add(fix)
  133. else:
  134. # Infer the full module name for the fixer.
  135. # First ensure that no names clash (e.g.
  136. # lib2to3.fixes.fix_blah and libpasteurize.fixes.fix_blah):
  137. found = [f for f in avail_fixes
  138. if f.endswith('fix_{0}'.format(fix))]
  139. if len(found) > 1:
  140. print("Ambiguous fixer name. Choose a fully qualified "
  141. "module name instead from these:\n" +
  142. "\n".join(" " + myf for myf in found),
  143. file=sys.stderr)
  144. return 2
  145. elif len(found) == 0:
  146. print("Unknown fixer. Use --list-fixes or -l for a list.",
  147. file=sys.stderr)
  148. return 2
  149. explicit.add(found[0])
  150. if len(explicit & unwanted_fixes) > 0:
  151. print("Conflicting usage: the following fixers have been "
  152. "simultaneously requested and disallowed:\n" +
  153. "\n".join(" " + myf for myf in (explicit & unwanted_fixes)),
  154. file=sys.stderr)
  155. return 2
  156. requested = avail_fixes.union(explicit) if all_present else explicit
  157. else:
  158. requested = avail_fixes.union(explicit)
  159. fixer_names = requested | extra_fixes - unwanted_fixes
  160. # Initialize the refactoring tool
  161. rt = StdoutRefactoringTool(sorted(fixer_names), flags, set(),
  162. options.nobackups, not options.no_diffs)
  163. # Refactor all files and directories passed as arguments
  164. if not rt.errors:
  165. if refactor_stdin:
  166. rt.refactor_stdin()
  167. else:
  168. try:
  169. rt.refactor(args, options.write, None,
  170. options.processes)
  171. except refactor.MultiprocessingUnsupported:
  172. assert options.processes > 1
  173. print("Sorry, -j isn't " \
  174. "supported on this platform.", file=sys.stderr)
  175. return 1
  176. rt.summarize()
  177. # Return error status (0 if rt.errors is zero)
  178. return int(bool(rt.errors))