From d595c6584c530565cfe51fd4fb601ffaaed9062f Mon Sep 17 00:00:00 2001 From: Simon Maillard Date: Wed, 4 Dec 2024 14:23:24 +0000 Subject: [PATCH 1/3] =?UTF-8?q?[IMP]=C2=A0split=20generate=5Fodoo=5Fcomman?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In 2 functions: - generate_odoo_command_options - generate_odoo_command To be able to use generate_odoo_command_options from other places --- odoo_openupgrade_wizard/tools/tools_odoo.py | 105 ++++++++++++-------- 1 file changed, 64 insertions(+), 41 deletions(-) diff --git a/odoo_openupgrade_wizard/tools/tools_odoo.py b/odoo_openupgrade_wizard/tools/tools_odoo.py index 2cd3093..f63a287 100644 --- a/odoo_openupgrade_wizard/tools/tools_odoo.py +++ b/odoo_openupgrade_wizard/tools/tools_odoo.py @@ -121,7 +121,7 @@ def get_docker_container_name(ctx, database: str, migration_step: dict) -> str: ) -def generate_odoo_command( +def generate_odoo_command_options( ctx, migration_step: dict, execution_context: str, @@ -130,13 +130,13 @@ def generate_odoo_command( update: str = False, init: str = False, stop_after_init: bool = False, - shell: bool = False, -) -> str: +) -> list: + """ + Generate Odoo command options as a string to append to any command. + """ odoo_env_path = get_odoo_env_path(ctx, migration_step["version"]) # Compute 'server_wide_modules' - # For that purpose, read the custom odoo.conf file - # to know if server_wide_modules is defined custom_odoo_config_file = odoo_env_path / "odoo.conf" parser = configparser.RawConfigParser() parser.read(custom_odoo_config_file) @@ -147,7 +147,7 @@ def generate_odoo_command( migration_step, execution_context ) - # compute 'addons_path' + # Compute 'addons_path' addons_path_list, empty_addons_path_list = get_odoo_addons_path( ctx, odoo_env_path, migration_step, execution_context ) @@ -161,49 +161,72 @@ def generate_odoo_command( " because it doesn't contain any odoo module." ) - # compute 'log_file' - log_file_name = "{}____{}.log".format( - ctx.obj["log_prefix"], migration_step["complete_name"] + # Compute 'log_file' + log_file_name = ( + f"{ctx.obj['log_prefix']}____{migration_step['complete_name']}.log" ) - log_file_docker_path = "/env/log/%s" % log_file_name + log_file_docker_path = f"/env/log/{log_file_name}" - database_cmd = database and "--database %s" % database or "" - load_cmd = ( - server_wide_modules - and "--load %s" % ",".join(server_wide_modules) - or "" + # Build options string + options = [ + "--config=/odoo_env/odoo.conf", + "--data-dir=/env/filestore/", + f"--addons-path={addons_path}", + f"--logfile={log_file_docker_path}", + "--db_host=db", + "--db_port=5432", + "--db_user=odoo", + "--db_password=odoo", + "--workers=0", + f"{'--without-demo=all' if not demo else ''}", + f"{'--load ' + ','.join(server_wide_modules) if server_wide_modules else ''}", # noqa + f"{'--database=' + database if database else ''}", + f"{'--update ' + update if update else ''}", + f"{'--init ' + init if init else ''}", + f"{'--stop-after-init' if stop_after_init else ''}", + ] + + # remove empty strings + return [x for x in options if x] + + +def generate_odoo_command( + ctx, + migration_step: dict, + execution_context: str, + database: str, + demo: bool = False, + update: str = False, + init: str = False, + stop_after_init: bool = False, + shell: bool = False, +) -> str: + """ + Generate the full Odoo command using options from + generate_odoo_command_options. + """ + options = generate_odoo_command_options( + ctx, + migration_step, + execution_context, + database, + demo, + update, + init, + stop_after_init, ) - update_cmd = update and "--update %s" % update or "" - init_cmd = init and "--init %s" % init or "" - stop_after_init_cmd = stop_after_init and "--stop-after-init" or "" - shell_cmd = shell and "shell" or "" - demo_cmd = not demo and "--without-demo all" or "" - command = ( + + base_command = ( Path("/odoo_env") / Path(get_odoo_folder(migration_step, execution_context)) / Path(get_odoo_run_command(migration_step)) ) - result = ( - f" {command}" - f" {shell_cmd}" - f" --config=/odoo_env/odoo.conf" - f" --data-dir=/env/filestore/" - f" --addons-path={addons_path}" - f" --logfile={log_file_docker_path}" - f" --db_host=db" - f" --db_port=5432" - f" --db_user=odoo" - f" --db_password=odoo" - f" --workers=0" - f" {demo_cmd}" - f" {load_cmd}" - f" {database_cmd}" - f" {update_cmd}" - f" {init_cmd}" - f" {stop_after_init_cmd}" - ) - return result + options_as_string = " ".join(options) + if shell: + return f"{base_command} shell {options_as_string}" + else: + return f"{base_command} {options_as_string}" def run_odoo( From 95f58f3a9f02caed3c7c9bc3bbc19df4d6c66573 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Taymans?= Date: Fri, 31 Jan 2025 13:14:43 +0100 Subject: [PATCH 2/3] [FIX] generate_odoo_command_options docstring --- odoo_openupgrade_wizard/tools/tools_odoo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/odoo_openupgrade_wizard/tools/tools_odoo.py b/odoo_openupgrade_wizard/tools/tools_odoo.py index f63a287..56e33cf 100644 --- a/odoo_openupgrade_wizard/tools/tools_odoo.py +++ b/odoo_openupgrade_wizard/tools/tools_odoo.py @@ -132,7 +132,7 @@ def generate_odoo_command_options( stop_after_init: bool = False, ) -> list: """ - Generate Odoo command options as a string to append to any command. + Generate Odoo command options as a list of string to append to any command. """ odoo_env_path = get_odoo_env_path(ctx, migration_step["version"]) From a960ddc83c9c16fcf4fa0c9a3aa82c7ba05de214 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Taymans?= Date: Fri, 31 Jan 2025 13:17:55 +0100 Subject: [PATCH 3/3] [ADD] newsfragments --- newsfragments/+refactoring.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 newsfragments/+refactoring.misc diff --git a/newsfragments/+refactoring.misc b/newsfragments/+refactoring.misc new file mode 100644 index 0000000..aa5edbe --- /dev/null +++ b/newsfragments/+refactoring.misc @@ -0,0 +1 @@ +Refactoring of the function tools.tools_odoo.generate_odoo_command.