Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 1 | """ |
| 2 | Main program for 2to3. |
| 3 | """ |
| 4 | |
| 5 | import sys |
| 6 | import os |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 7 | import difflib |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 8 | import logging |
Benjamin Peterson | 0b24b3d | 2008-12-16 03:57:54 +0000 | [diff] [blame] | 9 | import shutil |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 10 | import optparse |
| 11 | |
| 12 | from . import refactor |
| 13 | |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 14 | |
| 15 | def diff_texts(a, b, filename): |
| 16 | """Return a unified diff of two strings.""" |
| 17 | a = a.splitlines() |
| 18 | b = b.splitlines() |
| 19 | return difflib.unified_diff(a, b, filename, filename, |
| 20 | "(original)", "(refactored)", |
| 21 | lineterm="") |
| 22 | |
| 23 | |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 24 | class StdoutRefactoringTool(refactor.MultiprocessRefactoringTool): |
Benjamin Peterson | d61de7f | 2008-09-27 22:17:35 +0000 | [diff] [blame] | 25 | """ |
| 26 | Prints output to stdout. |
| 27 | """ |
| 28 | |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 29 | def __init__(self, fixers, options, explicit, nobackups, show_diffs): |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 30 | self.nobackups = nobackups |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 31 | self.show_diffs = show_diffs |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 32 | super(StdoutRefactoringTool, self).__init__(fixers, options, explicit) |
| 33 | |
Benjamin Peterson | d61de7f | 2008-09-27 22:17:35 +0000 | [diff] [blame] | 34 | def log_error(self, msg, *args, **kwargs): |
| 35 | self.errors.append((msg, args, kwargs)) |
| 36 | self.logger.error(msg, *args, **kwargs) |
| 37 | |
Benjamin Peterson | d481e3d | 2009-05-09 19:42:23 +0000 | [diff] [blame] | 38 | def write_file(self, new_text, filename, old_text, encoding): |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 39 | if not self.nobackups: |
| 40 | # Make backup |
| 41 | backup = filename + ".bak" |
| 42 | if os.path.lexists(backup): |
| 43 | try: |
| 44 | os.remove(backup) |
| 45 | except os.error as err: |
| 46 | self.log_message("Can't remove backup %s", backup) |
| 47 | try: |
| 48 | os.rename(filename, backup) |
| 49 | except os.error as err: |
| 50 | self.log_message("Can't rename %s to %s", filename, backup) |
| 51 | # Actually write the new file |
Benjamin Peterson | d481e3d | 2009-05-09 19:42:23 +0000 | [diff] [blame] | 52 | write = super(StdoutRefactoringTool, self).write_file |
| 53 | write(new_text, filename, old_text, encoding) |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 54 | if not self.nobackups: |
Benjamin Peterson | 8bcddca | 2009-01-03 16:53:14 +0000 | [diff] [blame] | 55 | shutil.copymode(backup, filename) |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 56 | |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 57 | def print_output(self, old, new, filename, equal): |
| 58 | if equal: |
| 59 | self.log_message("No changes to %s", filename) |
| 60 | else: |
| 61 | self.log_message("Refactored %s", filename) |
| 62 | if self.show_diffs: |
| 63 | for line in diff_texts(old, new, filename): |
| 64 | print(line) |
| 65 | |
| 66 | def warn(msg): |
Benjamin Peterson | 8fb00be | 2009-09-25 20:34:04 +0000 | [diff] [blame] | 67 | print("WARNING: %s" % (msg,), file=sys.stderr) |
Benjamin Peterson | d61de7f | 2008-09-27 22:17:35 +0000 | [diff] [blame] | 68 | |
| 69 | |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 70 | def main(fixer_pkg, args=None): |
| 71 | """Main program. |
| 72 | |
| 73 | Args: |
| 74 | fixer_pkg: the name of a package where the fixers are located. |
| 75 | args: optional; a list of command line arguments. If omitted, |
| 76 | sys.argv[1:] is used. |
| 77 | |
| 78 | Returns a suggested exit status (0, 1, 2). |
| 79 | """ |
| 80 | # Set up option parser |
Benjamin Peterson | 0b24b3d | 2008-12-16 03:57:54 +0000 | [diff] [blame] | 81 | parser = optparse.OptionParser(usage="2to3 [options] file|dir ...") |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 82 | parser.add_option("-d", "--doctests_only", action="store_true", |
| 83 | help="Fix up doctests only") |
| 84 | parser.add_option("-f", "--fix", action="append", default=[], |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 85 | help="Each FIX specifies a transformation; default: all") |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 86 | parser.add_option("-j", "--processes", action="store", default=1, |
| 87 | type="int", help="Run 2to3 concurrently") |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 88 | parser.add_option("-x", "--nofix", action="append", default=[], |
| 89 | help="Prevent a fixer from being run.") |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 90 | parser.add_option("-l", "--list-fixes", action="store_true", |
| 91 | help="List available transformations (fixes/fix_*.py)") |
| 92 | parser.add_option("-p", "--print-function", action="store_true", |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 93 | help="DEPRECATED Modify the grammar so that print() is " |
| 94 | "a function") |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 95 | parser.add_option("-v", "--verbose", action="store_true", |
| 96 | help="More verbose logging") |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 97 | parser.add_option("--no-diffs", action="store_true", |
| 98 | help="Don't show diffs of the refactoring") |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 99 | parser.add_option("-w", "--write", action="store_true", |
| 100 | help="Write back modified files") |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 101 | parser.add_option("-n", "--nobackups", action="store_true", default=False, |
| 102 | help="Don't write backups for modified files.") |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 103 | |
| 104 | # Parse command line arguments |
| 105 | refactor_stdin = False |
| 106 | options, args = parser.parse_args(args) |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 107 | if not options.write and options.no_diffs: |
| 108 | warn("not writing files and not printing diffs; that's not very useful") |
| 109 | if options.print_function: |
| 110 | warn("-p is deprecated; " |
| 111 | "detection of from __future__ import print_function is automatic") |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 112 | if not options.write and options.nobackups: |
| 113 | parser.error("Can't use -n without -w") |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 114 | if options.list_fixes: |
Benjamin Peterson | c0a40ab | 2008-09-11 21:05:25 +0000 | [diff] [blame] | 115 | print("Available transformations for the -f/--fix option:") |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 116 | for fixname in refactor.get_all_fix_names(fixer_pkg): |
Benjamin Peterson | c0a40ab | 2008-09-11 21:05:25 +0000 | [diff] [blame] | 117 | print(fixname) |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 118 | if not args: |
| 119 | return 0 |
| 120 | if not args: |
Benjamin Peterson | c0a40ab | 2008-09-11 21:05:25 +0000 | [diff] [blame] | 121 | print("At least one file or directory argument required.", file=sys.stderr) |
| 122 | print("Use --help to show usage.", file=sys.stderr) |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 123 | return 2 |
| 124 | if "-" in args: |
| 125 | refactor_stdin = True |
| 126 | if options.write: |
Benjamin Peterson | c0a40ab | 2008-09-11 21:05:25 +0000 | [diff] [blame] | 127 | print("Can't write to stdin.", file=sys.stderr) |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 128 | return 2 |
| 129 | |
| 130 | # Set up logging handler |
| 131 | level = logging.DEBUG if options.verbose else logging.INFO |
| 132 | logging.basicConfig(format='%(name)s: %(message)s', level=level) |
| 133 | |
| 134 | # Initialize the refactoring tool |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 135 | avail_fixes = set(refactor.get_fixers_from_package(fixer_pkg)) |
| 136 | unwanted_fixes = set(fixer_pkg + ".fix_" + fix for fix in options.nofix) |
| 137 | explicit = set() |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 138 | if options.fix: |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 139 | all_present = False |
| 140 | for fix in options.fix: |
| 141 | if fix == "all": |
| 142 | all_present = True |
| 143 | else: |
| 144 | explicit.add(fixer_pkg + ".fix_" + fix) |
| 145 | requested = avail_fixes.union(explicit) if all_present else explicit |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 146 | else: |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 147 | requested = avail_fixes.union(explicit) |
| 148 | fixer_names = requested.difference(unwanted_fixes) |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 149 | rt = StdoutRefactoringTool(sorted(fixer_names), None, sorted(explicit), |
| 150 | options.nobackups, not options.no_diffs) |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 151 | |
| 152 | # Refactor all files and directories passed as arguments |
| 153 | if not rt.errors: |
| 154 | if refactor_stdin: |
| 155 | rt.refactor_stdin() |
| 156 | else: |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 157 | try: |
| 158 | rt.refactor(args, options.write, options.doctests_only, |
| 159 | options.processes) |
| 160 | except refactor.MultiprocessingUnsupported: |
| 161 | assert options.processes > 1 |
Benjamin Peterson | 8fb00be | 2009-09-25 20:34:04 +0000 | [diff] [blame] | 162 | print("Sorry, -j isn't supported on this platform.", |
| 163 | file=sys.stderr) |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 164 | return 1 |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 165 | rt.summarize() |
| 166 | |
| 167 | # Return error status (0 if rt.errors is zero) |
| 168 | return int(bool(rt.errors)) |