Skip to content

Instantly share code, notes, and snippets.

@rafnixg
Created March 31, 2023 02:39
Show Gist options
  • Save rafnixg/8be94e4b5226b19a465f5a36935828c7 to your computer and use it in GitHub Desktop.
Save rafnixg/8be94e4b5226b19a465f5a36935828c7 to your computer and use it in GitHub Desktop.
Odoo shell enviroment wrapper for executre custom code
import sys
def execute_script(env, script_path):
# Importa el archivo de script como un modulo
import importlib.util
spec = importlib.util.spec_from_file_location("module.name", script_path)
script_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(script_module)
# Ejecuta la función 'run' del modulo importado
script_module.run(env)
if __name__ == "__main__":
import odoo
from odoo import api, SUPERUSER_ID
config_file = "/etc/odoo/odoo.conf"
database_name = "dbname"
script_path = sys.argv[1] # TODO: Validate
odoo.tools.config.parse_config(['-c', config_file])
with odoo.api.Environment.manage():
registry = odoo.registry(database_name)
with registry.cursor() as cursor:
env = odoo.api.Environment(cursor, SUPERUSER_ID, {})
execute_script(env, script_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment