You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.7 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 LasLabs Inc.
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
  4. import logging
  5. from openerp import api, fields, models
  6. from openerp.modules.module import get_module_path
  7. _logger = logging.getLogger(__name__)
  8. try:
  9. from checksumdir import dirhash
  10. except ImportError:
  11. _logger.debug('Cannot `import checksumdir`.')
  12. class Module(models.Model):
  13. _inherit = 'ir.module.module'
  14. checksum_dir = fields.Char(
  15. compute='_compute_checksum_dir',
  16. )
  17. checksum_installed = fields.Char()
  18. @api.depends('name')
  19. def _compute_checksum_dir(self):
  20. exclude = self.env["ir.config_parameter"].get_param(
  21. "module_auto_update.checksum_excluded_extensions",
  22. "pyc,pyo",
  23. ).split(",")
  24. for r in self:
  25. try:
  26. r.checksum_dir = dirhash(
  27. get_module_path(r.name),
  28. 'sha1',
  29. excluded_extensions=exclude,
  30. )
  31. except TypeError:
  32. # Module path not found
  33. pass
  34. def _store_checksum_installed(self, vals):
  35. if self.env.context.get('retain_checksum_installed'):
  36. return
  37. if 'checksum_installed' not in vals:
  38. if vals.get('state') == 'installed':
  39. for r in self:
  40. r.checksum_installed = r.checksum_dir
  41. elif vals.get('state') == 'uninstalled':
  42. self.write({'checksum_installed': False})
  43. @api.multi
  44. def button_uninstall_cancel(self):
  45. # TODO Use super() like in v10 after pull is merged
  46. # HACK https://github.com/odoo/odoo/pull/18597
  47. return self.with_context(retain_checksum_installed=True).write({
  48. 'state': 'installed',
  49. })
  50. @api.multi
  51. def button_upgrade_cancel(self):
  52. # TODO Use super() like in v10 after pull is merged
  53. # HACK https://github.com/odoo/odoo/pull/18597
  54. return self.with_context(retain_checksum_installed=True).write({
  55. 'state': 'installed',
  56. })
  57. @api.model
  58. def create(self, vals):
  59. res = super(Module, self).create(vals)
  60. res._store_checksum_installed(vals)
  61. return res
  62. @api.model
  63. def update_list(self):
  64. res = super(Module, self).update_list()
  65. installed_modules = self.search([('state', '=', 'installed')])
  66. upgradeable_modules = installed_modules.filtered(
  67. lambda r: r.checksum_dir != r.checksum_installed,
  68. )
  69. upgradeable_modules.write({'state': "to upgrade"})
  70. return res
  71. @api.multi
  72. def write(self, vals):
  73. res = super(Module, self).write(vals)
  74. self._store_checksum_installed(vals)
  75. return res