diff options
author | Gregory P. Smith <greg@krypto.org> | 2012-02-12 23:56:49 (GMT) |
---|---|---|
committer | Gregory P. Smith <greg@krypto.org> | 2012-02-12 23:56:49 (GMT) |
commit | db66310e8f81415645ee27ebfa61043a078a2d3c (patch) | |
tree | df0d3506c44a24fc39101266a765165ab2ef01f1 /Lib/lib2to3/refactor.py | |
parent | c7551a16c7211301c51d15084894e0fed49fe086 (diff) | |
parent | 58f23ffb21b0fadb8df91c7c9735401f50b3e844 (diff) | |
download | cpython-db66310e8f81415645ee27ebfa61043a078a2d3c.zip cpython-db66310e8f81415645ee27ebfa61043a078a2d3c.tar.gz cpython-db66310e8f81415645ee27ebfa61043a078a2d3c.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.
Diffstat (limited to 'Lib/lib2to3/refactor.py')
-rw-r--r-- | Lib/lib2to3/refactor.py | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/Lib/lib2to3/refactor.py b/Lib/lib2to3/refactor.py index 3c06405..1e85810 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: |