From deabde4b8f76e97f156ce04ee7afcc0f3ee31e9d Mon Sep 17 00:00:00 2001 From: Sylvain LE GAL Date: Tue, 30 Mar 2021 11:32:04 +0200 Subject: [PATCH] [FIX+IMP] module_analysis : remove analysis when updating modules, because the analysis is partial (it also make the update slower) ; Add instead a cron task that is executed nightly to update analysis automatically --- module_analysis/__manifest__.py | 5 ++++- module_analysis/data/ir_cron.xml | 20 ++++++++++++++++++++ module_analysis/models/ir_module_module.py | 15 ++++++++++----- module_analysis/post_init_hook.py | 9 ++++++--- module_analysis/readme/CONFIGURE.rst | 8 +++++++- module_analysis/tests/test_module.py | 6 +++--- 6 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 module_analysis/data/ir_cron.xml diff --git a/module_analysis/__manifest__.py b/module_analysis/__manifest__.py index 877dee51e..151be2463 100644 --- a/module_analysis/__manifest__.py +++ b/module_analysis/__manifest__.py @@ -20,11 +20,14 @@ "views/view_ir_module_type.xml", "views/view_ir_module_type_rule.xml", "views/view_ir_module_module.xml", + "data/ir_cron.xml", "data/ir_config_parameter.xml", "data/ir_module_type.xml", "data/ir_module_type_rule.xml", ], - "external_dependencies": {"python": ["pygount"]}, + "external_dependencies": { + "python": ["pygount"], + }, "post_init_hook": "analyse_installed_modules", "installable": True, } diff --git a/module_analysis/data/ir_cron.xml b/module_analysis/data/ir_cron.xml new file mode 100644 index 000000000..73e360f2e --- /dev/null +++ b/module_analysis/data/ir_cron.xml @@ -0,0 +1,20 @@ + + + + + Update Module Analysis + + + code + model.cron_analyse_code() + 1 + days + + -1 + + + diff --git a/module_analysis/models/ir_module_module.py b/module_analysis/models/ir_module_module.py index 4f36fa517..9e932b4af 100644 --- a/module_analysis/models/ir_module_module.py +++ b/module_analysis/models/ir_module_module.py @@ -79,14 +79,12 @@ class IrModuleModule(models.Model): def update_list(self): res = super().update_list() if self.env.context.get("analyse_installed_modules", False): - self.search([("state", "=", "installed")]).button_analyse_code() + self.search([("state", "=", "installed")])._analyse_code() return res def write(self, vals): res = super().write(vals) - if vals.get("state", False) == "installed": - self.button_analyse_code() - elif vals.get("state", False) == "uninstalled" and "module_analysis" not in [ + if vals.get("state", False) == "uninstalled" and "module_analysis" not in [ x.name for x in self ]: self.write(self._get_clean_analyse_values()) @@ -94,6 +92,14 @@ class IrModuleModule(models.Model): # Public Section def button_analyse_code(self): + self._analyse_code() + + @api.model + def cron_analyse_code(self): + self.search([("state", "=", "installed")])._analyse_code() + + # Custom Section + def _analyse_code(self): IrModuleAuthor = self.env["ir.module.author"] IrModuleTypeRule = self.env["ir.module.type.rule"] rules = IrModuleTypeRule.search([]) @@ -150,7 +156,6 @@ class IrModuleModule(models.Model): values[v["field"]] = v["value"] module.write(values) - # Custom Section @api.model def _get_files_to_analyse( self, path, file_extensions, exclude_directories, exclude_files diff --git a/module_analysis/post_init_hook.py b/module_analysis/post_init_hook.py index e59ee48fc..e509c55f3 100644 --- a/module_analysis/post_init_hook.py +++ b/module_analysis/post_init_hook.py @@ -6,6 +6,9 @@ from odoo import SUPERUSER_ID, api def analyse_installed_modules(cr, registry): - env = api.Environment(cr, SUPERUSER_ID, {}) - installed_modules = env["ir.module.module"].search([("state", "=", "installed")]) - installed_modules.button_analyse_code() + with api.Environment.manage(): + env = api.Environment(cr, SUPERUSER_ID, {}) + installed_modules = env["ir.module.module"].search( + ["|", ("state", "=", "installed"), ("name", "=", "module_analysis")] + ) + installed_modules.button_analyse_code() diff --git a/module_analysis/readme/CONFIGURE.rst b/module_analysis/readme/CONFIGURE.rst index 527b149d6..f580339eb 100644 --- a/module_analysis/readme/CONFIGURE.rst +++ b/module_analysis/readme/CONFIGURE.rst @@ -14,7 +14,7 @@ developped for exemple, .. image:: ../static/description/add_module_type_rules.png -to update the data, you have to : +to update the data manually, you have to : * Go to 'Apps' / 'Update Apps List' @@ -24,6 +24,12 @@ to update the data, you have to : This will update analysis of your installed modules. +to update the data automatically, you have to : + +* Go to 'Settings' / 'Technical' / 'Scheduled Actions' + +* Configure the action 'Update Module Analysis'. (By default, the analysis will be done nightly) + Adding Extra data ~~~~~~~~~~~~~~~~~ diff --git a/module_analysis/tests/test_module.py b/module_analysis/tests/test_module.py index f84cf920e..2908d0f95 100644 --- a/module_analysis/tests/test_module.py +++ b/module_analysis/tests/test_module.py @@ -2,11 +2,11 @@ # @author: Sylvain LE GAL (https://twitter.com/legalsylvain) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from odoo.tests import tagged -from odoo.tests.common import TransactionCase +from odoo.tests.common import TransactionCase, at_install, post_install -@tagged("post_install", "-at_install") +@at_install(False) +@post_install(True) class TestModule(TransactionCase): def setUp(self): super().setUp()