template.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 os
  17. import click
  18. import requests
  19. from contextlib import closing
  20. from flow_client.flow_cli.utils import cli_args
  21. from flow_client.flow_cli.utils.cli_utils import (preprocess, download_from_request, access_server, prettify)
  22. @click.group(short_help="Template Operations")
  23. @click.pass_context
  24. def template(ctx):
  25. """
  26. \b
  27. fate template file download
  28. """
  29. pass
  30. @template.command("download", short_help="Template Download Command")
  31. @cli_args.MIN_DATA
  32. @cli_args.OUTPUT_PATH
  33. @click.pass_context
  34. def download(ctx, **kwargs):
  35. """
  36. \b
  37. - DESCRIPTION:
  38. Download template conf/dsl/data files
  39. \b
  40. - USAGE:
  41. flow template download --min-data 1 --output-path ./examples/
  42. """
  43. config_data, dsl_data = preprocess(**kwargs)
  44. tar_file_name = 'template.tar.gz'
  45. extract_dir = config_data['output_path']
  46. with closing(access_server('post', ctx, 'template/download', config_data, False, stream=True)) as response:
  47. if response.status_code == 200:
  48. download_from_request(http_response=response, tar_file_name=tar_file_name, extract_dir=extract_dir)
  49. res = {'retcode': 0,
  50. 'directory': extract_dir,
  51. 'retmsg': 'download successfully, please check {} directory'.format(extract_dir)}
  52. else:
  53. res = response.json() if isinstance(response, requests.models.Response) else response
  54. prettify(res)