Browse Source

[REF][module_auto_update] Recompute dir hashes only when needed

By removing the recomputation from `update_list` we get faster CLI module upgrades and it only performs the autoupdate when using the autoupdate wizard or cron.
pull/918/head
Jairo Llopis 8 years ago
parent
commit
5a2c146705
  1. 15
      module_auto_update/models/module.py
  2. 12
      module_auto_update/tests/test_module.py
  3. 20
      module_auto_update/tests/test_module_upgrade.py
  4. 14
      module_auto_update/wizards/module_upgrade.py

15
module_auto_update/models/module.py

@ -37,8 +37,9 @@ class Module(models.Model):
excluded_extensions=exclude, excluded_extensions=exclude,
) )
except TypeError: except TypeError:
# Module path not found
pass
_logger.debug(
"Cannot compute dir hash for %s, module not found",
r.display_name)
def _store_checksum_installed(self, vals): def _store_checksum_installed(self, vals):
if self.env.context.get('retain_checksum_installed'): if self.env.context.get('retain_checksum_installed'):
@ -72,16 +73,6 @@ class Module(models.Model):
res._store_checksum_installed(vals) res._store_checksum_installed(vals)
return res return res
@api.model
def update_list(self):
res = super(Module, self).update_list()
installed_modules = self.search([('state', '=', 'installed')])
upgradeable_modules = installed_modules.filtered(
lambda r: r.checksum_dir != r.checksum_installed,
)
upgradeable_modules.write({'state': "to upgrade"})
return res
@api.multi @api.multi
def write(self, vals): def write(self, vals):
res = super(Module, self).write(vals) res = super(Module, self).write(vals)

12
module_auto_update/tests/test_module.py

@ -154,33 +154,33 @@ class TestModule(TransactionCase):
) )
@mock.patch('%s.get_module_path' % model) @mock.patch('%s.get_module_path' % model)
def test_update_list(self, get_module_path_mock):
def test_get_module_list(self, module_path_mock):
"""It should change the state of modules with different """It should change the state of modules with different
checksum_dir and checksum_installed to 'to upgrade'""" checksum_dir and checksum_installed to 'to upgrade'"""
get_module_path_mock.return_value = self.own_dir_path
module_path_mock.return_value = self.own_dir_path
vals = { vals = {
'name': 'module_auto_update_test_module', 'name': 'module_auto_update_test_module',
'state': 'installed', 'state': 'installed',
} }
test_module = self.create_test_module(vals) test_module = self.create_test_module(vals)
test_module.checksum_installed = 'test' test_module.checksum_installed = 'test'
self.env['ir.module.module'].update_list()
self.env['base.module.upgrade'].get_module_list()
self.assertEqual( self.assertEqual(
test_module.state, 'to upgrade', test_module.state, 'to upgrade',
'List update does not mark upgradeable modules "to upgrade"', 'List update does not mark upgradeable modules "to upgrade"',
) )
@mock.patch('%s.get_module_path' % model) @mock.patch('%s.get_module_path' % model)
def test_update_list_only_changes_installed(self, get_module_path_mock):
def test_get_module_list_only_changes_installed(self, module_path_mock):
"""It should not change the state of a module with a former state """It should not change the state of a module with a former state
other than 'installed' to 'to upgrade'""" other than 'installed' to 'to upgrade'"""
get_module_path_mock.return_value = self.own_dir_path
module_path_mock.return_value = self.own_dir_path
vals = { vals = {
'name': 'module_auto_update_test_module', 'name': 'module_auto_update_test_module',
'state': 'uninstalled', 'state': 'uninstalled',
} }
test_module = self.create_test_module(vals) test_module = self.create_test_module(vals)
self.env['ir.module.module'].update_list()
self.env['base.module.upgrade'].get_module_list()
self.assertNotEqual( self.assertNotEqual(
test_module.state, 'to upgrade', test_module.state, 'to upgrade',
'List update changed state of an uninstalled module', 'List update changed state of an uninstalled module',

20
module_auto_update/tests/test_module_upgrade.py

@ -31,12 +31,14 @@ class TestModuleUpgrade(TransactionCase):
@mock.patch.object(RegistryManager, 'new') @mock.patch.object(RegistryManager, 'new')
def test_upgrade_module(self, new_mock): def test_upgrade_module(self, new_mock):
"""It should call update_list method on ir.module.module"""
update_list_mock = mock.MagicMock()
self.env['ir.module.module']._patch_method(
'update_list',
update_list_mock,
)
self.env['base.module.upgrade'].upgrade_module()
update_list_mock.assert_called_once_with()
self.env['ir.module.module']._revert_method('update_list')
"""Calls get_module_list when upgrading in api.model mode"""
get_module_list_mock = mock.MagicMock()
try:
self.env['base.module.upgrade']._patch_method(
'get_module_list',
get_module_list_mock,
)
self.env['base.module.upgrade'].upgrade_module()
get_module_list_mock.assert_called_once_with()
finally:
self.env['base.module.upgrade']._revert_method('get_module_list')

14
module_auto_update/wizards/module_upgrade.py

@ -8,6 +8,16 @@ from openerp import api, models
class ModuleUpgrade(models.TransientModel): class ModuleUpgrade(models.TransientModel):
_inherit = 'base.module.upgrade' _inherit = 'base.module.upgrade'
@api.model
def get_module_list(self):
Module = self.env["ir.module.module"]
installed_modules = Module.search([('state', '=', 'installed')])
upgradeable_modules = installed_modules.filtered(
lambda r: r.checksum_dir != r.checksum_installed,
)
upgradeable_modules.write({'state': "to upgrade"})
return super(ModuleUpgrade, self).get_module_list()
@api.multi @api.multi
def upgrade_module_cancel(self): def upgrade_module_cancel(self):
return super( return super(
@ -17,5 +27,7 @@ class ModuleUpgrade(models.TransientModel):
@api.multi @api.multi
def upgrade_module(self): def upgrade_module(self):
self.env['ir.module.module'].update_list()
# Compute updates by checksum when called in @api.model fashion
if not self:
self.get_module_list()
return super(ModuleUpgrade, self).upgrade_module() return super(ModuleUpgrade, self).upgrade_module()
Loading…
Cancel
Save