blob: 20ee434a1524684220d739c34c3132846a1b43e3 [file] [log] [blame]
Benjamin Peterson8951b612008-09-03 02:27:16 +00001"""
2Main program for 2to3.
3"""
4
Benjamin Peterson8d26b0b2010-05-07 19:10:11 +00005from __future__ import with_statement
6
Benjamin Peterson8951b612008-09-03 02:27:16 +00007import sys
8import os
Benjamin Peterson3059b002009-07-20 16:42:03 +00009import difflib
Benjamin Peterson8951b612008-09-03 02:27:16 +000010import logging
Benjamin Peterson0b24b3d2008-12-16 03:57:54 +000011import shutil
Benjamin Peterson8951b612008-09-03 02:27:16 +000012import optparse
13
14from . import refactor
15
Benjamin Peterson3059b002009-07-20 16:42:03 +000016
17def diff_texts(a, b, filename):
18 """Return a unified diff of two strings."""
19 a = a.splitlines()
20 b = b.splitlines()
21 return difflib.unified_diff(a, b, filename, filename,
22 "(original)", "(refactored)",
23 lineterm="")
24
25
Benjamin Peterson608d8bc2009-05-05 23:23:31 +000026class StdoutRefactoringTool(refactor.MultiprocessRefactoringTool):
Benjamin Petersond61de7f2008-09-27 22:17:35 +000027 """
28 Prints output to stdout.
29 """
30
Benjamin Peterson3059b002009-07-20 16:42:03 +000031 def __init__(self, fixers, options, explicit, nobackups, show_diffs):
Benjamin Peterson206e3072008-10-19 14:07:49 +000032 self.nobackups = nobackups
Benjamin Peterson3059b002009-07-20 16:42:03 +000033 self.show_diffs = show_diffs
Benjamin Peterson206e3072008-10-19 14:07:49 +000034 super(StdoutRefactoringTool, self).__init__(fixers, options, explicit)
35
Benjamin Petersond61de7f2008-09-27 22:17:35 +000036 def log_error(self, msg, *args, **kwargs):
37 self.errors.append((msg, args, kwargs))
38 self.logger.error(msg, *args, **kwargs)
39
Benjamin Petersond481e3d2009-05-09 19:42:23 +000040 def write_file(self, new_text, filename, old_text, encoding):
Benjamin Peterson206e3072008-10-19 14:07:49 +000041 if not self.nobackups:
42 # Make backup
43 backup = filename + ".bak"
44 if os.path.lexists(backup):
45 try:
46 os.remove(backup)
47 except os.error as err:
48 self.log_message("Can't remove backup %s", backup)
49 try:
50 os.rename(filename, backup)
51 except os.error as err:
52 self.log_message("Can't rename %s to %s", filename, backup)
53 # Actually write the new file
Benjamin Petersond481e3d2009-05-09 19:42:23 +000054 write = super(StdoutRefactoringTool, self).write_file
55 write(new_text, filename, old_text, encoding)
Benjamin Peterson92035012008-12-27 16:00:54 +000056 if not self.nobackups:
Benjamin Peterson8bcddca2009-01-03 16:53:14 +000057 shutil.copymode(backup, filename)
Benjamin Peterson206e3072008-10-19 14:07:49 +000058
Benjamin Peterson3059b002009-07-20 16:42:03 +000059 def print_output(self, old, new, filename, equal):
60 if equal:
61 self.log_message("No changes to %s", filename)
62 else:
63 self.log_message("Refactored %s", filename)
64 if self.show_diffs:
Benjamin Petersonffa94b02009-12-29 00:06:20 +000065 diff_lines = diff_texts(old, new, filename)
66 try:
Benjamin Peterson8d26b0b2010-05-07 19:10:11 +000067 if self.output_lock is not None:
68 with self.output_lock:
69 for line in diff_lines:
70 print(line)
71 sys.stdout.flush()
72 else:
73 for line in diff_lines:
74 print(line)
Benjamin Petersonffa94b02009-12-29 00:06:20 +000075 except UnicodeEncodeError:
76 warn("couldn't encode %s's diff for your terminal" %
77 (filename,))
78 return
Benjamin Peterson3059b002009-07-20 16:42:03 +000079
80def warn(msg):
Benjamin Peterson8fb00be2009-09-25 20:34:04 +000081 print("WARNING: %s" % (msg,), file=sys.stderr)
Benjamin Petersond61de7f2008-09-27 22:17:35 +000082
83
Benjamin Peterson8951b612008-09-03 02:27:16 +000084def main(fixer_pkg, args=None):
85 """Main program.
86
87 Args:
88 fixer_pkg: the name of a package where the fixers are located.
89 args: optional; a list of command line arguments. If omitted,
90 sys.argv[1:] is used.
91
92 Returns a suggested exit status (0, 1, 2).
93 """
94 # Set up option parser
Benjamin Peterson0b24b3d2008-12-16 03:57:54 +000095 parser = optparse.OptionParser(usage="2to3 [options] file|dir ...")
Benjamin Peterson8951b612008-09-03 02:27:16 +000096 parser.add_option("-d", "--doctests_only", action="store_true",
97 help="Fix up doctests only")
98 parser.add_option("-f", "--fix", action="append", default=[],
Benjamin Peterson206e3072008-10-19 14:07:49 +000099 help="Each FIX specifies a transformation; default: all")
Benjamin Peterson608d8bc2009-05-05 23:23:31 +0000100 parser.add_option("-j", "--processes", action="store", default=1,
101 type="int", help="Run 2to3 concurrently")
Benjamin Peterson206e3072008-10-19 14:07:49 +0000102 parser.add_option("-x", "--nofix", action="append", default=[],
Martin v. Löwise2bb4eb2010-12-03 23:11:07 +0000103 help="Prevent a transformation from being run")
Benjamin Peterson8951b612008-09-03 02:27:16 +0000104 parser.add_option("-l", "--list-fixes", action="store_true",
Benjamin Peterson8d26b0b2010-05-07 19:10:11 +0000105 help="List available transformations")
Benjamin Peterson8951b612008-09-03 02:27:16 +0000106 parser.add_option("-p", "--print-function", action="store_true",
Benjamin Peterson20211002009-11-25 18:34:42 +0000107 help="Modify the grammar so that print() is a function")
Benjamin Peterson8951b612008-09-03 02:27:16 +0000108 parser.add_option("-v", "--verbose", action="store_true",
109 help="More verbose logging")
Benjamin Peterson3059b002009-07-20 16:42:03 +0000110 parser.add_option("--no-diffs", action="store_true",
111 help="Don't show diffs of the refactoring")
Benjamin Peterson8951b612008-09-03 02:27:16 +0000112 parser.add_option("-w", "--write", action="store_true",
113 help="Write back modified files")
Benjamin Peterson206e3072008-10-19 14:07:49 +0000114 parser.add_option("-n", "--nobackups", action="store_true", default=False,
Martin v. Löwise2bb4eb2010-12-03 23:11:07 +0000115 help="Don't write backups for modified files")
Benjamin Peterson8951b612008-09-03 02:27:16 +0000116
117 # Parse command line arguments
118 refactor_stdin = False
Benjamin Peterson20211002009-11-25 18:34:42 +0000119 flags = {}
Benjamin Peterson8951b612008-09-03 02:27:16 +0000120 options, args = parser.parse_args(args)
Benjamin Peterson3059b002009-07-20 16:42:03 +0000121 if not options.write and options.no_diffs:
122 warn("not writing files and not printing diffs; that's not very useful")
Benjamin Peterson206e3072008-10-19 14:07:49 +0000123 if not options.write and options.nobackups:
124 parser.error("Can't use -n without -w")
Benjamin Peterson8951b612008-09-03 02:27:16 +0000125 if options.list_fixes:
Benjamin Petersonc0a40ab2008-09-11 21:05:25 +0000126 print("Available transformations for the -f/--fix option:")
Benjamin Peterson8951b612008-09-03 02:27:16 +0000127 for fixname in refactor.get_all_fix_names(fixer_pkg):
Benjamin Petersonc0a40ab2008-09-11 21:05:25 +0000128 print(fixname)
Benjamin Peterson8951b612008-09-03 02:27:16 +0000129 if not args:
130 return 0
131 if not args:
Benjamin Petersonc0a40ab2008-09-11 21:05:25 +0000132 print("At least one file or directory argument required.", file=sys.stderr)
133 print("Use --help to show usage.", file=sys.stderr)
Benjamin Peterson8951b612008-09-03 02:27:16 +0000134 return 2
135 if "-" in args:
136 refactor_stdin = True
137 if options.write:
Benjamin Petersonc0a40ab2008-09-11 21:05:25 +0000138 print("Can't write to stdin.", file=sys.stderr)
Benjamin Peterson8951b612008-09-03 02:27:16 +0000139 return 2
Benjamin Peterson20211002009-11-25 18:34:42 +0000140 if options.print_function:
141 flags["print_function"] = True
Benjamin Peterson8951b612008-09-03 02:27:16 +0000142
143 # Set up logging handler
144 level = logging.DEBUG if options.verbose else logging.INFO
145 logging.basicConfig(format='%(name)s: %(message)s', level=level)
146
147 # Initialize the refactoring tool
Benjamin Peterson206e3072008-10-19 14:07:49 +0000148 avail_fixes = set(refactor.get_fixers_from_package(fixer_pkg))
149 unwanted_fixes = set(fixer_pkg + ".fix_" + fix for fix in options.nofix)
150 explicit = set()
Benjamin Peterson8951b612008-09-03 02:27:16 +0000151 if options.fix:
Benjamin Peterson206e3072008-10-19 14:07:49 +0000152 all_present = False
153 for fix in options.fix:
154 if fix == "all":
155 all_present = True
156 else:
157 explicit.add(fixer_pkg + ".fix_" + fix)
158 requested = avail_fixes.union(explicit) if all_present else explicit
Benjamin Peterson8951b612008-09-03 02:27:16 +0000159 else:
Benjamin Peterson206e3072008-10-19 14:07:49 +0000160 requested = avail_fixes.union(explicit)
161 fixer_names = requested.difference(unwanted_fixes)
Benjamin Peterson20211002009-11-25 18:34:42 +0000162 rt = StdoutRefactoringTool(sorted(fixer_names), flags, sorted(explicit),
Benjamin Peterson3059b002009-07-20 16:42:03 +0000163 options.nobackups, not options.no_diffs)
Benjamin Peterson8951b612008-09-03 02:27:16 +0000164
165 # Refactor all files and directories passed as arguments
166 if not rt.errors:
167 if refactor_stdin:
168 rt.refactor_stdin()
169 else:
Benjamin Peterson608d8bc2009-05-05 23:23:31 +0000170 try:
171 rt.refactor(args, options.write, options.doctests_only,
172 options.processes)
173 except refactor.MultiprocessingUnsupported:
174 assert options.processes > 1
Benjamin Peterson8fb00be2009-09-25 20:34:04 +0000175 print("Sorry, -j isn't supported on this platform.",
176 file=sys.stderr)
Benjamin Peterson608d8bc2009-05-05 23:23:31 +0000177 return 1
Benjamin Peterson8951b612008-09-03 02:27:16 +0000178 rt.summarize()
179
180 # Return error status (0 if rt.errors is zero)
181 return int(bool(rt.errors))