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