summaryrefslogtreecommitdiffstats
path: root/Lib/lib2to3/refactor.py
diff options
context:
space:
mode:
authorGregory P. Smith <greg@krypto.org>2012-02-12 23:50:21 (GMT)
committerGregory P. Smith <greg@krypto.org>2012-02-12 23:50:21 (GMT)
commit58f23ffb21b0fadb8df91c7c9735401f50b3e844 (patch)
treeef26a40799350fb1d788a1e05212810a1f5a4c5f /Lib/lib2to3/refactor.py
parent51d04d1ba8643d8a0c89d8307bf43697223f31e2 (diff)
downloadcpython-58f23ffb21b0fadb8df91c7c9735401f50b3e844.zip
cpython-58f23ffb21b0fadb8df91c7c9735401f50b3e844.tar.gz
cpython-58f23ffb21b0fadb8df91c7c9735401f50b3e844.tar.bz2
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
Diffstat (limited to 'Lib/lib2to3/refactor.py')
-rw-r--r--Lib/lib2to3/refactor.py20
1 files changed, 13 insertions, 7 deletions
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 FixerError(Exception):
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 @@ class RefactoringTool(object):
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 @@ class RefactoringTool(object):
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 @@ class RefactoringTool(object):
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 @@ class RefactoringTool(object):
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 @@ class RefactoringTool(object):
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: