From aa863efd9294dcd8afef6e5fa590ae2f88cbb0a2 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sun, 5 Jun 2011 16:00:19 +0200 Subject: [PATCH] Don't scan the whole file contents for template detection --- scripts/flask-07-upgrade.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/scripts/flask-07-upgrade.py b/scripts/flask-07-upgrade.py index 5398584e..216db1bf 100644 --- a/scripts/flask-07-upgrade.py +++ b/scripts/flask-07-upgrade.py @@ -33,6 +33,9 @@ except ImportError: ast = None +TEMPLATE_LOOKAHEAD = 4096 + + _from_import_re = re.compile(r'^\s*from flask import\s+') _string_re_part = r"('([^'\\]*(?:\\.[^'\\]*)*)'" \ r'|"([^"\\]*(?:\\.[^"\\]*)*)")' @@ -50,11 +53,6 @@ _blueprint_related = [ ] -def error(message): - print >> sys.stderr, 'error:', message - sys.exit(1) - - def make_diff(filename, old, new): for line in difflib.unified_diff(old.splitlines(), new.splitlines(), posixpath.normpath(posixpath.join('a', filename)), @@ -253,7 +251,7 @@ def walk_path(path): yield filename, 'python' else: with open(filename) as f: - contents = f.read() + contents = f.read(TEMPLATE_LOOKAHEAD) if '{% for' or '{% if' or '{{ url_for' in contents: yield filename, 'template' @@ -286,11 +284,6 @@ def autodetect_template_bundles(paths): def main(): """Entrypoint""" - if ast is None: - error('Python 2.6 or later is required to run the upgrade script.\n' - 'The runtime requirements for Flask 0.7 however are still ' - 'Python 2.5.') - parser = OptionParser() parser.add_option('-T', '--no-teardown-detection', dest='no_teardown', action='store_true', help='Do not attempt to ' @@ -307,6 +300,11 @@ def main(): if not args: args = ['.'] + if ast is None: + parser.error('Python 2.6 or later is required to run the upgrade script.\n' + 'The runtime requirements for Flask 0.7 however are still ' + 'Python 2.5.') + if options.no_bundled_tmpl: template_bundles = False elif options.bundled_tmpl: