From e970c6ca35988879b207103f1ef6383f3abbec45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Thu, 7 Oct 2021 15:07:45 +0200 Subject: [PATCH 1/2] Silence Odoo 15 Environment.manage warning Also, refactor to isolate most compatibility code in a dedicated module where it will be easier to locate when we drop older versions. --- click_odoo/__init__.py | 4 ++-- click_odoo/compat.py | 47 +++++++++++++++++++++++++++++++++++++++ click_odoo/env.py | 25 +++------------------ click_odoo/env_options.py | 9 +++----- newsfragments/43.feature | 1 + tests/scripts/script5.py | 8 +++---- 6 files changed, 59 insertions(+), 35 deletions(-) create mode 100644 click_odoo/compat.py create mode 100644 newsfragments/43.feature diff --git a/click_odoo/__init__.py b/click_odoo/__init__.py index 63f80a9..ab512c0 100644 --- a/click_odoo/__init__.py +++ b/click_odoo/__init__.py @@ -1,7 +1,7 @@ # Copyright 2018 ACSONE SA/NV () # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). +from .compat import odoo # noqa +from .compat import odoo_bin # noqa from .env import OdooEnvironment # noqa -from .env import odoo # noqa -from .env import odoo_bin # noqa from .env_options import env_options # noqa diff --git a/click_odoo/compat.py b/click_odoo/compat.py new file mode 100644 index 0000000..3aea5c6 --- /dev/null +++ b/click_odoo/compat.py @@ -0,0 +1,47 @@ +import contextlib + +__all__ = [ + "Environment", + "environment_manage", + "odoo", + "odoo_bin", + "odoo_version_info", +] + +try: + import odoo + from odoo.api import Environment +except ImportError as e: + if hasattr(e, "name") and e.name != "odoo": + raise + # Odoo < 10 + try: + import openerp as odoo + from openerp.api import Environment + except ImportError: + if hasattr(e, "name") and e.name != "openerp": + raise + raise ImportError("No module named odoo nor openerp") + + +try: + from odoo.release import version_info as odoo_version_info +except ImportError: + from openerp.release import version_info as odoo_version_info + + +if odoo_version_info < (10, 0): + odoo_bin = "openerp-server" +else: + odoo_bin = "odoo" + + +if odoo_version_info < (15, 0): + environment_manage = Environment.manage +else: + + @contextlib.contextmanager + def environment_manage(): + # Environment.manage is a no-op in Odoo 15+, but it + # emits a noisy warning so let's avoid it. + yield diff --git a/click_odoo/env.py b/click_odoo/env.py index ec3ecef..08d0691 100644 --- a/click_odoo/env.py +++ b/click_odoo/env.py @@ -4,31 +4,14 @@ import logging from contextlib import contextmanager -try: - import odoo - from odoo.api import Environment - - odoo_bin = "odoo" -except ImportError as e: - if hasattr(e, "name") and e.name != "odoo": - raise - # Odoo < 10 - try: - import openerp as odoo - from openerp.api import Environment - - odoo_bin = "openerp-server" - except ImportError: - if hasattr(e, "name") and e.name != "openerp": - raise - raise ImportError("No module named odoo nor openerp") +from .compat import Environment, environment_manage, odoo, odoo_version_info _logger = logging.getLogger(__name__) @contextmanager def OdooEnvironment(database, rollback=False, **kwargs): - with Environment.manage(): + with environment_manage(): registry = odoo.registry(database) try: with registry.cursor() as cr: @@ -53,9 +36,7 @@ def OdooEnvironment(database, rollback=False, **kwargs): else: cr.commit() finally: - if odoo.tools.parse_version( - odoo.release.version - ) < odoo.tools.parse_version("10.0"): + if odoo_version_info < (10, 0): odoo.modules.registry.RegistryManager.delete(database) else: odoo.modules.registry.Registry.delete(database) diff --git a/click_odoo/env_options.py b/click_odoo/env_options.py index 28644e0..d6493a4 100644 --- a/click_odoo/env_options.py +++ b/click_odoo/env_options.py @@ -8,6 +8,7 @@ import click from click.decorators import _param_memo # XXX undocumented click internal +from .compat import odoo_version_info from .env import OdooEnvironment, odoo _logger = logging.getLogger(__name__) @@ -112,9 +113,7 @@ def _register(self, ctx, param, value): return value def _fix_odoo_logging(self): - if odoo.tools.parse_version(odoo.release.version) < odoo.tools.parse_version( - "9.0c" - ): + if odoo_version_info < (9, 0): handlers = logging.getLogger().handlers if handlers and len(handlers) == 1: handler = handlers[0] @@ -123,9 +122,7 @@ def _fix_odoo_logging(self): handler.stream = sys.stderr def _fix_disable_wsgi_module_handlers(self): - if odoo.tools.parse_version(odoo.release.version) < odoo.tools.parse_version( - "9.0c" - ): + if odoo_version_info < (9, 0): odoo.service.wsgi_server.module_handlers[:] = [] def get_odoo_args(self, ctx): diff --git a/newsfragments/43.feature b/newsfragments/43.feature new file mode 100644 index 0000000..a33e4a8 --- /dev/null +++ b/newsfragments/43.feature @@ -0,0 +1 @@ +Silenced a noisy warning about Environment.manage() being a no-op in Odoo 15. diff --git a/tests/scripts/script5.py b/tests/scripts/script5.py index 3784ea1..55a5634 100644 --- a/tests/scripts/script5.py +++ b/tests/scripts/script5.py @@ -1,12 +1,10 @@ # noqa try: import odoo.release as release - import odoo.tools.parse_version as parse_version except (AttributeError, ImportError): import openerp.release as release - import openerp.tools.parse_version as parse_version -if parse_version(release.version) > parse_version("9.0c"): - import odoo.addons.addon1 # noqa -else: +if release.version_info < (10, 0): import openerp.addons.addon1 # noqa +else: + import odoo.addons.addon1 # noqa From 0a3b436d83c5b1f8d86d1834d8cee04c34e87cab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Fri, 8 Oct 2021 11:44:07 +0200 Subject: [PATCH 2/2] Remove another direct call to Environment.manage() --- click_odoo/env_options.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/click_odoo/env_options.py b/click_odoo/env_options.py index d6493a4..28cd102 100644 --- a/click_odoo/env_options.py +++ b/click_odoo/env_options.py @@ -8,7 +8,7 @@ import click from click.decorators import _param_memo # XXX undocumented click internal -from .compat import odoo_version_info +from .compat import environment_manage, odoo_version_info from .env import OdooEnvironment, odoo _logger = logging.getLogger(__name__) @@ -210,7 +210,7 @@ def _invoke(self, ctx): ctx.params["env"] = env return self.org_invoke(ctx) else: - with odoo.api.Environment.manage(): + with environment_manage(): ctx.params["env"] = None return self.org_invoke(ctx) except click.exceptions.Exit: