Skip to content

Commit

Permalink
[ADD] manifest-behind-migrations: check manifest version >= migrations
Browse files Browse the repository at this point in the history
A new check has been added to verify that the migrations and manifest
version for a module are up to date.

Closes #461.
  • Loading branch information
antonag32 committed Nov 8, 2023
1 parent ca8822f commit 6c45cd7
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ external-request-timeout | Use of external request method `%s` without timeout.
invalid-commit | Use of cr.commit() directly - More info https://github.com/OCA/odoo-community.org/blob/master/website/Contribution/CONTRIBUTING.rst#never-commit-the-transaction | E8102
license-allowed | License "%s" not allowed in manifest file. | C8105
manifest-author-string | The author key in the manifest file must be a string (with comma separated values) | E8101
manifest-behind-migrations | Manifest version (%s) is lower than migration scripts (%s) | E8145
manifest-data-duplicated | The file "%s" is duplicated in lines %s from manifest key "%s" | W8125
manifest-deprecated-key | Deprecated key "%s" in manifest file | C8103
manifest-maintainers-list | The maintainers key in the manifest file must be a list of strings | E8104
Expand Down Expand Up @@ -203,6 +204,12 @@ Checks valid only for odoo <= 13.0

- https://github.com/OCA/pylint-odoo/blob/v9.0.1/testing/resources/test_repo/broken_module3/__openerp__.py#L5 The author key in the manifest file must be a string (with comma separated values)

* manifest-behind-migrations

- https://github.com/OCA/pylint-odoo/blob/v9.0.1/testing/resources/test_repo/broken_module2/__openerp__.py#L2 Manifest version (1.0) is lower than migration scripts (2.0)
- https://github.com/OCA/pylint-odoo/blob/v9.0.1/testing/resources/test_repo/eleven_module/__manifest__.py#L1 Manifest version (11.0.1.0.0) is lower than migration scripts (11.0.1.0.1)
- https://github.com/OCA/pylint-odoo/blob/v9.0.1/testing/resources/test_repo/test_module/__openerp__.py#L2 Manifest version (10.0.1.0.0) is lower than migration scripts (11.0.1.0.0)

* manifest-data-duplicated

- https://github.com/OCA/pylint-odoo/blob/v9.0.1/testing/resources/test_repo/broken_module/__openerp__.py#L18 The file "duplicated.xml" is duplicated in lines 19 from manifest key "data"
Expand Down
45 changes: 45 additions & 0 deletions src/pylint_odoo/checkers/odoo_addons.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@
"no-raise-unlink",
"Use @api.ondelete to add any constraints instead",
),
"E8145": (
"Manifest version (%s) is lower than migration scripts (%s)",
"manifest-behind-migrations",
"Update your manifest version, otherwise the migration script won't run",
),
"F8101": ('File "%s": "%s" not found.', "resource-not-exist", CHECK_DESCRIPTION),
"R8101": (
"`odoo.exceptions.Warning` is a deprecated alias to `odoo.exceptions.UserError` "
Expand Down Expand Up @@ -556,8 +561,25 @@ class OdooAddons(OdooBaseChecker, BaseChecker):

def __init__(self, linter: PyLinter):
super().__init__(linter)
self._module_versions = {}
self._module_migrations = defaultdict(set)
self._deprecated_odoo_methods = set()

@staticmethod
def version_greater_equal_than(original, against):
"""Compare two versions and state which one is bigger than the other.
:param str original: Original version
:param str against: Value to compare against
:return: True if a >= b, otherwise False
"""
for original_val, against_val in zip(original.split("."), against.split(".")):
if int(against_val) > int(original_val):
return False
if int(original_val) > int(against_val):
return True

return True

def close(self):
"""Final process get all cached values and add messages"""
self.linter.config.deprecated_odoo_model_methods = set()
Expand All @@ -579,6 +601,18 @@ def close(self):
"consider-merging-classes-inherited", node=first_node, args=(odoo_class_inherit, ", ".join(path_nodes))
)

if self.linter.is_message_enabled("manifest-behind-migrations"):
for module, migrations in self._module_migrations.items():
module_version, manifest_node = self._module_versions[module]
for migration in migrations:
try:
if not self.version_greater_equal_than(module_version, migration):
self.add_message(
"manifest-behind-migrations", node=manifest_node, args=(module_version, migration)
)
except ValueError:
continue

def visit_module(self, node):
"""Initizalize the cache to save the original library name
of all imported node
Expand All @@ -587,6 +621,13 @@ def visit_module(self, node):
All these methods are these "visit_*" methods are called from pylint API
"""
self._from_imports = {}
basename = os.path.basename(node.file)
if basename not in {"pre-migration.py", "post-migration.py"}:
return

migration_version = os.path.basename(os.path.dirname(node.file))
module_name = os.path.basename(os.path.abspath(os.path.join(node.file, "..", "..", "..")))
self._module_migrations[module_name].add(migration_version)

def leave_module(self, node):
"""Clear variables"""
Expand Down Expand Up @@ -1006,6 +1047,7 @@ def visit_call(self, node):
"missing-readme",
"resource-not-exist",
"website-manifest-key-not-valid-uri",
"manifest-behind-migrations",
)
def visit_dict(self, node):
if not os.path.basename(self.linter.current_file) in misc.MANIFEST_FILES or not isinstance(
Expand Down Expand Up @@ -1068,6 +1110,9 @@ def visit_dict(self, node):

# Check version format
version_format = manifest_dict.get("version", "")
self._module_versions[os.path.basename(os.path.dirname(self.linter.current_file))] = (version_format, node)

# Check version format
formatrgx = self.formatversion(version_format)
if version_format and not formatrgx:
self.add_message(
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Should raise manifest-behind-migrations but since manifest version is not parseable, it won't
Empty file.
Empty file.
Empty file.
1 change: 1 addition & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"website-manifest-key-not-valid-uri": 1,
"no-raise-unlink": 2,
"deprecated-odoo-model-method": 2,
"manifest-behind-migrations": 3,
}


Expand Down

0 comments on commit 6c45cd7

Please sign in to comment.