Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 1 | # Copyright 2006 Google, Inc. All Rights Reserved. |
| 2 | # Licensed to PSF under a Contributor Agreement. |
| 3 | |
| 4 | """Refactoring framework. |
| 5 | |
| 6 | Used as a main program, this can refactor any number of files and/or |
| 7 | recursively descend down directories. Imported as a module, this |
| 8 | provides infrastructure to write your own refactoring tool. |
| 9 | """ |
| 10 | |
| 11 | __author__ = "Guido van Rossum <guido@python.org>" |
| 12 | |
| 13 | |
| 14 | # Python imports |
Victor Stinner | 272d888 | 2017-06-16 08:59:01 +0200 | [diff] [blame] | 15 | import io |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 16 | import os |
Miss Islington (bot) | 69802f6 | 2019-07-24 17:00:39 -0700 | [diff] [blame] | 17 | import pkgutil |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 18 | import sys |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 19 | import logging |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 20 | import operator |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 21 | import collections |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 22 | from itertools import chain |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 23 | |
| 24 | # Local imports |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 25 | from .pgen2 import driver, tokenize, token |
Benjamin Peterson | f37eb3a | 2010-10-14 23:00:04 +0000 | [diff] [blame] | 26 | from .fixer_util import find_root |
Benjamin Peterson | 2c3ac6b | 2009-06-11 23:47:38 +0000 | [diff] [blame] | 27 | from . import pytree, pygram |
Benjamin Peterson | f37eb3a | 2010-10-14 23:00:04 +0000 | [diff] [blame] | 28 | from . import btm_matcher as bm |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 29 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 30 | |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 31 | def get_all_fix_names(fixer_pkg, remove_prefix=True): |
| 32 | """Return a sorted list of all available fix names in the given package.""" |
| 33 | pkg = __import__(fixer_pkg, [], [], ["*"]) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 34 | fix_names = [] |
Miss Islington (bot) | 69802f6 | 2019-07-24 17:00:39 -0700 | [diff] [blame] | 35 | for finder, name, ispkg in pkgutil.iter_modules(pkg.__path__): |
| 36 | if name.startswith("fix_"): |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 37 | if remove_prefix: |
| 38 | name = name[4:] |
Miss Islington (bot) | 69802f6 | 2019-07-24 17:00:39 -0700 | [diff] [blame] | 39 | fix_names.append(name) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 40 | return fix_names |
| 41 | |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 42 | |
| 43 | class _EveryNode(Exception): |
| 44 | pass |
| 45 | |
| 46 | |
| 47 | def _get_head_types(pat): |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 48 | """ Accepts a pytree Pattern Node and returns a set |
| 49 | of the pattern types which will match first. """ |
| 50 | |
| 51 | if isinstance(pat, (pytree.NodePattern, pytree.LeafPattern)): |
| 52 | # NodePatters must either have no type and no content |
| 53 | # or a type and content -- so they don't get any farther |
| 54 | # Always return leafs |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 55 | if pat.type is None: |
| 56 | raise _EveryNode |
Serhiy Storchaka | db9b65d | 2014-12-13 21:50:49 +0200 | [diff] [blame] | 57 | return {pat.type} |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 58 | |
| 59 | if isinstance(pat, pytree.NegatedPattern): |
| 60 | if pat.content: |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 61 | return _get_head_types(pat.content) |
| 62 | raise _EveryNode # Negated Patterns don't have a type |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 63 | |
| 64 | if isinstance(pat, pytree.WildcardPattern): |
| 65 | # Recurse on each node in content |
| 66 | r = set() |
| 67 | for p in pat.content: |
| 68 | for x in p: |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 69 | r.update(_get_head_types(x)) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 70 | return r |
| 71 | |
| 72 | raise Exception("Oh no! I don't understand pattern %s" %(pat)) |
| 73 | |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 74 | |
| 75 | def _get_headnode_dict(fixer_list): |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 76 | """ Accepts a list of fixers and returns a dictionary |
| 77 | of head node type --> fixer list. """ |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 78 | head_nodes = collections.defaultdict(list) |
| 79 | every = [] |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 80 | for fixer in fixer_list: |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 81 | if fixer.pattern: |
| 82 | try: |
| 83 | heads = _get_head_types(fixer.pattern) |
| 84 | except _EveryNode: |
| 85 | every.append(fixer) |
| 86 | else: |
| 87 | for node_type in heads: |
| 88 | head_nodes[node_type].append(fixer) |
| 89 | else: |
| 90 | if fixer._accept_type is not None: |
| 91 | head_nodes[fixer._accept_type].append(fixer) |
| 92 | else: |
| 93 | every.append(fixer) |
| 94 | for node_type in chain(pygram.python_grammar.symbol2number.values(), |
| 95 | pygram.python_grammar.tokens): |
| 96 | head_nodes[node_type].extend(every) |
| 97 | return dict(head_nodes) |
| 98 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 99 | |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 100 | def get_fixers_from_package(pkg_name): |
| 101 | """ |
| 102 | Return the fully qualified names for fixers in the package pkg_name. |
| 103 | """ |
| 104 | return [pkg_name + "." + fix_name |
| 105 | for fix_name in get_all_fix_names(pkg_name, False)] |
| 106 | |
Benjamin Peterson | d481e3d | 2009-05-09 19:42:23 +0000 | [diff] [blame] | 107 | def _identity(obj): |
| 108 | return obj |
| 109 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 110 | |
Benjamin Peterson | 8d26b0b | 2010-05-07 19:10:11 +0000 | [diff] [blame] | 111 | def _detect_future_features(source): |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 112 | have_docstring = False |
| 113 | gen = tokenize.generate_tokens(io.StringIO(source).readline) |
| 114 | def advance(): |
| 115 | tok = next(gen) |
| 116 | return tok[0], tok[1] |
Serhiy Storchaka | db9b65d | 2014-12-13 21:50:49 +0200 | [diff] [blame] | 117 | ignore = frozenset({token.NEWLINE, tokenize.NL, token.COMMENT}) |
Benjamin Peterson | 8d26b0b | 2010-05-07 19:10:11 +0000 | [diff] [blame] | 118 | features = set() |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 119 | try: |
| 120 | while True: |
| 121 | tp, value = advance() |
| 122 | if tp in ignore: |
| 123 | continue |
| 124 | elif tp == token.STRING: |
| 125 | if have_docstring: |
| 126 | break |
| 127 | have_docstring = True |
Benjamin Peterson | 2021100 | 2009-11-25 18:34:42 +0000 | [diff] [blame] | 128 | elif tp == token.NAME and value == "from": |
| 129 | tp, value = advance() |
Benjamin Peterson | 8d26b0b | 2010-05-07 19:10:11 +0000 | [diff] [blame] | 130 | if tp != token.NAME or value != "__future__": |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 131 | break |
Benjamin Peterson | 2021100 | 2009-11-25 18:34:42 +0000 | [diff] [blame] | 132 | tp, value = advance() |
Benjamin Peterson | 8d26b0b | 2010-05-07 19:10:11 +0000 | [diff] [blame] | 133 | if tp != token.NAME or value != "import": |
Benjamin Peterson | 2021100 | 2009-11-25 18:34:42 +0000 | [diff] [blame] | 134 | break |
| 135 | tp, value = advance() |
| 136 | if tp == token.OP and value == "(": |
| 137 | tp, value = advance() |
| 138 | while tp == token.NAME: |
Benjamin Peterson | 8d26b0b | 2010-05-07 19:10:11 +0000 | [diff] [blame] | 139 | features.add(value) |
Benjamin Peterson | 2021100 | 2009-11-25 18:34:42 +0000 | [diff] [blame] | 140 | tp, value = advance() |
Benjamin Peterson | 8d26b0b | 2010-05-07 19:10:11 +0000 | [diff] [blame] | 141 | if tp != token.OP or value != ",": |
Benjamin Peterson | 2021100 | 2009-11-25 18:34:42 +0000 | [diff] [blame] | 142 | break |
| 143 | tp, value = advance() |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 144 | else: |
| 145 | break |
| 146 | except StopIteration: |
| 147 | pass |
Benjamin Peterson | 8d26b0b | 2010-05-07 19:10:11 +0000 | [diff] [blame] | 148 | return frozenset(features) |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 149 | |
| 150 | |
Benjamin Peterson | d61de7f | 2008-09-27 22:17:35 +0000 | [diff] [blame] | 151 | class FixerError(Exception): |
| 152 | """A fixer could not be loaded.""" |
| 153 | |
| 154 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 155 | class RefactoringTool(object): |
| 156 | |
Gregory P. Smith | 58f23ff | 2012-02-12 15:50:21 -0800 | [diff] [blame] | 157 | _default_options = {"print_function" : False, |
| 158 | "write_unchanged_files" : False} |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 159 | |
Benjamin Peterson | d61de7f | 2008-09-27 22:17:35 +0000 | [diff] [blame] | 160 | CLASS_PREFIX = "Fix" # The prefix for fixer classes |
| 161 | FILE_PREFIX = "fix_" # The prefix for modules with a fixer within |
| 162 | |
| 163 | def __init__(self, fixer_names, options=None, explicit=None): |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 164 | """Initializer. |
| 165 | |
Benjamin Peterson | df6dc8f | 2008-06-15 02:57:40 +0000 | [diff] [blame] | 166 | Args: |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 167 | fixer_names: a list of fixers to import |
Serhiy Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame] | 168 | options: a dict with configuration. |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 169 | explicit: a list of fixers to run even if they are explicit. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 170 | """ |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 171 | self.fixers = fixer_names |
Benjamin Peterson | d61de7f | 2008-09-27 22:17:35 +0000 | [diff] [blame] | 172 | self.explicit = explicit or [] |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 173 | self.options = self._default_options.copy() |
| 174 | if options is not None: |
| 175 | self.options.update(options) |
Benjamin Peterson | 2021100 | 2009-11-25 18:34:42 +0000 | [diff] [blame] | 176 | if self.options["print_function"]: |
| 177 | self.grammar = pygram.python_grammar_no_print_statement |
| 178 | else: |
| 179 | self.grammar = pygram.python_grammar |
Gregory P. Smith | 58f23ff | 2012-02-12 15:50:21 -0800 | [diff] [blame] | 180 | # When this is True, the refactor*() methods will call write_file() for |
| 181 | # files processed even if they were not changed during refactoring. If |
| 182 | # and only if the refactor method's write parameter was True. |
| 183 | self.write_unchanged_files = self.options.get("write_unchanged_files") |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 184 | self.errors = [] |
| 185 | self.logger = logging.getLogger("RefactoringTool") |
| 186 | self.fixer_log = [] |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 187 | self.wrote = False |
Benjamin Peterson | 2021100 | 2009-11-25 18:34:42 +0000 | [diff] [blame] | 188 | self.driver = driver.Driver(self.grammar, |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 189 | convert=pytree.convert, |
| 190 | logger=self.logger) |
| 191 | self.pre_order, self.post_order = self.get_fixers() |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 192 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 193 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 194 | self.files = [] # List of files that were or should be modified |
| 195 | |
Benjamin Peterson | f37eb3a | 2010-10-14 23:00:04 +0000 | [diff] [blame] | 196 | self.BM = bm.BottomMatcher() |
| 197 | self.bmi_pre_order = [] # Bottom Matcher incompatible fixers |
| 198 | self.bmi_post_order = [] |
| 199 | |
| 200 | for fixer in chain(self.post_order, self.pre_order): |
| 201 | if fixer.BM_compatible: |
| 202 | self.BM.add_fixer(fixer) |
| 203 | # remove fixers that will be handled by the bottom-up |
| 204 | # matcher |
| 205 | elif fixer in self.pre_order: |
| 206 | self.bmi_pre_order.append(fixer) |
| 207 | elif fixer in self.post_order: |
| 208 | self.bmi_post_order.append(fixer) |
| 209 | |
| 210 | self.bmi_pre_order_heads = _get_headnode_dict(self.bmi_pre_order) |
| 211 | self.bmi_post_order_heads = _get_headnode_dict(self.bmi_post_order) |
| 212 | |
| 213 | |
| 214 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 215 | def get_fixers(self): |
| 216 | """Inspects the options to load the requested patterns and handlers. |
Martin v. Löwis | f733c60 | 2008-03-19 05:26:18 +0000 | [diff] [blame] | 217 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 218 | Returns: |
| 219 | (pre_order, post_order), where pre_order is the list of fixers that |
| 220 | want a pre-order AST traversal, and post_order is the list that want |
| 221 | post-order traversal. |
| 222 | """ |
| 223 | pre_order_fixers = [] |
| 224 | post_order_fixers = [] |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 225 | for fix_mod_path in self.fixers: |
Benjamin Peterson | d61de7f | 2008-09-27 22:17:35 +0000 | [diff] [blame] | 226 | mod = __import__(fix_mod_path, {}, {}, ["*"]) |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 227 | fix_name = fix_mod_path.rsplit(".", 1)[-1] |
Benjamin Peterson | d61de7f | 2008-09-27 22:17:35 +0000 | [diff] [blame] | 228 | if fix_name.startswith(self.FILE_PREFIX): |
| 229 | fix_name = fix_name[len(self.FILE_PREFIX):] |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 230 | parts = fix_name.split("_") |
Benjamin Peterson | d61de7f | 2008-09-27 22:17:35 +0000 | [diff] [blame] | 231 | class_name = self.CLASS_PREFIX + "".join([p.title() for p in parts]) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 232 | try: |
| 233 | fix_class = getattr(mod, class_name) |
| 234 | except AttributeError: |
Serhiy Storchaka | 5affd23 | 2017-04-05 09:37:24 +0300 | [diff] [blame] | 235 | raise FixerError("Can't find %s.%s" % (fix_name, class_name)) from None |
Benjamin Peterson | d61de7f | 2008-09-27 22:17:35 +0000 | [diff] [blame] | 236 | fixer = fix_class(self.options, self.fixer_log) |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 237 | if fixer.explicit and self.explicit is not True and \ |
| 238 | fix_mod_path not in self.explicit: |
Berker Peksag | 3a81f9b | 2015-05-13 13:39:51 +0300 | [diff] [blame] | 239 | self.log_message("Skipping optional fixer: %s", fix_name) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 240 | continue |
| 241 | |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 242 | self.log_debug("Adding transformation: %s", fix_name) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 243 | if fixer.order == "pre": |
| 244 | pre_order_fixers.append(fixer) |
| 245 | elif fixer.order == "post": |
| 246 | post_order_fixers.append(fixer) |
| 247 | else: |
Benjamin Peterson | d61de7f | 2008-09-27 22:17:35 +0000 | [diff] [blame] | 248 | raise FixerError("Illegal fixer order: %r" % fixer.order) |
Martin v. Löwis | 3faa84f | 2008-03-22 00:07:09 +0000 | [diff] [blame] | 249 | |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 250 | key_func = operator.attrgetter("run_order") |
| 251 | pre_order_fixers.sort(key=key_func) |
| 252 | post_order_fixers.sort(key=key_func) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 253 | return (pre_order_fixers, post_order_fixers) |
| 254 | |
| 255 | def log_error(self, msg, *args, **kwds): |
Benjamin Peterson | d61de7f | 2008-09-27 22:17:35 +0000 | [diff] [blame] | 256 | """Called when an error occurs.""" |
| 257 | raise |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 258 | |
| 259 | def log_message(self, msg, *args): |
| 260 | """Hook to log a message.""" |
| 261 | if args: |
| 262 | msg = msg % args |
| 263 | self.logger.info(msg) |
| 264 | |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 265 | def log_debug(self, msg, *args): |
| 266 | if args: |
| 267 | msg = msg % args |
| 268 | self.logger.debug(msg) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 269 | |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 270 | def print_output(self, old_text, new_text, filename, equal): |
| 271 | """Called with the old version, new version, and filename of a |
| 272 | refactored file.""" |
Benjamin Peterson | d61de7f | 2008-09-27 22:17:35 +0000 | [diff] [blame] | 273 | pass |
| 274 | |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 275 | def refactor(self, items, write=False, doctests_only=False): |
| 276 | """Refactor a list of files and directories.""" |
Benjamin Peterson | f37eb3a | 2010-10-14 23:00:04 +0000 | [diff] [blame] | 277 | |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 278 | for dir_or_file in items: |
| 279 | if os.path.isdir(dir_or_file): |
Benjamin Peterson | d61de7f | 2008-09-27 22:17:35 +0000 | [diff] [blame] | 280 | self.refactor_dir(dir_or_file, write, doctests_only) |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 281 | else: |
Benjamin Peterson | d61de7f | 2008-09-27 22:17:35 +0000 | [diff] [blame] | 282 | self.refactor_file(dir_or_file, write, doctests_only) |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 283 | |
| 284 | def refactor_dir(self, dir_name, write=False, doctests_only=False): |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 285 | """Descends down a directory and refactor every Python file found. |
| 286 | |
| 287 | Python files are assumed to have a .py extension. |
| 288 | |
| 289 | Files and subdirectories starting with '.' are skipped. |
| 290 | """ |
Martin v. Löwis | e2bb4eb | 2010-12-03 23:11:07 +0000 | [diff] [blame] | 291 | py_ext = os.extsep + "py" |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 292 | for dirpath, dirnames, filenames in os.walk(dir_name): |
| 293 | self.log_debug("Descending into %s", dirpath) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 294 | dirnames.sort() |
| 295 | filenames.sort() |
| 296 | for name in filenames: |
Martin v. Löwis | e2bb4eb | 2010-12-03 23:11:07 +0000 | [diff] [blame] | 297 | if (not name.startswith(".") and |
| 298 | os.path.splitext(name)[1] == py_ext): |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 299 | fullname = os.path.join(dirpath, name) |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 300 | self.refactor_file(fullname, write, doctests_only) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 301 | # Modify dirnames in-place to remove subdirs with leading dots |
| 302 | dirnames[:] = [dn for dn in dirnames if not dn.startswith(".")] |
| 303 | |
Benjamin Peterson | d481e3d | 2009-05-09 19:42:23 +0000 | [diff] [blame] | 304 | def _read_python_source(self, filename): |
| 305 | """ |
| 306 | Do our best to decode a Python source file correctly. |
| 307 | """ |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 308 | try: |
Benjamin Peterson | d481e3d | 2009-05-09 19:42:23 +0000 | [diff] [blame] | 309 | f = open(filename, "rb") |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 310 | except OSError as err: |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 311 | self.log_error("Can't open %s: %s", filename, err) |
Benjamin Peterson | d481e3d | 2009-05-09 19:42:23 +0000 | [diff] [blame] | 312 | return None, None |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 313 | try: |
Benjamin Peterson | d481e3d | 2009-05-09 19:42:23 +0000 | [diff] [blame] | 314 | encoding = tokenize.detect_encoding(f.readline)[0] |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 315 | finally: |
| 316 | f.close() |
Aaron Ang | c127a86 | 2018-04-17 14:34:14 -0700 | [diff] [blame] | 317 | with io.open(filename, "r", encoding=encoding, newline='') as f: |
Victor Stinner | 272d888 | 2017-06-16 08:59:01 +0200 | [diff] [blame] | 318 | return f.read(), encoding |
Benjamin Peterson | d481e3d | 2009-05-09 19:42:23 +0000 | [diff] [blame] | 319 | |
| 320 | def refactor_file(self, filename, write=False, doctests_only=False): |
| 321 | """Refactors a file.""" |
| 322 | input, encoding = self._read_python_source(filename) |
| 323 | if input is None: |
| 324 | # Reading the file failed. |
| 325 | return |
| 326 | input += "\n" # Silence certain parse errors |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 327 | if doctests_only: |
| 328 | self.log_debug("Refactoring doctests in %s", filename) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 329 | output = self.refactor_docstring(input, filename) |
Gregory P. Smith | 58f23ff | 2012-02-12 15:50:21 -0800 | [diff] [blame] | 330 | if self.write_unchanged_files or output != input: |
Benjamin Peterson | d481e3d | 2009-05-09 19:42:23 +0000 | [diff] [blame] | 331 | self.processed_file(output, filename, input, write, encoding) |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 332 | else: |
| 333 | self.log_debug("No doctest changes in %s", filename) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 334 | else: |
| 335 | tree = self.refactor_string(input, filename) |
Gregory P. Smith | 58f23ff | 2012-02-12 15:50:21 -0800 | [diff] [blame] | 336 | if self.write_unchanged_files or (tree and tree.was_changed): |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 337 | # The [:-1] is to take off the \n we added earlier |
Benjamin Peterson | d481e3d | 2009-05-09 19:42:23 +0000 | [diff] [blame] | 338 | self.processed_file(str(tree)[:-1], filename, |
| 339 | write=write, encoding=encoding) |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 340 | else: |
| 341 | self.log_debug("No changes in %s", filename) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 342 | |
| 343 | def refactor_string(self, data, name): |
| 344 | """Refactor a given input string. |
Martin v. Löwis | f733c60 | 2008-03-19 05:26:18 +0000 | [diff] [blame] | 345 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 346 | Args: |
| 347 | data: a string holding the code to be refactored. |
| 348 | name: a human-readable name for use in error/log messages. |
Martin v. Löwis | f733c60 | 2008-03-19 05:26:18 +0000 | [diff] [blame] | 349 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 350 | Returns: |
| 351 | An AST corresponding to the refactored input stream; None if |
| 352 | there were errors during the parse. |
| 353 | """ |
Benjamin Peterson | 8d26b0b | 2010-05-07 19:10:11 +0000 | [diff] [blame] | 354 | features = _detect_future_features(data) |
| 355 | if "print_function" in features: |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 356 | self.driver.grammar = pygram.python_grammar_no_print_statement |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 357 | try: |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 358 | tree = self.driver.parse_string(data) |
Martin v. Löwis | 8a5f8ca | 2008-03-19 05:33:36 +0000 | [diff] [blame] | 359 | except Exception as err: |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 360 | self.log_error("Can't parse %s: %s: %s", |
| 361 | name, err.__class__.__name__, err) |
| 362 | return |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 363 | finally: |
Benjamin Peterson | 2021100 | 2009-11-25 18:34:42 +0000 | [diff] [blame] | 364 | self.driver.grammar = self.grammar |
Benjamin Peterson | 8d26b0b | 2010-05-07 19:10:11 +0000 | [diff] [blame] | 365 | tree.future_features = features |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 366 | self.log_debug("Refactoring %s", name) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 367 | self.refactor_tree(tree, name) |
| 368 | return tree |
| 369 | |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 370 | def refactor_stdin(self, doctests_only=False): |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 371 | input = sys.stdin.read() |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 372 | if doctests_only: |
| 373 | self.log_debug("Refactoring doctests in stdin") |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 374 | output = self.refactor_docstring(input, "<stdin>") |
Gregory P. Smith | 58f23ff | 2012-02-12 15:50:21 -0800 | [diff] [blame] | 375 | if self.write_unchanged_files or output != input: |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 376 | self.processed_file(output, "<stdin>", input) |
| 377 | else: |
| 378 | self.log_debug("No doctest changes in stdin") |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 379 | else: |
| 380 | tree = self.refactor_string(input, "<stdin>") |
Gregory P. Smith | 58f23ff | 2012-02-12 15:50:21 -0800 | [diff] [blame] | 381 | if self.write_unchanged_files or (tree and tree.was_changed): |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 382 | self.processed_file(str(tree), "<stdin>", input) |
| 383 | else: |
| 384 | self.log_debug("No changes in stdin") |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 385 | |
| 386 | def refactor_tree(self, tree, name): |
| 387 | """Refactors a parse tree (modifying the tree in place). |
Martin v. Löwis | f733c60 | 2008-03-19 05:26:18 +0000 | [diff] [blame] | 388 | |
Benjamin Peterson | f37eb3a | 2010-10-14 23:00:04 +0000 | [diff] [blame] | 389 | For compatible patterns the bottom matcher module is |
| 390 | used. Otherwise the tree is traversed node-to-node for |
| 391 | matches. |
| 392 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 393 | Args: |
| 394 | tree: a pytree.Node instance representing the root of the tree |
| 395 | to be refactored. |
| 396 | name: a human-readable name for this tree. |
Martin v. Löwis | f733c60 | 2008-03-19 05:26:18 +0000 | [diff] [blame] | 397 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 398 | Returns: |
| 399 | True if the tree was modified, False otherwise. |
| 400 | """ |
Benjamin Peterson | f37eb3a | 2010-10-14 23:00:04 +0000 | [diff] [blame] | 401 | |
Benjamin Peterson | 8bcddca | 2009-01-03 16:53:14 +0000 | [diff] [blame] | 402 | for fixer in chain(self.pre_order, self.post_order): |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 403 | fixer.start_tree(tree, name) |
| 404 | |
Benjamin Peterson | f37eb3a | 2010-10-14 23:00:04 +0000 | [diff] [blame] | 405 | #use traditional matching for the incompatible fixers |
| 406 | self.traverse_by(self.bmi_pre_order_heads, tree.pre_order()) |
| 407 | self.traverse_by(self.bmi_post_order_heads, tree.post_order()) |
| 408 | |
| 409 | # obtain a set of candidate nodes |
| 410 | match_set = self.BM.run(tree.leaves()) |
| 411 | |
| 412 | while any(match_set.values()): |
| 413 | for fixer in self.BM.fixers: |
| 414 | if fixer in match_set and match_set[fixer]: |
| 415 | #sort by depth; apply fixers from bottom(of the AST) to top |
| 416 | match_set[fixer].sort(key=pytree.Base.depth, reverse=True) |
| 417 | |
| 418 | if fixer.keep_line_order: |
| 419 | #some fixers(eg fix_imports) must be applied |
| 420 | #with the original file's line order |
| 421 | match_set[fixer].sort(key=pytree.Base.get_lineno) |
| 422 | |
| 423 | for node in list(match_set[fixer]): |
| 424 | if node in match_set[fixer]: |
| 425 | match_set[fixer].remove(node) |
| 426 | |
| 427 | try: |
| 428 | find_root(node) |
Benjamin Peterson | 1654d74 | 2012-09-25 11:48:50 -0400 | [diff] [blame] | 429 | except ValueError: |
Benjamin Peterson | f37eb3a | 2010-10-14 23:00:04 +0000 | [diff] [blame] | 430 | # this node has been cut off from a |
| 431 | # previous transformation ; skip |
| 432 | continue |
| 433 | |
| 434 | if node.fixers_applied and fixer in node.fixers_applied: |
| 435 | # do not apply the same fixer again |
| 436 | continue |
| 437 | |
| 438 | results = fixer.match(node) |
| 439 | |
| 440 | if results: |
| 441 | new = fixer.transform(node, results) |
| 442 | if new is not None: |
| 443 | node.replace(new) |
| 444 | #new.fixers_applied.append(fixer) |
| 445 | for node in new.post_order(): |
| 446 | # do not apply the fixer again to |
| 447 | # this or any subnode |
| 448 | if not node.fixers_applied: |
| 449 | node.fixers_applied = [] |
| 450 | node.fixers_applied.append(fixer) |
| 451 | |
| 452 | # update the original match set for |
| 453 | # the added code |
| 454 | new_matches = self.BM.run(new.leaves()) |
| 455 | for fxr in new_matches: |
| 456 | if not fxr in match_set: |
| 457 | match_set[fxr]=[] |
| 458 | |
| 459 | match_set[fxr].extend(new_matches[fxr]) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 460 | |
Benjamin Peterson | 8bcddca | 2009-01-03 16:53:14 +0000 | [diff] [blame] | 461 | for fixer in chain(self.pre_order, self.post_order): |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 462 | fixer.finish_tree(tree, name) |
| 463 | return tree.was_changed |
| 464 | |
| 465 | def traverse_by(self, fixers, traversal): |
| 466 | """Traverse an AST, applying a set of fixers to each node. |
Martin v. Löwis | f733c60 | 2008-03-19 05:26:18 +0000 | [diff] [blame] | 467 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 468 | This is a helper method for refactor_tree(). |
Martin v. Löwis | f733c60 | 2008-03-19 05:26:18 +0000 | [diff] [blame] | 469 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 470 | Args: |
| 471 | fixers: a list of fixer instances. |
| 472 | traversal: a generator that yields AST nodes. |
Martin v. Löwis | f733c60 | 2008-03-19 05:26:18 +0000 | [diff] [blame] | 473 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 474 | Returns: |
| 475 | None |
| 476 | """ |
| 477 | if not fixers: |
| 478 | return |
| 479 | for node in traversal: |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 480 | for fixer in fixers[node.type]: |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 481 | results = fixer.match(node) |
| 482 | if results: |
| 483 | new = fixer.transform(node, results) |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 484 | if new is not None: |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 485 | node.replace(new) |
| 486 | node = new |
| 487 | |
Benjamin Peterson | d481e3d | 2009-05-09 19:42:23 +0000 | [diff] [blame] | 488 | def processed_file(self, new_text, filename, old_text=None, write=False, |
| 489 | encoding=None): |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 490 | """ |
Gregory P. Smith | 58f23ff | 2012-02-12 15:50:21 -0800 | [diff] [blame] | 491 | Called when a file has been refactored and there may be changes. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 492 | """ |
| 493 | self.files.append(filename) |
| 494 | if old_text is None: |
Benjamin Peterson | d481e3d | 2009-05-09 19:42:23 +0000 | [diff] [blame] | 495 | old_text = self._read_python_source(filename)[0] |
| 496 | if old_text is None: |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 497 | return |
Benjamin Peterson | 3059b00 | 2009-07-20 16:42:03 +0000 | [diff] [blame] | 498 | equal = old_text == new_text |
| 499 | self.print_output(old_text, new_text, filename, equal) |
| 500 | if equal: |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 501 | self.log_debug("No changes to %s", filename) |
Gregory P. Smith | 58f23ff | 2012-02-12 15:50:21 -0800 | [diff] [blame] | 502 | if not self.write_unchanged_files: |
| 503 | return |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 504 | if write: |
Benjamin Peterson | d481e3d | 2009-05-09 19:42:23 +0000 | [diff] [blame] | 505 | self.write_file(new_text, filename, old_text, encoding) |
Benjamin Peterson | d61de7f | 2008-09-27 22:17:35 +0000 | [diff] [blame] | 506 | else: |
| 507 | self.log_debug("Not writing changes to %s", filename) |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 508 | |
Benjamin Peterson | d481e3d | 2009-05-09 19:42:23 +0000 | [diff] [blame] | 509 | def write_file(self, new_text, filename, old_text, encoding=None): |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 510 | """Writes a string to a file. |
| 511 | |
| 512 | It first shows a unified diff between the old text and the new text, and |
| 513 | then rewrites the file; the latter is only done if the write option is |
| 514 | set. |
| 515 | """ |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 516 | try: |
Jason R. Coombs | cafaf04 | 2018-07-13 11:26:03 -0400 | [diff] [blame] | 517 | fp = io.open(filename, "w", encoding=encoding, newline='') |
Andrew Svetlov | ad28c7f | 2012-12-18 22:02:39 +0200 | [diff] [blame] | 518 | except OSError as err: |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 519 | self.log_error("Can't create %s: %s", filename, err) |
| 520 | return |
Victor Stinner | 272d888 | 2017-06-16 08:59:01 +0200 | [diff] [blame] | 521 | |
| 522 | with fp: |
| 523 | try: |
| 524 | fp.write(new_text) |
| 525 | except OSError as err: |
| 526 | self.log_error("Can't write %s: %s", filename, err) |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 527 | self.log_debug("Wrote changes to %s", filename) |
| 528 | self.wrote = True |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 529 | |
| 530 | PS1 = ">>> " |
| 531 | PS2 = "... " |
| 532 | |
| 533 | def refactor_docstring(self, input, filename): |
| 534 | """Refactors a docstring, looking for doctests. |
| 535 | |
| 536 | This returns a modified version of the input string. It looks |
| 537 | for doctests, which start with a ">>>" prompt, and may be |
| 538 | continued with "..." prompts, as long as the "..." is indented |
| 539 | the same as the ">>>". |
| 540 | |
| 541 | (Unfortunately we can't use the doctest module's parser, |
| 542 | since, like most parsers, it is not geared towards preserving |
| 543 | the original source.) |
| 544 | """ |
| 545 | result = [] |
| 546 | block = None |
| 547 | block_lineno = None |
| 548 | indent = None |
| 549 | lineno = 0 |
Ezio Melotti | d8b509b | 2011-09-28 17:37:55 +0300 | [diff] [blame] | 550 | for line in input.splitlines(keepends=True): |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 551 | lineno += 1 |
| 552 | if line.lstrip().startswith(self.PS1): |
| 553 | if block is not None: |
| 554 | result.extend(self.refactor_doctest(block, block_lineno, |
| 555 | indent, filename)) |
| 556 | block_lineno = lineno |
| 557 | block = [line] |
| 558 | i = line.find(self.PS1) |
| 559 | indent = line[:i] |
| 560 | elif (indent is not None and |
| 561 | (line.startswith(indent + self.PS2) or |
| 562 | line == indent + self.PS2.rstrip() + "\n")): |
| 563 | block.append(line) |
| 564 | else: |
| 565 | if block is not None: |
| 566 | result.extend(self.refactor_doctest(block, block_lineno, |
| 567 | indent, filename)) |
| 568 | block = None |
| 569 | indent = None |
| 570 | result.append(line) |
| 571 | if block is not None: |
| 572 | result.extend(self.refactor_doctest(block, block_lineno, |
| 573 | indent, filename)) |
| 574 | return "".join(result) |
| 575 | |
| 576 | def refactor_doctest(self, block, lineno, indent, filename): |
| 577 | """Refactors one doctest. |
| 578 | |
| 579 | A doctest is given as a block of lines, the first of which starts |
| 580 | with ">>>" (possibly indented), while the remaining lines start |
| 581 | with "..." (identically indented). |
| 582 | |
| 583 | """ |
| 584 | try: |
| 585 | tree = self.parse_block(block, lineno, indent) |
Martin v. Löwis | 8a5f8ca | 2008-03-19 05:33:36 +0000 | [diff] [blame] | 586 | except Exception as err: |
Benjamin Peterson | 4eb5fa5 | 2010-08-08 19:01:25 +0000 | [diff] [blame] | 587 | if self.logger.isEnabledFor(logging.DEBUG): |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 588 | for line in block: |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 589 | self.log_debug("Source: %s", line.rstrip("\n")) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 590 | self.log_error("Can't parse docstring in %s line %s: %s: %s", |
| 591 | filename, lineno, err.__class__.__name__, err) |
| 592 | return block |
| 593 | if self.refactor_tree(tree, filename): |
Ezio Melotti | d8b509b | 2011-09-28 17:37:55 +0300 | [diff] [blame] | 594 | new = str(tree).splitlines(keepends=True) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 595 | # Undo the adjustment of the line numbers in wrap_toks() below. |
| 596 | clipped, new = new[:lineno-1], new[lineno-1:] |
| 597 | assert clipped == ["\n"] * (lineno-1), clipped |
| 598 | if not new[-1].endswith("\n"): |
| 599 | new[-1] += "\n" |
| 600 | block = [indent + self.PS1 + new.pop(0)] |
| 601 | if new: |
| 602 | block += [indent + self.PS2 + line for line in new] |
| 603 | return block |
| 604 | |
| 605 | def summarize(self): |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 606 | if self.wrote: |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 607 | were = "were" |
| 608 | else: |
| 609 | were = "need to be" |
| 610 | if not self.files: |
| 611 | self.log_message("No files %s modified.", were) |
| 612 | else: |
| 613 | self.log_message("Files that %s modified:", were) |
| 614 | for file in self.files: |
| 615 | self.log_message(file) |
| 616 | if self.fixer_log: |
| 617 | self.log_message("Warnings/messages while refactoring:") |
| 618 | for message in self.fixer_log: |
| 619 | self.log_message(message) |
| 620 | if self.errors: |
| 621 | if len(self.errors) == 1: |
| 622 | self.log_message("There was 1 error:") |
| 623 | else: |
| 624 | self.log_message("There were %d errors:", len(self.errors)) |
| 625 | for msg, args, kwds in self.errors: |
| 626 | self.log_message(msg, *args, **kwds) |
| 627 | |
| 628 | def parse_block(self, block, lineno, indent): |
| 629 | """Parses a block into a tree. |
| 630 | |
| 631 | This is necessary to get correct line number / offset information |
| 632 | in the parser diagnostics and embedded into the parse tree. |
| 633 | """ |
Benjamin Peterson | 2243e5e | 2010-05-22 18:59:24 +0000 | [diff] [blame] | 634 | tree = self.driver.parse_tokens(self.wrap_toks(block, lineno, indent)) |
| 635 | tree.future_features = frozenset() |
| 636 | return tree |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 637 | |
| 638 | def wrap_toks(self, block, lineno, indent): |
| 639 | """Wraps a tokenize stream to systematically modify start/end.""" |
Martin v. Löwis | 8a5f8ca | 2008-03-19 05:33:36 +0000 | [diff] [blame] | 640 | tokens = tokenize.generate_tokens(self.gen_lines(block, indent).__next__) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 641 | for type, value, (line0, col0), (line1, col1), line_text in tokens: |
| 642 | line0 += lineno - 1 |
| 643 | line1 += lineno - 1 |
| 644 | # Don't bother updating the columns; this is too complicated |
| 645 | # since line_text would also have to be updated and it would |
| 646 | # still break for tokens spanning lines. Let the user guess |
| 647 | # that the column numbers for doctests are relative to the |
| 648 | # end of the prompt string (PS1 or PS2). |
| 649 | yield type, value, (line0, col0), (line1, col1), line_text |
| 650 | |
| 651 | |
| 652 | def gen_lines(self, block, indent): |
| 653 | """Generates lines as expected by tokenize from a list of lines. |
| 654 | |
| 655 | This strips the first len(indent + self.PS1) characters off each line. |
| 656 | """ |
| 657 | prefix1 = indent + self.PS1 |
| 658 | prefix2 = indent + self.PS2 |
| 659 | prefix = prefix1 |
| 660 | for line in block: |
| 661 | if line.startswith(prefix): |
| 662 | yield line[len(prefix):] |
| 663 | elif line == prefix.rstrip() + "\n": |
| 664 | yield "\n" |
| 665 | else: |
| 666 | raise AssertionError("line=%r, prefix=%r" % (line, prefix)) |
| 667 | prefix = prefix2 |
| 668 | while True: |
| 669 | yield "" |
| 670 | |
| 671 | |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 672 | class MultiprocessingUnsupported(Exception): |
| 673 | pass |
| 674 | |
| 675 | |
| 676 | class MultiprocessRefactoringTool(RefactoringTool): |
| 677 | |
| 678 | def __init__(self, *args, **kwargs): |
| 679 | super(MultiprocessRefactoringTool, self).__init__(*args, **kwargs) |
| 680 | self.queue = None |
Benjamin Peterson | 8d26b0b | 2010-05-07 19:10:11 +0000 | [diff] [blame] | 681 | self.output_lock = None |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 682 | |
| 683 | def refactor(self, items, write=False, doctests_only=False, |
| 684 | num_processes=1): |
| 685 | if num_processes == 1: |
| 686 | return super(MultiprocessRefactoringTool, self).refactor( |
| 687 | items, write, doctests_only) |
| 688 | try: |
| 689 | import multiprocessing |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 690 | except ImportError: |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 691 | raise MultiprocessingUnsupported |
| 692 | if self.queue is not None: |
| 693 | raise RuntimeError("already doing multiple processes") |
| 694 | self.queue = multiprocessing.JoinableQueue() |
Benjamin Peterson | 8d26b0b | 2010-05-07 19:10:11 +0000 | [diff] [blame] | 695 | self.output_lock = multiprocessing.Lock() |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 696 | processes = [multiprocessing.Process(target=self._child) |
Benjamin Peterson | 87b8719 | 2009-07-03 13:18:18 +0000 | [diff] [blame] | 697 | for i in range(num_processes)] |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 698 | try: |
| 699 | for p in processes: |
| 700 | p.start() |
| 701 | super(MultiprocessRefactoringTool, self).refactor(items, write, |
| 702 | doctests_only) |
| 703 | finally: |
| 704 | self.queue.join() |
Benjamin Peterson | 87b8719 | 2009-07-03 13:18:18 +0000 | [diff] [blame] | 705 | for i in range(num_processes): |
Benjamin Peterson | 608d8bc | 2009-05-05 23:23:31 +0000 | [diff] [blame] | 706 | self.queue.put(None) |
| 707 | for p in processes: |
| 708 | if p.is_alive(): |
| 709 | p.join() |
| 710 | self.queue = None |
| 711 | |
| 712 | def _child(self): |
| 713 | task = self.queue.get() |
| 714 | while task is not None: |
| 715 | args, kwargs = task |
| 716 | try: |
| 717 | super(MultiprocessRefactoringTool, self).refactor_file( |
| 718 | *args, **kwargs) |
| 719 | finally: |
| 720 | self.queue.task_done() |
| 721 | task = self.queue.get() |
| 722 | |
| 723 | def refactor_file(self, *args, **kwargs): |
| 724 | if self.queue is not None: |
| 725 | self.queue.put((args, kwargs)) |
| 726 | else: |
| 727 | return super(MultiprocessRefactoringTool, self).refactor_file( |
| 728 | *args, **kwargs) |