From 2cafc5462d9b58c4a19a336c8b79d55bb9166913 Mon Sep 17 00:00:00 2001 From: Alexandre Flament Date: Thu, 1 Oct 2020 16:37:19 +0200 Subject: [PATCH 1/2] [fix] revert PR #2232 and #2230 --- docs/conf.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index ad884fd03..66c20594d 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -28,11 +28,8 @@ numfig = True exclude_patterns = ['build-templates/*.rst'] from searx import webapp -import searx.engines -searx.engines.initialize_engines(searx.settings['engines']) jinja_contexts = { 'webapp': dict(**webapp.__dict__), - 'engines': searx.engines.engines } # usage:: lorem :patch:`f373169` ipsum From 6c39917c4d3e3d3d6ced3fc14d1c78842fec3a19 Mon Sep 17 00:00:00 2001 From: Alexandre Flament Date: Thu, 1 Oct 2020 14:18:00 +0200 Subject: [PATCH 2/2] [mod] webapp.py: update engines initialization condition Always call initialize engines except on the first run of werkzeug with the reload feature. the reload feature is activated when: * searx_debug is True (SEARX_DEBUG environment variable or settings.yml) * FLASK_APP=searx/webapp.py FLASK_ENV=development flask run (see https://flask.palletsprojects.com/en/1.1.x/cli/ ) Fix SEARX_DEBUG=0 make docs docs/admin/engines.rst : engines are initialized See https://github.com/searx/searx/issues/2204#issuecomment-701373438 --- searx/webapp.py | 20 ++++++++++++++++---- searx/webutils.py | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/searx/webapp.py b/searx/webapp.py index 5723734c1..c19d3f3cc 100755 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -65,7 +65,7 @@ from searx.engines import ( from searx.webutils import ( UnicodeWriter, highlight_content, get_resources_directory, get_static_files, get_result_templates, get_themes, - prettify_url, new_hmac + prettify_url, new_hmac, is_flask_run_cmdline ) from searx.webadapter import get_search_query_from_webapp, get_selected_categories from searx.utils import html_to_text, gen_useragent, dict_subset, match_language @@ -114,9 +114,21 @@ app.jinja_env.lstrip_blocks = True app.jinja_env.add_extension('jinja2.ext.loopcontrols') app.secret_key = settings['server']['secret_key'] -if not searx_debug \ - or os.environ.get("WERKZEUG_RUN_MAIN") == "true" \ - or os.environ.get('UWSGI_ORIGINAL_PROC_NAME') is not None: +# see https://flask.palletsprojects.com/en/1.1.x/cli/ +# True if "FLASK_APP=searx/webapp.py FLASK_ENV=development flask run" +flask_run_development = \ + os.environ.get("FLASK_APP") is not None\ + and os.environ.get("FLASK_ENV") == 'development'\ + and is_flask_run_cmdline() + +# True if reload feature is activated of werkzeug, False otherwise (including uwsgi, etc..) +# __name__ != "__main__" if searx.webapp is imported (make test, make docs, uwsgi...) +# see run() at the end of this file : searx_debug activates the reload feature. +werkzeug_reloader = flask_run_development or (searx_debug and __name__ == "__main__") + +# initialize the engines except on the first run of the werkzeug server. +if not werkzeug_reloader\ + or (werkzeug_reloader and os.environ.get("WERKZEUG_RUN_MAIN") == "true"): initialize_engines(settings['engines']) babel = Babel(app) diff --git a/searx/webutils.py b/searx/webutils.py index 2900c0edd..bf50dbc25 100644 --- a/searx/webutils.py +++ b/searx/webutils.py @@ -4,6 +4,7 @@ import csv import hashlib import hmac import re +import inspect from io import StringIO from codecs import getincrementalencoder @@ -125,3 +126,18 @@ def highlight_content(content, query): content, flags=re.I | re.U) return content + + +def is_flask_run_cmdline(): + """Check if the application was started using "flask run" command line + + Inspect the callstack. + See https://github.com/pallets/flask/blob/master/src/flask/__main__.py + + Returns: + bool: True if the application was started using "flask run". + """ + frames = inspect.stack() + if len(frames) < 2: + return False + return frames[-2].filename.endswith('flask/cli.py')