Skip to content
Myr

Updated upgrade script to no longer change url_for that don't need to be changed

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 874079b · Browse files · 3 files+59 added−18 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+59 added−18 removed

  • other:+59 added−18 removed

3 files+59 added−18 removed

modified:scripts/testproj/templates/index.html+1 added
modified:scripts/testproj/test.py+10 added-1 removed
1from flask import Flask, Module, render_template1from flask import Flask, Module, render_template, url_for234mod = Module(__name__)5mod2 = Module(__name__, 'testmod2')
5 unmodified lines
11 g.db.close()12 return response131415@app.route('/')16def index_foo():17 x1 = url_for('somemod.index')18 x2 = url_for('.index')19 return render_template('test/index.html')202122@mod.route('/')23def index():24 x1 = url_for('somemod.index')25 x2 = url_for('.index')26 return render_template('test/index.html')272829@mod2.route('/')
17 unmodified lines
modified:scripts/flask-07-upgrade.py+48 added-17 removed
44 unmodified lines
45_module_constructor_re = re.compile(r'([a-zA-Z0-9_][a-zA-Z0-9_]*)\s*=\s*Module'46 r'\(__name__\s*(?:,\s*(?:name\s*=\s*)?(%s))?' %47 _string_re_part)48_error_handler_re = re.compile(r'%s\.error_handlers\[\s*(\d+)\s*\]' % _app_re_part)49_mod_route_re = re.compile(r'([a-zA-Z0-9_][a-zA-Z0-9_]*)\.route')49_mod_route_re = re.compile(r'@([a-zA-Z0-9_][a-zA-Z0-9_]*)\.route')50_blueprint_related = [51 (re.compile(r'request\.module'), 'request.blueprint'),52 (re.compile(r'register_module'), 'register_blueprint'),53 (re.compile(r'%s\.modules' % _app_re_part), '\\1.blueprints')
7 unmodified lines
61 lineterm=''):62 print line636465def fix_url_for(contents):66 def handle_match(match):67 prefix = match.group(1)68 endpoint = ast.literal_eval(match.group(2))69 if endpoint.startswith('.'):70 endpoint = endpoint[1:]71 else:72 endpoint = '.' + endpoint73 return prefix + repr(endpoint)74 return _url_for_re.sub(handle_match, contents)757665def looks_like_teardown_function(node):66 returns = [x for x in ast.walk(node) if isinstance(x, ast.Return)]67 if len(returns) != 1:68 return
12 unmodified lines
8182 return resp_name.id838485def fix_url_for(contents, module_declarations=None):86 if module_declarations is None:87 skip_module_test = True88 else:89 skip_module_test = False90 mapping = dict(module_declarations)91 annotated_lines = []9293 def make_line_annotations():94 if not annotated_lines:95 last_index = 096 for line in contents.splitlines(True):97 last_index += len(line)98 annotated_lines.append((last_index, line))99100 def backtrack_module_name(call_start):101 make_line_annotations()102 for idx, (line_end, line) in enumerate(annotated_lines):103 if line_end > call_start:104 for _, line in reversed(annotated_lines[:idx]):105 match = _mod_route_re.search(line)106 if match is not None:107 shortname = match.group(1)108 return mapping.get(shortname)109110 def handle_match(match):111 if not skip_module_test:112 modname = backtrack_module_name(match.start())113 if modname is None:114 return match.group(0)115 prefix = match.group(1)116 endpoint = ast.literal_eval(match.group(2))117 if endpoint.startswith('.'):118 endpoint = endpoint[1:]119 elif '.' not in endpoint:120 endpoint = '.' + endpoint121 else:122 return match.group(0)123 return prefix + repr(endpoint)124 return _url_for_re.sub(handle_match, contents)125126127def fix_teardown_funcs(contents):128129 def is_return_line(line):130 args = line.strip().split()
92 unmodified lines
223 new_contents = rewrite_blueprint_imports(new_contents)224225 for pattern, replacement in _blueprint_related:226 new_contents = pattern.sub(replacement, new_contents)197 return new_contents227 return new_contents, dict(modules_declared)228229230def upgrade_python_file(filename, contents, teardown):201 new_contents = fix_url_for(contents)231 new_contents = contents232 if teardown:233 new_contents = fix_teardown_funcs(new_contents)204 new_contents = rewrite_for_blueprints(new_contents, filename)234 new_contents, modules = rewrite_for_blueprints(new_contents, filename)235 new_contents = fix_url_for(new_contents, modules)236 new_contents = _error_handler_re.sub('\\1.error_handler_spec[None][\\2]',237 new_contents)238 make_diff(filename, contents, new_contents)239240241def upgrade_template_file(filename, contents):211 new_contents = fix_url_for(contents)242 new_contents = fix_url_for(contents, None)243 make_diff(filename, contents, new_contents)244245246def walk_path(path):
53 unmodified lines