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.

104 lines
3.8 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 os
  23. import json
  24. import uuid
  25. import logging
  26. import shutil
  27. import zipfile
  28. import tempfile
  29. from contextlib import closing
  30. from odoo import _, modules, api, sql_db, SUPERUSER_ID
  31. from odoo.tools import osutil, config, exec_pg_command
  32. from odoo.service import db
  33. from odoo.addons.muk_utils.tools import patch
  34. from odoo.addons.muk_fields_file.fields import file
  35. _logger = logging.getLogger(__name__)
  36. @patch.monkey_patch(db)
  37. @db.check_db_management_enabled
  38. def exp_duplicate_database(db_original_name, db_name):
  39. res = exp_duplicate_database.super(db_original_name, db_name)
  40. from_files = file.get_store_path(db_original_name)
  41. to_files = file.get_store_path(db_name)
  42. if os.path.exists(from_files) and not os.path.exists(to_files):
  43. shutil.copytree(from_files, to_files)
  44. return res
  45. @patch.monkey_patch(db)
  46. @db.check_db_management_enabled
  47. def exp_drop(db_name):
  48. res = exp_drop.super(db_name)
  49. files = file.get_store_path(db_name)
  50. if os.path.exists(files):
  51. shutil.rmtree(files)
  52. return res
  53. @patch.monkey_patch(db)
  54. @db.check_db_management_enabled
  55. def dump_db(db_name, stream, backup_format='zip'):
  56. if backup_format == 'zip':
  57. res = dump_db.super(db_name, False, backup_format)
  58. with osutil.tempdir() as dump_dir:
  59. with zipfile.ZipFile(res, 'r') as zip:
  60. zip.extractall(dump_dir)
  61. files = file.get_store_path(db_name)
  62. if os.path.exists(files):
  63. shutil.copytree(files, os.path.join(dump_dir, 'files'))
  64. if stream:
  65. osutil.zip_dir(dump_dir, stream, include_dir=False, fnct_sort=lambda file_name: file_name != 'dump.sql')
  66. else:
  67. t=tempfile.TemporaryFile()
  68. osutil.zip_dir(dump_dir, t, include_dir=False, fnct_sort=lambda file_name: file_name != 'dump.sql')
  69. t.seek(0)
  70. return t
  71. else:
  72. return dump_db.super(db_name, stream, backup_format)
  73. @patch.monkey_patch(db)
  74. @db.check_db_management_enabled
  75. def restore_db(db, dump_file, copy=False):
  76. res = restore_db.super(db, dump_file, copy)
  77. with osutil.tempdir() as dump_dir:
  78. if zipfile.is_zipfile(dump_file):
  79. with zipfile.ZipFile(dump_file, 'r') as zip:
  80. files = [m for m in zip.namelist() if m.startswith('files/')]
  81. if files:
  82. z.extractall(dump_dir, files)
  83. files_path = os.path.join(dump_dir, 'files')
  84. shutil.move(files_path, file.get_store_path(db_name))
  85. return res
  86. @patch.monkey_patch(db)
  87. @db.check_db_management_enabled
  88. def exp_rename(old_name, new_name):
  89. res = exp_rename.super(old_name, new_name)
  90. from_files = file.get_store_path(old_name)
  91. to_files = file.get_store_path(new_name)
  92. if os.path.exists(from_files) and not os.path.exists(to_files):
  93. shutil.copytree(from_files, to_files)
  94. return res