Skip to content

Commit fbd0b4f

Browse files
committed
[IMP] util/modules.py
Warn about usages of the module-altering functions outside `base` scripts. closes #334 Signed-off-by: Christophe Simonis (chs) <chs@odoo.com>
1 parent 39e1b8b commit fbd0b4f

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

src/util/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,9 @@ class UnknownModuleError(AssertionError):
1313
pass
1414

1515

16+
class UpgradeWarning(Warning):
17+
pass
18+
19+
1620
# Compat
1721
MigrationError = UpgradeError

src/util/modules.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
except ImportError:
1616
from collections import Sequence, Set
1717

18+
import functools
1819
import itertools
1920
import logging
2021
import os
22+
import warnings
2123
from inspect import currentframe
2224
from operator import itemgetter
2325

@@ -43,7 +45,7 @@
4345
from openerp.addons.web.controllers.main import module_topological_sort as topological_sort
4446

4547
from .const import ENVIRON, NEARLYWARN
46-
from .exceptions import MigrationError, SleepyDeveloperError, UnknownModuleError
48+
from .exceptions import MigrationError, SleepyDeveloperError, UnknownModuleError, UpgradeWarning
4749
from .fields import remove_field
4850
from .helpers import _validate_model, table_of_model
4951
from .misc import on_CI, parse_version, str2bool, version_gte
@@ -87,6 +89,28 @@
8789
basestring = str
8890

8991

92+
if version_gte("16.0"):
93+
from odoo.modules.registry import Registry as _Registry
94+
95+
def _warn_usage_outside_base(f):
96+
@functools.wraps(f)
97+
def wrapper(cr, *args, **kwargs):
98+
if "base" in _Registry(cr.dbname)._init_modules:
99+
warnings.warn(
100+
"Calling `{}` outside the module base can lead to unexpected results".format(f.__name__),
101+
UpgradeWarning,
102+
stacklevel=2,
103+
)
104+
return f(cr, *args, **kwargs)
105+
106+
return wrapper
107+
108+
else:
109+
# deactivated on old versions, to avoid modification of legacy upgrade scripts.
110+
def _warn_usage_outside_base(f):
111+
return f
112+
113+
90114
def modules_installed(cr, *modules):
91115
"""
92116
Return whether *all* given modules are installed.
@@ -124,6 +148,7 @@ def module_installed(cr, module):
124148
return modules_installed(cr, module)
125149

126150

151+
@_warn_usage_outside_base
127152
def uninstall_module(cr, module):
128153
"""
129154
Uninstall and remove all records owned by a module.
@@ -285,9 +310,11 @@ def uninstall_theme(cr, theme, base_theme=None):
285310
for website in websites:
286311
IrModuleModule._theme_remove(website)
287312
flush(env_["base"])
288-
uninstall_module(cr, theme)
313+
with warnings.catchwarnings(action="ignore", category=UpgradeWarning):
314+
uninstall_module(cr, theme)
289315

290316

317+
@_warn_usage_outside_base
291318
def remove_module(cr, module):
292319
"""
293320
Completely remove a module.
@@ -353,6 +380,7 @@ def _update_view_key(cr, old, new):
353380
)
354381

355382

383+
@_warn_usage_outside_base
356384
def rename_module(cr, old, new):
357385
"""
358386
Rename a module and all references to it.
@@ -389,6 +417,7 @@ def rename_module(cr, old, new):
389417
fu[new] = fu.pop(old)
390418

391419

420+
@_warn_usage_outside_base
392421
def merge_module(cr, old, into, update_dependers=True):
393422
"""
394423
Merge a module into another.
@@ -713,6 +742,7 @@ def _assert_modules_exists(cr, *modules):
713742
raise UnknownModuleError(*sorted(unexisting_modules))
714743

715744

745+
@_warn_usage_outside_base
716746
def new_module_dep(cr, module, new_dep):
717747
assert isinstance(new_dep, basestring)
718748
_assert_modules_exists(cr, module, new_dep)
@@ -741,6 +771,7 @@ def new_module_dep(cr, module, new_dep):
741771
_force_install_module(cr, new_dep, reason="it's a new dependency of {!r}".format(module))
742772

743773

774+
@_warn_usage_outside_base
744775
def remove_module_deps(cr, module, old_deps):
745776
assert isinstance(old_deps, (Sequence, Set)) and not isinstance(old_deps, basestring)
746777
# As the goal is to have dependencies removed, the objective is reached even when they don't exist.
@@ -758,13 +789,15 @@ def remove_module_deps(cr, module, old_deps):
758789
)
759790

760791

792+
@_warn_usage_outside_base
761793
def module_deps_diff(cr, module, plus=(), minus=()):
762794
for new_dep in plus:
763795
new_module_dep(cr, module, new_dep)
764796
if minus:
765797
remove_module_deps(cr, module, tuple(minus))
766798

767799

800+
@_warn_usage_outside_base
768801
def module_auto_install(cr, module, auto_install):
769802
if column_exists(cr, "ir_module_module_dependency", "auto_install_required"):
770803
params = []
@@ -790,6 +823,7 @@ def module_auto_install(cr, module, auto_install):
790823
cr.execute("UPDATE ir_module_module SET auto_install = %s WHERE name = %s", [auto_install is not False, module])
791824

792825

826+
@_warn_usage_outside_base
793827
def trigger_auto_install(cr, module):
794828
_assert_modules_exists(cr, module)
795829
if AUTO_INSTALL == "none":
@@ -881,6 +915,7 @@ def _set_module_countries(cr, module, countries):
881915
cr.execute(insert_query, [module, tuple(c.upper() for c in countries)])
882916

883917

918+
@_warn_usage_outside_base
884919
def new_module(cr, module, deps=(), auto_install=False, category=None, countries=()):
885920
if deps:
886921
_assert_modules_exists(cr, *deps)
@@ -930,7 +965,7 @@ def new_module(cr, module, deps=(), auto_install=False, category=None, countries
930965
trigger_auto_install(cr, module)
931966

932967

933-
def _caller_version(depth=2):
968+
def _caller_version(depth=3):
934969
frame = currentframe()
935970
version = "util"
936971
while version == "util":
@@ -941,6 +976,7 @@ def _caller_version(depth=2):
941976
return version
942977

943978

979+
@_warn_usage_outside_base
944980
def force_upgrade_of_fresh_module(cr, module, init=True):
945981
"""
946982
Force the execution of upgrade scripts for a module that is being installed.
@@ -1067,6 +1103,7 @@ def _trigger_auto_discovery(cr):
10671103
_force_upgrade_of_fresh_module(cr, module, init, version)
10681104

10691105

1106+
@_warn_usage_outside_base
10701107
def modules_auto_discovery(cr, force_installs=None, force_upgrades=None):
10711108
# Cursor, Optional[Set[str]], Optional[Set[str]] -> None
10721109

0 commit comments

Comments
 (0)