142 lines
5.7 KiB
Python
142 lines
5.7 KiB
Python
from services.git.handler import GitHandler
|
|
from services.odoo.connection import OdooConnection
|
|
import lib.color_log as color_log
|
|
|
|
|
|
class OdooModuleManager:
|
|
def __init__(self, config_path: str = "config/settings.yaml"):
|
|
self.config = OdooConnection(config_path)
|
|
|
|
def _manage_module(
|
|
self, action: str, instance_name: str = None, module_names: list = None
|
|
) -> None:
|
|
"""Generic method to install, uninstall, or upgrade modules."""
|
|
if action not in {"install", "uninstall", "upgrade"}:
|
|
raise ValueError(f"Invalid action: {action}")
|
|
|
|
self.config.connect(instance_name)
|
|
|
|
for instance in self.config.get_instances():
|
|
if instance_name and instance["name"] != instance_name:
|
|
continue
|
|
color_log.Show("INFO", f"Processing instance: {instance['name']}")
|
|
for module_name in module_names:
|
|
try:
|
|
color_log.Show(
|
|
"INFO",
|
|
f"{action.capitalize()}ing module: {module_name} in {instance['name']}",
|
|
)
|
|
module_ids = self.config.execute(
|
|
instance["name"],
|
|
"ir.module.module",
|
|
"search",
|
|
[("name", "=", module_name)],
|
|
)
|
|
|
|
if not module_ids and action in ["upgrade", "uninstall"]:
|
|
color_log.Show(
|
|
"WARNING",
|
|
f"Module {module_name} not found in {instance['name']}, skipping.",
|
|
)
|
|
continue
|
|
|
|
button_action = f"button_immediate_{action}"
|
|
self.config.execute(
|
|
instance["name"],
|
|
"ir.module.module",
|
|
button_action,
|
|
module_ids, # Pass list directly instead of wrapping in another list
|
|
)
|
|
color_log.Show(
|
|
"OK",
|
|
f"Module {module_name} {action}ed successfully in {instance['name']}",
|
|
)
|
|
except Exception as e:
|
|
color_log.Show(
|
|
"FAILED",
|
|
f"Error while {action}ing {module_name} in {instance['name']}: {e}",
|
|
)
|
|
|
|
def get_modules(self, instance_name: str = None) -> list:
|
|
"""Get a list of installed modules for the specified instance(s)."""
|
|
self.config.connect(instance_name)
|
|
modules = []
|
|
for instance in self.config.get_instances():
|
|
if instance_name and instance["name"] != instance_name:
|
|
continue
|
|
color_log.Show("INFO", f"Fetching modules for instance: {instance['name']}")
|
|
if not instance.get("modules"):
|
|
color_log.Show(
|
|
"WARNING",
|
|
f"No modules found for instance {instance['name']}, skipping.",
|
|
)
|
|
continue
|
|
modules.extend(instance["modules"])
|
|
return modules
|
|
|
|
def install(self, instance_name: str = None, module_names: list = None) -> None:
|
|
"""Install multiple modules for the specified instance(s)."""
|
|
self._manage_module("install", instance_name, module_names)
|
|
|
|
def uninstall(self, instance_name: str = None, module_names: list = None) -> None:
|
|
"""Uninstall multiple modules for the specified instance(s)."""
|
|
self._manage_module("uninstall", instance_name, module_names)
|
|
|
|
def upgrade(self, instance_name: str = None, module_names: list = None) -> None:
|
|
"""Upgrade multiple modules for the specified instance(s)."""
|
|
self._manage_module("upgrade", instance_name, module_names)
|
|
|
|
def update_list(self, instance_name: str = None) -> dict:
|
|
"""Update the module list and return the number of updated and added modules.
|
|
|
|
Args:
|
|
instance_name (str): Name of the Odoo instance to update modules for
|
|
|
|
Returns:
|
|
dict: Dictionary containing the number of updated and added modules
|
|
"""
|
|
self.config.connect(instance_name)
|
|
try:
|
|
updated, added = self.config.execute(
|
|
instance_name,
|
|
"ir.module.module",
|
|
"update_list",
|
|
)
|
|
color_log.Show(
|
|
"OK",
|
|
f"Module list updated successfully in {instance_name}. Updated: {updated}, Added: {added}",
|
|
)
|
|
return {"updated": updated, "added": added}
|
|
except Exception as e:
|
|
color_log.Show(
|
|
"FAILED",
|
|
f"Error updating module list in {instance_name}: {e}",
|
|
)
|
|
return {"updated": 0, "added": 0}
|
|
|
|
def is_module_installed(self, instance_name: str, module_name: str) -> bool:
|
|
"""Check if a module is installed in the specified instance.
|
|
|
|
Args:
|
|
instance_name (str): Name of the Odoo instance
|
|
module_name (str): Name of the module to check
|
|
|
|
Returns:
|
|
bool: True if the module is installed, False otherwise
|
|
"""
|
|
self.config.connect(instance_name)
|
|
try:
|
|
module_ids = self.config.execute(
|
|
instance_name,
|
|
"ir.module.module",
|
|
"search",
|
|
[("name", "=", module_name), ("state", "=", "installed")],
|
|
)
|
|
return bool(module_ids)
|
|
except Exception as e:
|
|
color_log.Show(
|
|
"FAILED",
|
|
f"Error checking module {module_name} in {instance_name}: {e}",
|
|
)
|
|
return False
|