From 986e8bc7666502c60b1fdd2a729e5c531c49b491 Mon Sep 17 00:00:00 2001 From: Akhil Narang Date: Fri, 2 Feb 2024 12:35:57 +0530 Subject: [PATCH] feat: include only installed apps in module map in certain scenarios "all" apps are only really needed during setup, installation of new apps, etc. Signed-off-by: Akhil Narang --- frappe/__init__.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/frappe/__init__.py b/frappe/__init__.py index 0978d469cd..694cfb97de 100644 --- a/frappe/__init__.py +++ b/frappe/__init__.py @@ -25,6 +25,7 @@ from typing import TYPE_CHECKING, Any, Literal, Optional, TypeAlias, overload import click from werkzeug.local import Local, release_local +import frappe from frappe.query_builder import ( get_query, get_query_builder, @@ -298,7 +299,7 @@ def init(site: str, sites_path: str = ".", new_site: bool = False, force=False) local.qb = get_query_builder(local.conf.db_type) local.qb.get_query = get_query setup_redis_cache_connection() - setup_module_map() + setup_module_map(include_all_apps=not (frappe.request or frappe.job or frappe.flags.in_migrate)) if not _qb_patched.get(local.conf.db_type): patch_query_execute() @@ -1684,7 +1685,7 @@ def append_hook(target, key, value): target[key].extend(value) -def setup_module_map(): +def setup_module_map(include_all_apps=True): """Rebuild map of all modules (internal).""" if conf.db_name: local.app_modules = cache.get_value("app_modules") @@ -1692,10 +1693,18 @@ def setup_module_map(): if not (local.app_modules and local.module_app): local.module_app, local.app_modules = {}, {} - for app in get_all_apps(with_internal_apps=True): + if include_all_apps: + apps = get_all_apps(with_internal_apps=True) + else: + apps = get_installed_apps(_ensure_on_bench=True) + for app in apps: local.app_modules.setdefault(app, []) for module in get_module_list(app): module = scrub(module) + if module in local.module_app: + print( + f"WARNING: module `{module}` found in apps `{local.module_app[module]}` and `{app}`" + ) local.module_app[module] = app local.app_modules[app].append(module)