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.

70 lines
2.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 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. _logger.debug(
  33. "Cannot compute dir hash for %s, module not found",
  34. r.display_name)
  35. @api.multi
  36. def _store_checksum_installed(self, vals):
  37. """Store the right installed checksum, if addon is installed."""
  38. if 'checksum_installed' not in vals:
  39. try:
  40. version = vals["latest_version"]
  41. except KeyError:
  42. return # Not [un]installing/updating any addon
  43. if version is False:
  44. # Uninstalling
  45. self.write({'checksum_installed': False})
  46. else:
  47. # Installing or updating
  48. for one in self:
  49. one.checksum_installed = one.checksum_dir
  50. @api.model
  51. def create(self, vals):
  52. res = super(Module, self).create(vals)
  53. res._store_checksum_installed(vals)
  54. return res
  55. @api.multi
  56. def write(self, vals):
  57. res = super(Module, self).write(vals)
  58. self._store_checksum_installed(vals)
  59. return res