Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

83 lines
2.9KB

  1. # Copyright (C) 2019 - Today: GRAP (http://www.grap.coop)
  2. # @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from .log import logger
  5. from .config import _MANIFEST_NAMES
  6. from .tools import _execute_shell
  7. class ModuleMigration():
  8. _migration = False
  9. _module_name = False
  10. _module_path = False
  11. def __init__(self, migration, module_name):
  12. self._migration = migration
  13. self._module_name = module_name
  14. self._module_path = self._migration._directory_path / module_name
  15. def run(self):
  16. logger.info("[%s] Running migration from %s to %s" % (
  17. self._module_name,
  18. self._migration._migration_steps[0]["init_version_name"],
  19. self._migration._migration_steps[-1]["target_version_name"],
  20. ))
  21. # Apply migration script
  22. for migration_script in self._migration._migration_scripts:
  23. migration_script.run(self._module_path,
  24. self._get_manifest_path(),
  25. self._module_name,
  26. self._migration._migration_steps,
  27. self._migration._directory_path,
  28. self._migration._commit_enabled
  29. )
  30. self._commit_changes("[MIG] %s: Migration to %s" % (
  31. self._module_name,
  32. self._migration._migration_steps[-1]["target_version_name"]))
  33. def _get_manifest_path(self):
  34. for manifest_name in _MANIFEST_NAMES:
  35. manifest_path = self._module_path / manifest_name
  36. if manifest_path.exists():
  37. return manifest_path
  38. def _rename_file(self, module_path, old_file_path, new_file_path):
  39. """
  40. Rename a file. try to execute 'git mv', to avoid huge diff.
  41. if 'git mv' fails, make a classical rename
  42. """
  43. logger.info(
  44. "Renaming file: '%s' by '%s' " % (
  45. old_file_path.replace(str(module_path.resolve()), ""),
  46. new_file_path.replace(str(module_path.resolve()), ""))
  47. )
  48. if self._migration._commit_enabled:
  49. _execute_shell(
  50. "git mv %s %s" % (old_file_path, new_file_path),
  51. path=module_path)
  52. else:
  53. _execute_shell(
  54. "mv %s %s" % (old_file_path, new_file_path),
  55. path=module_path)
  56. def _commit_changes(self, commit_name):
  57. if not self._migration._commit_enabled:
  58. return
  59. if _execute_shell("git diff", path=self._migration._directory_path):
  60. logger.info(
  61. "Commit changes for %s. commit name '%s'" % (
  62. self._module_name, commit_name
  63. ))
  64. _execute_shell(
  65. " git add . --all && git commit --no-verify -m '%s'" % (
  66. commit_name
  67. ), path=self._migration._directory_path)
上海开阖软件有限公司 沪ICP备12045867号-1