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.

113 lines
3.9 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (c) 2017-2019 MuK IT GmbH.
  4. #
  5. # This file is part of MuK Filestore Field
  6. # (see https://mukit.at).
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ###################################################################################
  22. import logging
  23. import os
  24. import shutil
  25. import tempfile
  26. import zipfile
  27. from odoo.addons.muk_fields_file.fields import file
  28. from odoo.addons.muk_utils.tools import patch
  29. from odoo.service import db
  30. from odoo.tools import osutil
  31. _logger = logging.getLogger(__name__)
  32. @patch.monkey_patch(db)
  33. @db.check_db_management_enabled
  34. def exp_duplicate_database(db_original_name, db_name):
  35. res = exp_duplicate_database.super(db_original_name, db_name)
  36. from_files = file.get_store_path(db_original_name)
  37. to_files = file.get_store_path(db_name)
  38. if os.path.exists(from_files) and not os.path.exists(to_files):
  39. shutil.copytree(from_files, to_files)
  40. return res
  41. @patch.monkey_patch(db)
  42. @db.check_db_management_enabled
  43. def exp_drop(db_name):
  44. res = exp_drop.super(db_name)
  45. files = file.get_store_path(db_name)
  46. if os.path.exists(files):
  47. shutil.rmtree(files)
  48. return res
  49. @patch.monkey_patch(db)
  50. @db.check_db_management_enabled
  51. def dump_db(db_name, stream, backup_format="zip"):
  52. if backup_format == "zip":
  53. res = dump_db.super(db_name, False, backup_format)
  54. with osutil.tempdir() as dump_dir:
  55. with zipfile.ZipFile(res, "r") as zip:
  56. zip.extractall(dump_dir)
  57. files = file.get_store_path(db_name)
  58. if os.path.exists(files):
  59. shutil.copytree(files, os.path.join(dump_dir, "files"))
  60. if stream:
  61. osutil.zip_dir(
  62. dump_dir,
  63. stream,
  64. include_dir=False,
  65. fnct_sort=lambda file_name: file_name != "dump.sql",
  66. )
  67. else:
  68. t = tempfile.TemporaryFile()
  69. osutil.zip_dir(
  70. dump_dir,
  71. t,
  72. include_dir=False,
  73. fnct_sort=lambda file_name: file_name != "dump.sql",
  74. )
  75. t.seek(0)
  76. return t
  77. else:
  78. return dump_db.super(db_name, stream, backup_format)
  79. @patch.monkey_patch(db)
  80. @db.check_db_management_enabled
  81. def restore_db(db, dump_file, copy=False):
  82. res = restore_db.super(db, dump_file, copy)
  83. with osutil.tempdir() as dump_dir:
  84. if zipfile.is_zipfile(dump_file):
  85. with zipfile.ZipFile(dump_file, "r") as zip:
  86. files = [m for m in zip.namelist() if m.startswith("files/")]
  87. if files:
  88. z.extractall(dump_dir, files)
  89. files_path = os.path.join(dump_dir, "files")
  90. shutil.move(files_path, file.get_store_path(db_name))
  91. return res
  92. @patch.monkey_patch(db)
  93. @db.check_db_management_enabled
  94. def exp_rename(old_name, new_name):
  95. res = exp_rename.super(old_name, new_name)
  96. from_files = file.get_store_path(old_name)
  97. to_files = file.get_store_path(new_name)
  98. if os.path.exists(from_files) and not os.path.exists(to_files):
  99. shutil.copytree(from_files, to_files)
  100. return res