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