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.

148 lines
5.1 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 LasLabs Inc.
  3. # Copyright 2018 ACSONE SA/NV.
  4. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
  5. import json
  6. import logging
  7. import os
  8. from openerp import api, exceptions, models, tools
  9. from openerp.modules.module import get_module_path
  10. from ..addon_hash import addon_hash
  11. PARAM_INSTALLED_CHECKSUMS = \
  12. 'module_auto_update.installed_checksums'
  13. PARAM_EXCLUDE_PATTERNS = \
  14. 'module_auto_update.exclude_patterns'
  15. DEFAULT_EXCLUDE_PATTERNS = \
  16. '*.pyc,*.pyo,i18n/*.pot,i18n_extra/*.pot,static/*'
  17. _logger = logging.getLogger(__name__)
  18. class IncompleteUpgradeError(exceptions.Warning):
  19. pass
  20. class Module(models.Model):
  21. _inherit = 'ir.module.module'
  22. @api.multi
  23. def _get_checksum_dir(self):
  24. self.ensure_one()
  25. exclude_patterns = self.env["ir.config_parameter"].get_param(
  26. PARAM_EXCLUDE_PATTERNS,
  27. DEFAULT_EXCLUDE_PATTERNS,
  28. )
  29. exclude_patterns = [p.strip() for p in exclude_patterns.split(',')]
  30. keep_langs = self.env['res.lang'].search([]).mapped('code')
  31. module_path = get_module_path(self.name)
  32. if module_path and os.path.isdir(module_path):
  33. checksum_dir = addon_hash(
  34. module_path,
  35. exclude_patterns,
  36. keep_langs,
  37. )
  38. else:
  39. checksum_dir = False
  40. return checksum_dir
  41. @api.model
  42. def _get_saved_checksums(self):
  43. Icp = self.env['ir.config_parameter']
  44. return json.loads(Icp.get_param(PARAM_INSTALLED_CHECKSUMS, '{}'))
  45. @api.model
  46. def _save_checksums(self, checksums):
  47. Icp = self.env['ir.config_parameter']
  48. Icp.set_param(PARAM_INSTALLED_CHECKSUMS, json.dumps(checksums))
  49. @api.model
  50. def _save_installed_checksums(self):
  51. checksums = {}
  52. installed_modules = self.search([('state', '=', 'installed')])
  53. for module in installed_modules:
  54. checksums[module.name] = module._get_checksum_dir()
  55. self._save_checksums(checksums)
  56. @api.model
  57. def _get_modules_partially_installed(self):
  58. return self.search([
  59. ('state', 'in', ('to install', 'to remove', 'to upgrade')),
  60. ])
  61. @api.model
  62. def _get_modules_with_changed_checksum(self):
  63. saved_checksums = self._get_saved_checksums()
  64. installed_modules = self.search([('state', '=', 'installed')])
  65. return installed_modules.filtered(
  66. lambda r: r._get_checksum_dir() != saved_checksums.get(r.name),
  67. )
  68. @api.model
  69. def upgrade_changed_checksum(self, overwrite_existing_translations=False):
  70. """Run an upgrade of the database, upgrading only changed modules.
  71. Installed modules for which the checksum has changed since the
  72. last successful run of this method are marked "to upgrade",
  73. then the normal Odoo scheduled upgrade process
  74. is launched.
  75. If there is no module with a changed checksum, and no module in state
  76. other than installed, uninstalled, uninstallable, this method does
  77. nothing, otherwise the normal Odoo upgrade process is launched.
  78. After a successful upgrade, the checksums of installed modules are
  79. saved.
  80. In case of error during the upgrade, an exception is raised.
  81. If any module remains to upgrade or to uninstall after the upgrade
  82. process, an exception is raised as well.
  83. Note: this method commits the current transaction at each important
  84. step, it is therefore not intended to be run as part of a
  85. larger transaction.
  86. """
  87. _logger.info(
  88. "Checksum upgrade starting (i18n-overwrite=%s)...",
  89. overwrite_existing_translations
  90. )
  91. tools.config['overwrite_existing_translations'] = \
  92. overwrite_existing_translations
  93. _logger.info("Updating modules list...")
  94. self.update_list()
  95. changed_modules = self._get_modules_with_changed_checksum()
  96. if not changed_modules and not self._get_modules_partially_installed():
  97. _logger.info("No checksum change detected in installed modules "
  98. "and all modules installed, nothing to do.")
  99. return
  100. _logger.info("Marking the following modules to upgrade, "
  101. "for their checksums changed: %s...",
  102. ','.join(changed_modules.mapped('name')))
  103. changed_modules.button_upgrade()
  104. self.env.cr.commit() # pylint: disable=invalid-commit
  105. _logger.info("Upgrading...")
  106. self.env['base.module.upgrade'].upgrade_module()
  107. self.env.cr.commit() # pylint: disable=invalid-commit
  108. _logger.info("Upgrade successful, updating checksums...")
  109. self._save_installed_checksums()
  110. self.env.cr.commit() # pylint: disable=invalid-commit
  111. partial_modules = self._get_modules_partially_installed()
  112. if partial_modules:
  113. raise IncompleteUpgradeError(
  114. "Checksum upgrade successful "
  115. "but incomplete for the following modules: %s" %
  116. ','.join(partial_modules.mapped('name'))
  117. )
  118. _logger.info("Checksum upgrade complete.")