Files
odoo_source/odoo/modules/neutralize.py
T
Martin Trigaux 22ab49e343 [IMP] *: use file_path and file_open
Replace all the calls to get_resource_path to the better file_path or
directly use file_open when not needed

Doing both a get_resource_path and file_open means checking twice that
the file exists.
Doing a simple path concatenation before a file_open is safe.
If given to another method (e.g. etree.parse), calling file_path is
the prefered method.

Note that get_resource_path used to return False when the file does
not exists while file_path/file_open raises a FileNotFoundException

closes odoo/odoo#135607

Related: odoo/upgrade#5187
Related: odoo/enterprise#47475
Signed-off-by: Martin Trigaux (mat) <mat@odoo.com>
2023-10-06 14:33:43 +00:00

33 lines
991 B
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from contextlib import suppress
import odoo
import logging
_logger = logging.getLogger(__name__)
def get_installed_modules(cursor):
cursor.execute('''
SELECT name
FROM ir_module_module
WHERE state IN ('installed', 'to upgrade', 'to remove');
''')
return [result[0] for result in cursor.fetchall()]
def get_neutralization_queries(modules):
# neutralization for each module
for module in modules:
filename = f'{module}/data/neutralize.sql'
with suppress(FileNotFoundError):
with odoo.tools.misc.file_open(filename) as file:
yield file.read().strip()
def neutralize_database(cursor):
installed_modules = get_installed_modules(cursor)
queries = get_neutralization_queries(installed_modules)
for query in queries:
cursor.execute(query)
_logger.info("Neutralization finished")