Issue #13930: Adds ability for 2to3 to write its output to a different
directory tree instead of overwriting the input files. Adds three command
line options: -o/--output-dir, -W/--write-unchanged-files and --add-suffix.
Feature backports into stable release branches for 2to3 are allowed by
a special exemption:
http://mail.python.org/pipermail/python-dev/2011-December/115089.html
diff --git a/Lib/lib2to3/refactor.py b/Lib/lib2to3/refactor.py
index ae5e40f..7cd034a 100644
--- a/Lib/lib2to3/refactor.py
+++ b/Lib/lib2to3/refactor.py
@@ -173,7 +173,8 @@
class RefactoringTool(object):
- _default_options = {"print_function" : False}
+ _default_options = {"print_function" : False,
+ "write_unchanged_files" : False}
CLASS_PREFIX = "Fix" # The prefix for fixer classes
FILE_PREFIX = "fix_" # The prefix for modules with a fixer within
@@ -195,6 +196,10 @@
self.grammar = pygram.python_grammar_no_print_statement
else:
self.grammar = pygram.python_grammar
+ # When this is True, the refactor*() methods will call write_file() for
+ # files processed even if they were not changed during refactoring. If
+ # and only if the refactor method's write parameter was True.
+ self.write_unchanged_files = self.options.get("write_unchanged_files")
self.errors = []
self.logger = logging.getLogger("RefactoringTool")
self.fixer_log = []
@@ -341,13 +346,13 @@
if doctests_only:
self.log_debug("Refactoring doctests in %s", filename)
output = self.refactor_docstring(input, filename)
- if output != input:
+ if self.write_unchanged_files or output != input:
self.processed_file(output, filename, input, write, encoding)
else:
self.log_debug("No doctest changes in %s", filename)
else:
tree = self.refactor_string(input, filename)
- if tree and tree.was_changed:
+ if self.write_unchanged_files or (tree and tree.was_changed):
# The [:-1] is to take off the \n we added earlier
self.processed_file(str(tree)[:-1], filename,
write=write, encoding=encoding)
@@ -386,13 +391,13 @@
if doctests_only:
self.log_debug("Refactoring doctests in stdin")
output = self.refactor_docstring(input, "<stdin>")
- if output != input:
+ if self.write_unchanged_files or output != input:
self.processed_file(output, "<stdin>", input)
else:
self.log_debug("No doctest changes in stdin")
else:
tree = self.refactor_string(input, "<stdin>")
- if tree and tree.was_changed:
+ if self.write_unchanged_files or (tree and tree.was_changed):
self.processed_file(str(tree), "<stdin>", input)
else:
self.log_debug("No changes in stdin")
@@ -502,7 +507,7 @@
def processed_file(self, new_text, filename, old_text=None, write=False,
encoding=None):
"""
- Called when a file has been refactored, and there are changes.
+ Called when a file has been refactored and there may be changes.
"""
self.files.append(filename)
if old_text is None:
@@ -513,7 +518,8 @@
self.print_output(old_text, new_text, filename, equal)
if equal:
self.log_debug("No changes to %s", filename)
- return
+ if not self.write_unchanged_files:
+ return
if write:
self.write_file(new_text, filename, old_text, encoding)
else: