Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python2.5 |
| 2 | # Copyright 2006 Google, Inc. All Rights Reserved. |
| 3 | # Licensed to PSF under a Contributor Agreement. |
| 4 | |
| 5 | """Refactoring framework. |
| 6 | |
| 7 | Used as a main program, this can refactor any number of files and/or |
| 8 | recursively descend down directories. Imported as a module, this |
| 9 | provides infrastructure to write your own refactoring tool. |
| 10 | """ |
| 11 | |
| 12 | __author__ = "Guido van Rossum <guido@python.org>" |
| 13 | |
| 14 | |
| 15 | # Python imports |
| 16 | import os |
| 17 | import sys |
| 18 | import difflib |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 19 | import logging |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 20 | import operator |
Martin v. Löwis | 6780a9d | 2008-05-02 21:30:20 +0000 | [diff] [blame] | 21 | from collections import defaultdict |
| 22 | from itertools import chain |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 23 | |
| 24 | # Local imports |
| 25 | from .pgen2 import driver |
| 26 | from .pgen2 import tokenize |
| 27 | |
| 28 | from . import pytree |
| 29 | from . import patcomp |
| 30 | from . import fixes |
| 31 | from . import pygram |
| 32 | |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 33 | |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 34 | def get_all_fix_names(fixer_pkg, remove_prefix=True): |
| 35 | """Return a sorted list of all available fix names in the given package.""" |
| 36 | pkg = __import__(fixer_pkg, [], [], ["*"]) |
| 37 | fixer_dir = os.path.dirname(pkg.__file__) |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 38 | fix_names = [] |
Benjamin Peterson | e607823 | 2008-06-15 02:31:05 +0000 | [diff] [blame] | 39 | names = os.listdir(fixer_dir) |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 40 | names.sort() |
| 41 | for name in names: |
| 42 | if name.startswith("fix_") and name.endswith(".py"): |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 43 | if remove_prefix: |
| 44 | name = name[4:] |
| 45 | fix_names.append(name[:-3]) |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 46 | return fix_names |
| 47 | |
Martin v. Löwis | 6780a9d | 2008-05-02 21:30:20 +0000 | [diff] [blame] | 48 | def get_head_types(pat): |
| 49 | """ Accepts a pytree Pattern Node and returns a set |
| 50 | of the pattern types which will match first. """ |
| 51 | |
| 52 | if isinstance(pat, (pytree.NodePattern, pytree.LeafPattern)): |
| 53 | # NodePatters must either have no type and no content |
| 54 | # or a type and content -- so they don't get any farther |
| 55 | # Always return leafs |
| 56 | return set([pat.type]) |
| 57 | |
| 58 | if isinstance(pat, pytree.NegatedPattern): |
| 59 | if pat.content: |
| 60 | return get_head_types(pat.content) |
| 61 | return set([None]) # Negated Patterns don't have a type |
| 62 | |
| 63 | if isinstance(pat, pytree.WildcardPattern): |
| 64 | # Recurse on each node in content |
| 65 | r = set() |
| 66 | for p in pat.content: |
| 67 | for x in p: |
| 68 | r.update(get_head_types(x)) |
| 69 | return r |
| 70 | |
| 71 | raise Exception("Oh no! I don't understand pattern %s" %(pat)) |
| 72 | |
| 73 | def get_headnode_dict(fixer_list): |
| 74 | """ Accepts a list of fixers and returns a dictionary |
| 75 | of head node type --> fixer list. """ |
| 76 | head_nodes = defaultdict(list) |
| 77 | for fixer in fixer_list: |
| 78 | if not fixer.pattern: |
| 79 | head_nodes[None].append(fixer) |
| 80 | continue |
| 81 | for t in get_head_types(fixer.pattern): |
| 82 | head_nodes[t].append(fixer) |
| 83 | return head_nodes |
| 84 | |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 85 | def get_fixers_from_package(pkg_name): |
| 86 | """ |
| 87 | Return the fully qualified names for fixers in the package pkg_name. |
| 88 | """ |
| 89 | return [pkg_name + "." + fix_name |
| 90 | for fix_name in get_all_fix_names(pkg_name, False)] |
| 91 | |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 92 | |
Benjamin Peterson | 08be291 | 2008-09-27 21:09:10 +0000 | [diff] [blame] | 93 | class FixerError(Exception): |
| 94 | """A fixer could not be loaded.""" |
| 95 | |
| 96 | |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 97 | class RefactoringTool(object): |
| 98 | |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 99 | _default_options = {"print_function": False} |
| 100 | |
Benjamin Peterson | 08be291 | 2008-09-27 21:09:10 +0000 | [diff] [blame] | 101 | CLASS_PREFIX = "Fix" # The prefix for fixer classes |
| 102 | FILE_PREFIX = "fix_" # The prefix for modules with a fixer within |
| 103 | |
| 104 | def __init__(self, fixer_names, options=None, explicit=None): |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 105 | """Initializer. |
| 106 | |
Benjamin Peterson | e607823 | 2008-06-15 02:31:05 +0000 | [diff] [blame] | 107 | Args: |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 108 | fixer_names: a list of fixers to import |
| 109 | options: an dict with configuration. |
| 110 | explicit: a list of fixers to run even if they are explicit. |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 111 | """ |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 112 | self.fixers = fixer_names |
Benjamin Peterson | 08be291 | 2008-09-27 21:09:10 +0000 | [diff] [blame] | 113 | self.explicit = explicit or [] |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 114 | self.options = self._default_options.copy() |
| 115 | if options is not None: |
| 116 | self.options.update(options) |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 117 | self.errors = [] |
| 118 | self.logger = logging.getLogger("RefactoringTool") |
| 119 | self.fixer_log = [] |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 120 | self.wrote = False |
| 121 | if self.options["print_function"]: |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 122 | del pygram.python_grammar.keywords["print"] |
| 123 | self.driver = driver.Driver(pygram.python_grammar, |
| 124 | convert=pytree.convert, |
| 125 | logger=self.logger) |
| 126 | self.pre_order, self.post_order = self.get_fixers() |
Martin v. Löwis | 6780a9d | 2008-05-02 21:30:20 +0000 | [diff] [blame] | 127 | |
| 128 | self.pre_order = get_headnode_dict(self.pre_order) |
| 129 | self.post_order = get_headnode_dict(self.post_order) |
| 130 | |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 131 | self.files = [] # List of files that were or should be modified |
| 132 | |
| 133 | def get_fixers(self): |
| 134 | """Inspects the options to load the requested patterns and handlers. |
Martin v. Löwis | ab41b37 | 2008-03-19 05:22:42 +0000 | [diff] [blame] | 135 | |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 136 | Returns: |
| 137 | (pre_order, post_order), where pre_order is the list of fixers that |
| 138 | want a pre-order AST traversal, and post_order is the list that want |
| 139 | post-order traversal. |
| 140 | """ |
| 141 | pre_order_fixers = [] |
| 142 | post_order_fixers = [] |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 143 | for fix_mod_path in self.fixers: |
Benjamin Peterson | 08be291 | 2008-09-27 21:09:10 +0000 | [diff] [blame] | 144 | mod = __import__(fix_mod_path, {}, {}, ["*"]) |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 145 | fix_name = fix_mod_path.rsplit(".", 1)[-1] |
Benjamin Peterson | 08be291 | 2008-09-27 21:09:10 +0000 | [diff] [blame] | 146 | if fix_name.startswith(self.FILE_PREFIX): |
| 147 | fix_name = fix_name[len(self.FILE_PREFIX):] |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 148 | parts = fix_name.split("_") |
Benjamin Peterson | 08be291 | 2008-09-27 21:09:10 +0000 | [diff] [blame] | 149 | class_name = self.CLASS_PREFIX + "".join([p.title() for p in parts]) |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 150 | try: |
| 151 | fix_class = getattr(mod, class_name) |
| 152 | except AttributeError: |
Benjamin Peterson | 08be291 | 2008-09-27 21:09:10 +0000 | [diff] [blame] | 153 | raise FixerError("Can't find %s.%s" % (fix_name, class_name)) |
| 154 | fixer = fix_class(self.options, self.fixer_log) |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 155 | if fixer.explicit and self.explicit is not True and \ |
| 156 | fix_mod_path not in self.explicit: |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 157 | self.log_message("Skipping implicit fixer: %s", fix_name) |
| 158 | continue |
| 159 | |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 160 | self.log_debug("Adding transformation: %s", fix_name) |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 161 | if fixer.order == "pre": |
| 162 | pre_order_fixers.append(fixer) |
| 163 | elif fixer.order == "post": |
| 164 | post_order_fixers.append(fixer) |
| 165 | else: |
Benjamin Peterson | 08be291 | 2008-09-27 21:09:10 +0000 | [diff] [blame] | 166 | raise FixerError("Illegal fixer order: %r" % fixer.order) |
Martin v. Löwis | baf267c | 2008-03-22 00:01:12 +0000 | [diff] [blame] | 167 | |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 168 | key_func = operator.attrgetter("run_order") |
| 169 | pre_order_fixers.sort(key=key_func) |
| 170 | post_order_fixers.sort(key=key_func) |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 171 | return (pre_order_fixers, post_order_fixers) |
| 172 | |
| 173 | def log_error(self, msg, *args, **kwds): |
Benjamin Peterson | 08be291 | 2008-09-27 21:09:10 +0000 | [diff] [blame] | 174 | """Called when an error occurs.""" |
| 175 | raise |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 176 | |
| 177 | def log_message(self, msg, *args): |
| 178 | """Hook to log a message.""" |
| 179 | if args: |
| 180 | msg = msg % args |
| 181 | self.logger.info(msg) |
| 182 | |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 183 | def log_debug(self, msg, *args): |
| 184 | if args: |
| 185 | msg = msg % args |
| 186 | self.logger.debug(msg) |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 187 | |
Benjamin Peterson | 08be291 | 2008-09-27 21:09:10 +0000 | [diff] [blame] | 188 | def print_output(self, lines): |
| 189 | """Called with lines of output to give to the user.""" |
| 190 | pass |
| 191 | |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 192 | def refactor(self, items, write=False, doctests_only=False): |
| 193 | """Refactor a list of files and directories.""" |
| 194 | for dir_or_file in items: |
| 195 | if os.path.isdir(dir_or_file): |
Benjamin Peterson | 08be291 | 2008-09-27 21:09:10 +0000 | [diff] [blame] | 196 | self.refactor_dir(dir_or_file, write, doctests_only) |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 197 | else: |
Benjamin Peterson | 08be291 | 2008-09-27 21:09:10 +0000 | [diff] [blame] | 198 | self.refactor_file(dir_or_file, write, doctests_only) |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 199 | |
| 200 | def refactor_dir(self, dir_name, write=False, doctests_only=False): |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 201 | """Descends down a directory and refactor every Python file found. |
| 202 | |
| 203 | Python files are assumed to have a .py extension. |
| 204 | |
| 205 | Files and subdirectories starting with '.' are skipped. |
| 206 | """ |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 207 | for dirpath, dirnames, filenames in os.walk(dir_name): |
| 208 | self.log_debug("Descending into %s", dirpath) |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 209 | dirnames.sort() |
| 210 | filenames.sort() |
| 211 | for name in filenames: |
| 212 | if not name.startswith(".") and name.endswith("py"): |
| 213 | fullname = os.path.join(dirpath, name) |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 214 | self.refactor_file(fullname, write, doctests_only) |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 215 | # Modify dirnames in-place to remove subdirs with leading dots |
| 216 | dirnames[:] = [dn for dn in dirnames if not dn.startswith(".")] |
| 217 | |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 218 | def refactor_file(self, filename, write=False, doctests_only=False): |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 219 | """Refactors a file.""" |
| 220 | try: |
| 221 | f = open(filename) |
| 222 | except IOError, err: |
| 223 | self.log_error("Can't open %s: %s", filename, err) |
| 224 | return |
| 225 | try: |
| 226 | input = f.read() + "\n" # Silence certain parse errors |
| 227 | finally: |
| 228 | f.close() |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 229 | if doctests_only: |
| 230 | self.log_debug("Refactoring doctests in %s", filename) |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 231 | output = self.refactor_docstring(input, filename) |
| 232 | if output != input: |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 233 | self.processed_file(output, filename, input, write=write) |
| 234 | else: |
| 235 | self.log_debug("No doctest changes in %s", filename) |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 236 | else: |
| 237 | tree = self.refactor_string(input, filename) |
| 238 | if tree and tree.was_changed: |
| 239 | # The [:-1] is to take off the \n we added earlier |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 240 | self.processed_file(str(tree)[:-1], filename, write=write) |
| 241 | else: |
| 242 | self.log_debug("No changes in %s", filename) |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 243 | |
| 244 | def refactor_string(self, data, name): |
| 245 | """Refactor a given input string. |
Martin v. Löwis | ab41b37 | 2008-03-19 05:22:42 +0000 | [diff] [blame] | 246 | |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 247 | Args: |
| 248 | data: a string holding the code to be refactored. |
| 249 | name: a human-readable name for use in error/log messages. |
Martin v. Löwis | ab41b37 | 2008-03-19 05:22:42 +0000 | [diff] [blame] | 250 | |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 251 | Returns: |
| 252 | An AST corresponding to the refactored input stream; None if |
| 253 | there were errors during the parse. |
| 254 | """ |
| 255 | try: |
| 256 | tree = self.driver.parse_string(data,1) |
| 257 | except Exception, err: |
| 258 | self.log_error("Can't parse %s: %s: %s", |
| 259 | name, err.__class__.__name__, err) |
| 260 | return |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 261 | self.log_debug("Refactoring %s", name) |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 262 | self.refactor_tree(tree, name) |
| 263 | return tree |
| 264 | |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 265 | def refactor_stdin(self, doctests_only=False): |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 266 | input = sys.stdin.read() |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 267 | if doctests_only: |
| 268 | self.log_debug("Refactoring doctests in stdin") |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 269 | output = self.refactor_docstring(input, "<stdin>") |
| 270 | if output != input: |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 271 | self.processed_file(output, "<stdin>", input) |
| 272 | else: |
| 273 | self.log_debug("No doctest changes in stdin") |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 274 | else: |
| 275 | tree = self.refactor_string(input, "<stdin>") |
| 276 | if tree and tree.was_changed: |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 277 | self.processed_file(str(tree), "<stdin>", input) |
| 278 | else: |
| 279 | self.log_debug("No changes in stdin") |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 280 | |
| 281 | def refactor_tree(self, tree, name): |
| 282 | """Refactors a parse tree (modifying the tree in place). |
Martin v. Löwis | ab41b37 | 2008-03-19 05:22:42 +0000 | [diff] [blame] | 283 | |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 284 | Args: |
| 285 | tree: a pytree.Node instance representing the root of the tree |
| 286 | to be refactored. |
| 287 | name: a human-readable name for this tree. |
Martin v. Löwis | ab41b37 | 2008-03-19 05:22:42 +0000 | [diff] [blame] | 288 | |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 289 | Returns: |
| 290 | True if the tree was modified, False otherwise. |
| 291 | """ |
Martin v. Löwis | 6780a9d | 2008-05-02 21:30:20 +0000 | [diff] [blame] | 292 | # Two calls to chain are required because pre_order.values() |
| 293 | # will be a list of lists of fixers: |
| 294 | # [[<fixer ...>, <fixer ...>], [<fixer ...>]] |
| 295 | all_fixers = chain(chain(*self.pre_order.values()),\ |
| 296 | chain(*self.post_order.values())) |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 297 | for fixer in all_fixers: |
| 298 | fixer.start_tree(tree, name) |
| 299 | |
| 300 | self.traverse_by(self.pre_order, tree.pre_order()) |
| 301 | self.traverse_by(self.post_order, tree.post_order()) |
| 302 | |
| 303 | for fixer in all_fixers: |
| 304 | fixer.finish_tree(tree, name) |
| 305 | return tree.was_changed |
| 306 | |
| 307 | def traverse_by(self, fixers, traversal): |
| 308 | """Traverse an AST, applying a set of fixers to each node. |
Martin v. Löwis | ab41b37 | 2008-03-19 05:22:42 +0000 | [diff] [blame] | 309 | |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 310 | This is a helper method for refactor_tree(). |
Martin v. Löwis | ab41b37 | 2008-03-19 05:22:42 +0000 | [diff] [blame] | 311 | |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 312 | Args: |
| 313 | fixers: a list of fixer instances. |
| 314 | traversal: a generator that yields AST nodes. |
Martin v. Löwis | ab41b37 | 2008-03-19 05:22:42 +0000 | [diff] [blame] | 315 | |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 316 | Returns: |
| 317 | None |
| 318 | """ |
| 319 | if not fixers: |
| 320 | return |
| 321 | for node in traversal: |
Martin v. Löwis | 6780a9d | 2008-05-02 21:30:20 +0000 | [diff] [blame] | 322 | for fixer in fixers[node.type] + fixers[None]: |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 323 | results = fixer.match(node) |
| 324 | if results: |
| 325 | new = fixer.transform(node, results) |
| 326 | if new is not None and (new != node or |
| 327 | str(new) != str(node)): |
| 328 | node.replace(new) |
| 329 | node = new |
| 330 | |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 331 | def processed_file(self, new_text, filename, old_text=None, write=False): |
| 332 | """ |
| 333 | Called when a file has been refactored, and there are changes. |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 334 | """ |
| 335 | self.files.append(filename) |
| 336 | if old_text is None: |
| 337 | try: |
| 338 | f = open(filename, "r") |
| 339 | except IOError, err: |
| 340 | self.log_error("Can't read %s: %s", filename, err) |
| 341 | return |
| 342 | try: |
| 343 | old_text = f.read() |
| 344 | finally: |
| 345 | f.close() |
| 346 | if old_text == new_text: |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 347 | self.log_debug("No changes to %s", filename) |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 348 | return |
Benjamin Peterson | 08be291 | 2008-09-27 21:09:10 +0000 | [diff] [blame] | 349 | self.print_output(diff_texts(old_text, new_text, filename)) |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 350 | if write: |
Benjamin Peterson | 0151b53 | 2008-09-03 02:14:03 +0000 | [diff] [blame] | 351 | self.write_file(new_text, filename, old_text) |
Benjamin Peterson | 08be291 | 2008-09-27 21:09:10 +0000 | [diff] [blame] | 352 | else: |
| 353 | self.log_debug("Not writing changes to %s", filename) |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 354 | |
| 355 | def write_file(self, new_text, filename, old_text=None): |
| 356 | """Writes a string to a file. |
| 357 | |
| 358 | It first shows a unified diff between the old text and the new text, and |
| 359 | then rewrites the file; the latter is only done if the write option is |
| 360 | set. |
| 361 | """ |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 362 | backup = filename + ".bak" |
| 363 | if os.path.lexists(backup): |
| 364 | try: |
| 365 | os.remove(backup) |
| 366 | except os.error, err: |
| 367 | self.log_message("Can't remove backup %s", backup) |
| 368 | try: |
| 369 | os.rename(filename, backup) |
| 370 | except os.error, err: |
| 371 | self.log_message("Can't rename %s to %s", filename, backup) |
| 372 | try: |
| 373 | f = open(filename, "w") |
| 374 | except os.error, err: |
| 375 | self.log_error("Can't create %s: %s", filename, err) |
| 376 | return |
| 377 | try: |
| 378 | try: |
| 379 | f.write(new_text) |
| 380 | except os.error, err: |
| 381 | self.log_error("Can't write %s: %s", filename, err) |
| 382 | finally: |
| 383 | f.close() |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 384 | self.log_debug("Wrote changes to %s", filename) |
| 385 | self.wrote = True |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 386 | |
| 387 | PS1 = ">>> " |
| 388 | PS2 = "... " |
| 389 | |
| 390 | def refactor_docstring(self, input, filename): |
| 391 | """Refactors a docstring, looking for doctests. |
| 392 | |
| 393 | This returns a modified version of the input string. It looks |
| 394 | for doctests, which start with a ">>>" prompt, and may be |
| 395 | continued with "..." prompts, as long as the "..." is indented |
| 396 | the same as the ">>>". |
| 397 | |
| 398 | (Unfortunately we can't use the doctest module's parser, |
| 399 | since, like most parsers, it is not geared towards preserving |
| 400 | the original source.) |
| 401 | """ |
| 402 | result = [] |
| 403 | block = None |
| 404 | block_lineno = None |
| 405 | indent = None |
| 406 | lineno = 0 |
| 407 | for line in input.splitlines(True): |
| 408 | lineno += 1 |
| 409 | if line.lstrip().startswith(self.PS1): |
| 410 | if block is not None: |
| 411 | result.extend(self.refactor_doctest(block, block_lineno, |
| 412 | indent, filename)) |
| 413 | block_lineno = lineno |
| 414 | block = [line] |
| 415 | i = line.find(self.PS1) |
| 416 | indent = line[:i] |
| 417 | elif (indent is not None and |
| 418 | (line.startswith(indent + self.PS2) or |
| 419 | line == indent + self.PS2.rstrip() + "\n")): |
| 420 | block.append(line) |
| 421 | else: |
| 422 | if block is not None: |
| 423 | result.extend(self.refactor_doctest(block, block_lineno, |
| 424 | indent, filename)) |
| 425 | block = None |
| 426 | indent = None |
| 427 | result.append(line) |
| 428 | if block is not None: |
| 429 | result.extend(self.refactor_doctest(block, block_lineno, |
| 430 | indent, filename)) |
| 431 | return "".join(result) |
| 432 | |
| 433 | def refactor_doctest(self, block, lineno, indent, filename): |
| 434 | """Refactors one doctest. |
| 435 | |
| 436 | A doctest is given as a block of lines, the first of which starts |
| 437 | with ">>>" (possibly indented), while the remaining lines start |
| 438 | with "..." (identically indented). |
| 439 | |
| 440 | """ |
| 441 | try: |
| 442 | tree = self.parse_block(block, lineno, indent) |
| 443 | except Exception, err: |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 444 | if self.log.isEnabledFor(logging.DEBUG): |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 445 | for line in block: |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 446 | self.log_debug("Source: %s", line.rstrip("\n")) |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 447 | self.log_error("Can't parse docstring in %s line %s: %s: %s", |
| 448 | filename, lineno, err.__class__.__name__, err) |
| 449 | return block |
| 450 | if self.refactor_tree(tree, filename): |
| 451 | new = str(tree).splitlines(True) |
| 452 | # Undo the adjustment of the line numbers in wrap_toks() below. |
| 453 | clipped, new = new[:lineno-1], new[lineno-1:] |
| 454 | assert clipped == ["\n"] * (lineno-1), clipped |
| 455 | if not new[-1].endswith("\n"): |
| 456 | new[-1] += "\n" |
| 457 | block = [indent + self.PS1 + new.pop(0)] |
| 458 | if new: |
| 459 | block += [indent + self.PS2 + line for line in new] |
| 460 | return block |
| 461 | |
| 462 | def summarize(self): |
Benjamin Peterson | eb55fd8 | 2008-09-03 00:21:32 +0000 | [diff] [blame] | 463 | if self.wrote: |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 464 | were = "were" |
| 465 | else: |
| 466 | were = "need to be" |
| 467 | if not self.files: |
| 468 | self.log_message("No files %s modified.", were) |
| 469 | else: |
| 470 | self.log_message("Files that %s modified:", were) |
| 471 | for file in self.files: |
| 472 | self.log_message(file) |
| 473 | if self.fixer_log: |
| 474 | self.log_message("Warnings/messages while refactoring:") |
| 475 | for message in self.fixer_log: |
| 476 | self.log_message(message) |
| 477 | if self.errors: |
| 478 | if len(self.errors) == 1: |
| 479 | self.log_message("There was 1 error:") |
| 480 | else: |
| 481 | self.log_message("There were %d errors:", len(self.errors)) |
| 482 | for msg, args, kwds in self.errors: |
| 483 | self.log_message(msg, *args, **kwds) |
| 484 | |
| 485 | def parse_block(self, block, lineno, indent): |
| 486 | """Parses a block into a tree. |
| 487 | |
| 488 | This is necessary to get correct line number / offset information |
| 489 | in the parser diagnostics and embedded into the parse tree. |
| 490 | """ |
| 491 | return self.driver.parse_tokens(self.wrap_toks(block, lineno, indent)) |
| 492 | |
| 493 | def wrap_toks(self, block, lineno, indent): |
| 494 | """Wraps a tokenize stream to systematically modify start/end.""" |
| 495 | tokens = tokenize.generate_tokens(self.gen_lines(block, indent).next) |
| 496 | for type, value, (line0, col0), (line1, col1), line_text in tokens: |
| 497 | line0 += lineno - 1 |
| 498 | line1 += lineno - 1 |
| 499 | # Don't bother updating the columns; this is too complicated |
| 500 | # since line_text would also have to be updated and it would |
| 501 | # still break for tokens spanning lines. Let the user guess |
| 502 | # that the column numbers for doctests are relative to the |
| 503 | # end of the prompt string (PS1 or PS2). |
| 504 | yield type, value, (line0, col0), (line1, col1), line_text |
| 505 | |
| 506 | |
| 507 | def gen_lines(self, block, indent): |
| 508 | """Generates lines as expected by tokenize from a list of lines. |
| 509 | |
| 510 | This strips the first len(indent + self.PS1) characters off each line. |
| 511 | """ |
| 512 | prefix1 = indent + self.PS1 |
| 513 | prefix2 = indent + self.PS2 |
| 514 | prefix = prefix1 |
| 515 | for line in block: |
| 516 | if line.startswith(prefix): |
| 517 | yield line[len(prefix):] |
| 518 | elif line == prefix.rstrip() + "\n": |
| 519 | yield "\n" |
| 520 | else: |
| 521 | raise AssertionError("line=%r, prefix=%r" % (line, prefix)) |
| 522 | prefix = prefix2 |
| 523 | while True: |
| 524 | yield "" |
| 525 | |
| 526 | |
| 527 | def diff_texts(a, b, filename): |
Benjamin Peterson | 08be291 | 2008-09-27 21:09:10 +0000 | [diff] [blame] | 528 | """Return a unified diff of two strings.""" |
Martin v. Löwis | 5e37bae | 2008-03-19 04:43:46 +0000 | [diff] [blame] | 529 | a = a.splitlines() |
| 530 | b = b.splitlines() |
Benjamin Peterson | 08be291 | 2008-09-27 21:09:10 +0000 | [diff] [blame] | 531 | return difflib.unified_diff(a, b, filename, filename, |
| 532 | "(original)", "(refactored)", |
| 533 | lineterm="") |