From d23804f4b49802b94839628d1f0334040c143610 Mon Sep 17 00:00:00 2001 From: Sylvain LE GAL Date: Tue, 21 Jun 2022 13:03:31 +0200 Subject: [PATCH] [IMP] allow to call run with a specific execution_context --- .../cli_generate_module_analysis.py | 7 ++-- odoo_openupgrade_wizard/cli_init.py | 6 ++-- odoo_openupgrade_wizard/cli_run.py | 11 +++++- .../configuration_version_dependant.py | 13 +++++-- odoo_openupgrade_wizard/templates.py | 2 +- odoo_openupgrade_wizard/tools_odoo.py | 36 ++++++++++++------- odoo_openupgrade_wizard/tools_odoo_module.py | 10 +++++- tests/data/output_expected/config.yml | 6 ++-- 8 files changed, 64 insertions(+), 27 deletions(-) diff --git a/odoo_openupgrade_wizard/cli_generate_module_analysis.py b/odoo_openupgrade_wizard/cli_generate_module_analysis.py index 2750ce8..4521da4 100644 --- a/odoo_openupgrade_wizard/cli_generate_module_analysis.py +++ b/odoo_openupgrade_wizard/cli_generate_module_analysis.py @@ -57,9 +57,6 @@ def generate_module_analysis(ctx, step, database, modules): modules = modules and modules.split(",") or [] - # Force to be in openupgrade mode - initial_step["action"] = final_step["action"] = "upgrade" - try: # INITIAL : Run odoo and install analysis module run_odoo( @@ -68,6 +65,7 @@ def generate_module_analysis(ctx, step, database, modules): database=initial_database, detached_container=False, stop_after_init=True, + execution_context="openupgrade", init=get_upgrade_analysis_module(initial_step), ) @@ -76,6 +74,7 @@ def generate_module_analysis(ctx, step, database, modules): ctx, initial_step, database=initial_database, + execution_context="openupgrade", detached_container=True, ) # INITIAL : install modules to analyse and generate records @@ -96,6 +95,7 @@ def generate_module_analysis(ctx, step, database, modules): detached_container=False, stop_after_init=True, init=get_upgrade_analysis_module(final_step), + execution_context="openupgrade", alternative_xml_rpc_port=alternative_xml_rpc_port, ) @@ -109,6 +109,7 @@ def generate_module_analysis(ctx, step, database, modules): database=final_database, detached_container=True, alternative_xml_rpc_port=alternative_xml_rpc_port, + execution_context="openupgrade", links={initial_container.name: odoo_initial_host_name}, ) diff --git a/odoo_openupgrade_wizard/cli_init.py b/odoo_openupgrade_wizard/cli_init.py index bd94cd3..43902a4 100644 --- a/odoo_openupgrade_wizard/cli_init.py +++ b/odoo_openupgrade_wizard/cli_init.py @@ -74,7 +74,7 @@ def init( steps = [ { "name": 1, - "action": "update", + "execution_context": "regular", "release": odoo_versions[0]["release"], "complete_name": "step_01__update__%s" % (odoo_versions[0]["release"]), @@ -87,7 +87,7 @@ def init( steps.append( { "name": step_nbr, - "action": "upgrade", + "execution_context": "openupgrade", "release": odoo_version["release"], "complete_name": "step_%s__upgrade__%s" % (str(step_nbr).rjust(2, "0"), odoo_version["release"]), @@ -100,7 +100,7 @@ def init( steps.append( { "name": step_nbr, - "action": "update", + "execution_context": "regular", "release": odoo_versions[-1]["release"], "complete_name": "step_%s__update__%s" % (str(step_nbr).rjust(2, "0"), odoo_versions[-1]["release"]), diff --git a/odoo_openupgrade_wizard/cli_run.py b/odoo_openupgrade_wizard/cli_run.py index 5b60008..e922270 100644 --- a/odoo_openupgrade_wizard/cli_run.py +++ b/odoo_openupgrade_wizard/cli_run.py @@ -27,8 +27,16 @@ from odoo_openupgrade_wizard.tools_postgres import ensure_database type=str, help="List of modules to install. Equivalent to -i odoo options.", ) +@click.option( + "-e", + "--execution-context", + type=click.Choice(["regular", "openupgrade"]), + help="Force to use an openupgrade (OCA/openupgrade)" + " or a regular (odoo/odoo or OCA/OCB) base code when running odoo." + " Let empty to use the defaut execution of the migration step.", +) @click.pass_context -def run(ctx, step, database, stop_after_init, init_modules): +def run(ctx, step, database, stop_after_init, init_modules, execution_context): migration_step = get_migration_step_from_options(ctx, step) ensure_database(ctx, database, state="present") @@ -40,6 +48,7 @@ def run(ctx, step, database, stop_after_init, init_modules): detached_container=not stop_after_init, init=init_modules, stop_after_init=stop_after_init, + execution_context=execution_context, ) if not stop_after_init: logger.info( diff --git a/odoo_openupgrade_wizard/configuration_version_dependant.py b/odoo_openupgrade_wizard/configuration_version_dependant.py index 33a0d8f..675f74c 100644 --- a/odoo_openupgrade_wizard/configuration_version_dependant.py +++ b/odoo_openupgrade_wizard/configuration_version_dependant.py @@ -89,11 +89,18 @@ def get_odoo_run_command(migration_step: dict) -> str: return "odoo.py" -def get_odoo_folder(migration_step: dict) -> str: +def get_odoo_folder( + migration_step: dict, execution_context: str = False +) -> str: """return the main odoo folder, depending on the migration step. (./src/odoo, ./src/openupgrade, ...)""" - if migration_step["action"] == "update": + if execution_context == "regular": + return "src/odoo" + elif execution_context == "openupgrade" and migration_step["release"] < 14: + return "src/openupgrade" + + if migration_step["execution_context"] == "regular": return "src/odoo" if migration_step["release"] >= 14.0: @@ -126,7 +133,7 @@ def get_server_wide_modules_upgrade(migration_step: dict) -> list: """return a list of modules to load, depending on the migration step.""" if ( migration_step["release"] >= 14.0 - and migration_step["action"] == "upgrade" + and migration_step["execution_context"] == "openupgrade" ): return ["openupgrade_framework"] return [] diff --git a/odoo_openupgrade_wizard/templates.py b/odoo_openupgrade_wizard/templates.py index c9c7fa4..94a8e60 100644 --- a/odoo_openupgrade_wizard/templates.py +++ b/odoo_openupgrade_wizard/templates.py @@ -17,7 +17,7 @@ migration_steps: {% for step in steps %} - name: {{ step['name'] }} release: {{ step['release'] }} - action: {{ step['action'] }} + execution_context: {{ step['execution_context'] }} complete_name: {{ step['complete_name'] }} {% endfor %} diff --git a/odoo_openupgrade_wizard/tools_odoo.py b/odoo_openupgrade_wizard/tools_odoo.py index e4efc00..77a1a22 100644 --- a/odoo_openupgrade_wizard/tools_odoo.py +++ b/odoo_openupgrade_wizard/tools_odoo.py @@ -20,7 +20,9 @@ from odoo_openupgrade_wizard.tools_postgres import get_postgres_container from odoo_openupgrade_wizard.tools_system import get_script_folder -def get_odoo_addons_path(ctx, root_path: Path, migration_step: dict) -> str: +def get_odoo_addons_path( + ctx, root_path: Path, migration_step: dict, execution_context: str = False +) -> str: odoo_version = get_odoo_version_from_migration_step(ctx, migration_step) repo_file = get_odoo_env_path(ctx, odoo_version) / Path("repos.yml") base_module_folder = get_base_module_folder(migration_step) @@ -31,7 +33,9 @@ def get_odoo_addons_path(ctx, root_path: Path, migration_step: dict) -> str: addons_path = [] for key in data.keys(): path = root_path / Path(key) - if str(path).endswith(get_odoo_folder(migration_step)): + if str(path).endswith( + get_odoo_folder(migration_step, execution_context) + ): # Add two folder for odoo folder addons_path.append(path / Path("addons")) addons_path.append( @@ -79,6 +83,7 @@ def get_odoo_version_from_migration_step(ctx, migration_step: dict) -> dict: def generate_odoo_command( ctx, migration_step: dict, + execution_context: str, database: str, update: str, init: str, @@ -94,7 +99,7 @@ def generate_odoo_command( demo_cmd = not demo and "--without-demo all" or "" command = ( Path("/odoo_env") - / Path(get_odoo_folder(migration_step)) + / Path(get_odoo_folder(migration_step, execution_context)) / Path(get_odoo_run_command(migration_step)) ) result = ( @@ -110,7 +115,9 @@ def generate_odoo_command( return result -def generate_odoo_config_file(ctx, migration_step, log_file): +def generate_odoo_config_file( + ctx, migration_step, log_file, execution_context +): """Create a config file name _auto_generated_odoo.cfg in the according environment (defined by migration_step) This configuration file is a merge of the odoo.cfg file that can @@ -133,7 +140,7 @@ def generate_odoo_config_file(ctx, migration_step, log_file): [ str(x) for x in get_odoo_addons_path( - ctx, Path("/odoo_env"), migration_step + ctx, Path("/odoo_env"), migration_step, execution_context ) ] ) @@ -173,6 +180,7 @@ def run_odoo( stop_after_init: bool = False, shell: bool = False, demo: bool = False, + execution_context: str = False, alternative_xml_rpc_port: int = False, links: dict = {}, ): @@ -180,13 +188,12 @@ def run_odoo( get_postgres_container(ctx) logger.info( "Launching Odoo Container (Release {release}) for {db_text}" - " in {action} mode. Demo Data is {demo_text}" + " in {execution_context} mode. Demo Data is {demo_text}" " {stop_text} {init_text} {update_text}".format( release=migration_step["release"], db_text=database and "database '%s'" % database or "any databases", - action=migration_step["action"] == "update" - and "regular" - or "OpenUpgrade", + execution_context=execution_context + or migration_step["execution_context"], demo_text=demo and "enabled" or "disabled", stop_text=stop_after_init and " (stop-after-init)" or "", init_text=init and " (Init : %s)" % init or "", @@ -199,11 +206,12 @@ def run_odoo( log_file = "/env/log/{}____{}.log".format( ctx.obj["log_prefix"], migration_step["complete_name"] ) - generate_odoo_config_file(ctx, migration_step, log_file) + generate_odoo_config_file(ctx, migration_step, log_file, execution_context) command = generate_odoo_command( ctx, migration_step, + execution_context, database=database, update=update, init=init, @@ -240,7 +248,11 @@ def kill_odoo(ctx, migration_step: dict): def execute_click_odoo_python_files( - ctx, database: str, migration_step: dict, python_files: list = [] + ctx, + database: str, + migration_step: dict, + python_files: list = [], + execution_context: str = False, ): if not python_files: @@ -264,7 +276,7 @@ def execute_click_odoo_python_files( log_file = "/env/log/{}____{}__post_migration.log".format( ctx.obj["log_prefix"], migration_step["complete_name"] ) - generate_odoo_config_file(ctx, migration_step, log_file) + generate_odoo_config_file(ctx, migration_step, log_file, execution_context) for python_file in python_files: # TODO, check if we should set python2 for old version of Odoo diff --git a/odoo_openupgrade_wizard/tools_odoo_module.py b/odoo_openupgrade_wizard/tools_odoo_module.py index c9ebf08..19c377b 100644 --- a/odoo_openupgrade_wizard/tools_odoo_module.py +++ b/odoo_openupgrade_wizard/tools_odoo_module.py @@ -173,6 +173,11 @@ class Analysis(object): apriori_module_path = OdooModule.get_addon_path( ctx, apriori_module_name, current_release ) + if not apriori_module_path: + raise ValueError( + "Unable to find the path of the module %s for the release %s" + % (apriori_module_name, current_release) + ) apriori_absolute_path = ( apriori_module_path / Path(apriori_module_name) @@ -345,11 +350,14 @@ class OdooModule(object): # Try to find the repository that contains the module main_path = get_odoo_env_path(ctx, {"release": current_release}) addons_path = get_odoo_addons_path( - ctx, main_path, {"release": current_release, "action": "upgrade"} + ctx, + main_path, + {"release": current_release, "execution_context": "openupgrade"}, ) for addon_path in addons_path: if (addon_path / module_name).exists(): return addon_path + return False @classmethod diff --git a/tests/data/output_expected/config.yml b/tests/data/output_expected/config.yml index ff05a0d..6ebcf2e 100644 --- a/tests/data/output_expected/config.yml +++ b/tests/data/output_expected/config.yml @@ -19,17 +19,17 @@ migration_steps: - name: 1 release: 13.0 - action: update + execution_context: regular complete_name: step_01__update__13.0 - name: 2 release: 14.0 - action: upgrade + execution_context: openupgrade complete_name: step_02__upgrade__14.0 - name: 3 release: 14.0 - action: update + execution_context: regular complete_name: step_03__update__14.0