mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 05:31:20 +00:00 
			
		
		
		
	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.
This commit is contained in:
		
						commit
						db66310e8f
					
				
					 6 changed files with 274 additions and 14 deletions
				
			
		|  | @ -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 @@ def __init__(self, fixer_names, options=None, explicit=None): | |||
|             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 @@ def refactor_file(self, filename, write=False, doctests_only=False): | |||
|         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 @@ def refactor_stdin(self, doctests_only=False): | |||
|         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 traverse_by(self, fixers, traversal): | |||
|     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 @@ def processed_file(self, new_text, filename, old_text=None, write=False, | |||
|         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: | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Gregory P. Smith
						Gregory P. Smith