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.

174 lines
6.0 KiB

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