template_app.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import io
  17. import os
  18. import tarfile
  19. from flask import send_file, request
  20. from fate_arch.common import file_utils
  21. from fate_flow.settings import TEMPLATE_INFO_PATH
  22. from fate_flow.utils.base_utils import get_fate_flow_directory
  23. @manager.route('/download', methods=['post'])
  24. def template_download():
  25. min_data = request.json.get("min_data", False) if request.json else False
  26. memory_file = io.BytesIO()
  27. dir_dict = {}
  28. template_info = file_utils.load_yaml_conf(TEMPLATE_INFO_PATH)
  29. data_dir = template_info.get("template_data", {}).get("base_dir")
  30. min_data_file = template_info.get("template_data", {}).get("min_data", [])
  31. for name, dir_name in template_info.get("template_path", {}).items():
  32. dir_dict[name] = os.path.join(get_fate_flow_directory(), dir_name)
  33. delete_dir_list = []
  34. for name, dir_list in template_info.get("delete_path").items():
  35. for dir_name in dir_list:
  36. delete_dir_list.append(os.path.join(dir_dict[name], dir_name))
  37. tar = tarfile.open(fileobj=memory_file, mode='w:gz')
  38. for name, base_dir in dir_dict.items():
  39. for root, dir, files in os.walk(base_dir):
  40. for file in files:
  41. if min_data:
  42. if data_dir in root and file not in min_data_file:
  43. continue
  44. if root in delete_dir_list:
  45. continue
  46. full_path = os.path.join(root, file)
  47. rel_path = os.path.join(name, os.path.relpath(full_path, base_dir))
  48. tar.add(full_path, rel_path)
  49. tar.close()
  50. memory_file.seek(0)
  51. return send_file(memory_file, attachment_filename=f'template.tar.gz', as_attachment=True)