Skip to content

Instantly share code, notes, and snippets.

@oerp-odoo
Last active November 28, 2022 12:32
Show Gist options
  • Save oerp-odoo/d301a5cf154e8ca3774fa9541408b6bb to your computer and use it in GitHub Desktop.
Save oerp-odoo/d301a5cf154e8ca3774fa9541408b6bb to your computer and use it in GitHub Desktop.
Install/Upgrade/Uninstall modules via Odoo RPC
#!/usr/bin/env python3
"""Install/update/uninstall specified odoo module."""
import odoorpc
import argparse
USER = 'admin'
PASSWORD = 'admin'
HOST = '127.0.0.1'
PORT = '8069'
DB = 'odoodb'
def prepare_args():
"""Prepare arguments for module action RPC call."""
parser = argparse.ArgumentParser(
description="Run modules install, upgrade or uninstall."
)
parser.add_argument(
'-i',
'--install',
help="Comma separated list of modules to install",
)
parser.add_argument(
'-u',
'--upgrade',
help="Comma separated list of modules to upgrade",
)
parser.add_argument(
'-del',
'--delete',
help="Comma separated list of modules to uninstall",
)
parser.add_argument(
'--user',
help="User to log in with",
default=USER,
)
parser.add_argument(
'--password',
help="Password to log in with",
default=PASSWORD,
)
parser.add_argument(
'--host',
help="Host to log in to",
default=HOST,
)
parser.add_argument(
'--port',
help="Odoo port",
default=PORT,
)
parser.add_argument(
'-d',
'--database',
help="Database name to log in to",
default=DB,
)
return parser.parse_args()
def login(user, password, host, port, database):
"""Login to Odoo database and return connection object."""
odoo = odoorpc.ODOO(host, port=port)
odoo.login(database, user, password)
return odoo
def _find_modules(env, module_names):
IrModuleModule = env['ir.module.module']
modules = module_names.replace(' ', '').split(',')
module_ids = IrModuleModule.search([('name', 'in', modules)])
return IrModuleModule.browse(module_ids)
def trigger_action(env, module_names, action):
modules = _find_modules(env, module_names)
method = getattr(modules, f'button_immediate_{action}')
return method()
if __name__ == '__main__':
args = prepare_args()
odoo = login(args.user, args.password, args.host, args.port, args.database)
env = odoo.env
if args.install:
trigger_action(env, args.install, 'install')
if args.upgrade:
trigger_action(env, args.upgrade, 'upgrade')
if args.delete:
trigger_action(env, args.delete, 'uninstall')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment