From f75821c2bc1e94394b6af03daa460cef864d4213 Mon Sep 17 00:00:00 2001 From: Sylvain LE GAL Date: Fri, 1 Mar 2024 12:57:05 +0100 Subject: [PATCH] [FIX] Do not crash silently, when a repos.yml file reference an empty addons folder. Step to reproduce : - run a V15 instance - add a reference to 'OCA/geospatial' repo that doesn't contain any odoo module. As a result, Odoo will exit with the following error: odoo-bin: error: option --addons-path: the path '/odoo_env/src/OCA/geospatial' is not a valid addons directory To avoid such problem, and avoid to have to remove empty repository (that could become non empty in the future, and contains some migrations scripts), - we reimplement a version of the odoo function _is_addons_path (odoo/odoo/tools/config.py) - we add an info log : Skipping addons path '.../src/env_15.0/src/OCA/geospatial' because it doesn't contain any odoo module. --- .../configuration_version_dependant.py | 9 ++++ odoo_openupgrade_wizard/tools/tools_odoo.py | 48 ++++++++++++++----- .../tools/tools_odoo_module.py | 6 +-- 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/odoo_openupgrade_wizard/configuration_version_dependant.py b/odoo_openupgrade_wizard/configuration_version_dependant.py index 3fe534f..55e98e4 100644 --- a/odoo_openupgrade_wizard/configuration_version_dependant.py +++ b/odoo_openupgrade_wizard/configuration_version_dependant.py @@ -102,6 +102,15 @@ def get_base_module_folder(migration_step: dict) -> str: return "openerp" +def get_manifest_name(migration_step: dict) -> str: + """return the name of the manifest file present in + each odoo module""" + if migration_step["version"] >= 10.0: + return "__manifest__.py" + + return "__openerp__.py" + + def skip_addon_path(migration_step: dict, path: Path) -> bool: """return a boolean to indicate if the addon_path should be remove (during the generation of the addons_path). diff --git a/odoo_openupgrade_wizard/tools/tools_odoo.py b/odoo_openupgrade_wizard/tools/tools_odoo.py index 3f0a464..083524c 100644 --- a/odoo_openupgrade_wizard/tools/tools_odoo.py +++ b/odoo_openupgrade_wizard/tools/tools_odoo.py @@ -11,6 +11,7 @@ from loguru import logger from odoo_openupgrade_wizard.configuration_version_dependant import ( get_base_module_folder, + get_manifest_name, get_odoo_folder, get_odoo_run_command, get_server_wide_modules_upgrade, @@ -47,8 +48,17 @@ def get_repo_file_path(ctx, odoo_version: float) -> Path: def get_odoo_addons_path( - ctx, root_path: Path, migration_step: dict, execution_context: str = False + ctx, + odoo_env_path: Path, + migration_step: dict, + execution_context: str = False, ) -> str: + """Return + - addons_path: a list of Path of that contains odoo module + for the current migration_step, + based on the analysis of the repos.yml file + - empty_addons_path: a list of Path of empty folders. + (without any odoo module)""" repo_file = get_repo_file_path(ctx, migration_step["version"]) base_module_folder = get_base_module_folder(migration_step) stream = open(repo_file, "r") @@ -56,9 +66,10 @@ def get_odoo_addons_path( data = data addons_path = [] + empty_addons_path = [] odoo_folder = get_odoo_folder(migration_step, execution_context) for key in data.keys(): - path = root_path / Path(key) + path = Path(key) if str(path).endswith(odoo_folder): # Add two folder for odoo folder addons_path.append(path / Path("addons")) @@ -67,10 +78,21 @@ def get_odoo_addons_path( ) elif skip_addon_path(migration_step, path): pass - else: + elif is_addons_path(ctx, odoo_env_path / path, migration_step): addons_path.append(path) + else: + empty_addons_path.append(path) - return addons_path + return addons_path, empty_addons_path + + +def is_addons_path(ctx, path: Path, migration_step: dict): + for folder in [x for x in path.iterdir() if x.is_dir()]: + if (folder / "__init__.py").exists() and ( + folder / get_manifest_name(migration_step) + ).exists(): + return True + return False def get_odoo_env_path(ctx, odoo_version: float) -> Path: @@ -123,14 +145,18 @@ def generate_odoo_command( ) # compute 'addons_path' - addons_path = ",".join( - [ - str(x) - for x in get_odoo_addons_path( - ctx, Path("/odoo_env"), migration_step, execution_context - ) - ] + addons_path_list, empty_addons_path_list = get_odoo_addons_path( + ctx, odoo_env_path, migration_step, execution_context ) + addons_path = ",".join( + [str(Path("/odoo_env") / x) for x in addons_path_list] + ) + for empty_addons_path in empty_addons_path_list: + logger.info( + "Skipping addons path" + f" '{(odoo_env_path / empty_addons_path).resolve()}'" + " because it doesn't contain any odoo module." + ) # compute 'log_file' log_file_name = "{}____{}.log".format( diff --git a/odoo_openupgrade_wizard/tools/tools_odoo_module.py b/odoo_openupgrade_wizard/tools/tools_odoo_module.py index 9d6e4dc..14139d8 100644 --- a/odoo_openupgrade_wizard/tools/tools_odoo_module.py +++ b/odoo_openupgrade_wizard/tools/tools_odoo_module.py @@ -379,14 +379,14 @@ class OdooModule(object): """ # Try to find the repository that contains the module main_path = get_odoo_env_path(ctx, current_version) - addons_path = get_odoo_addons_path( + addons_path, _ = get_odoo_addons_path( ctx, main_path, {"version": current_version, "execution_context": "openupgrade"}, ) for addon_path in addons_path: - if (addon_path / module_name).exists(): - return addon_path + if (main_path / addon_path / module_name).exists(): + return main_path / addon_path return False