data.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 click
  17. import os
  18. import sys
  19. from flow_client.flow_cli.utils import cli_args
  20. from flow_client.flow_cli.utils.cli_utils import preprocess, access_server, check_abs_path, prettify
  21. from requests_toolbelt import MultipartEncoder, MultipartEncoderMonitor
  22. import json
  23. @click.group(short_help="Data Operations")
  24. @click.pass_context
  25. def data(ctx):
  26. """
  27. \b
  28. Provides numbers of data operational commands, including upload, download and etc.
  29. For more details, please check out the help text.
  30. """
  31. pass
  32. @data.command("upload", short_help="Upload Table Command")
  33. @cli_args.CONF_PATH
  34. @click.option('--verbose', is_flag=True, default=False,
  35. help="If specified, verbose mode will be turn on. "
  36. "Users can have feedback on upload task in progress. (default: False)")
  37. @click.option('--drop', is_flag=True, default=False,
  38. help="If specified, data of old version would be replaced by the current version. "
  39. "Otherwise, current upload task would be rejected. (default: False)")
  40. @click.pass_context
  41. def upload(ctx, **kwargs):
  42. """
  43. \b
  44. - DESCRIPTION:
  45. Upload Data Table.
  46. \b
  47. - Usage:
  48. flow data upload -c fateflow/examples/upload/upload_guest.json
  49. flow data upload -c fateflow/examples/upload/upload_host.json --verbose --drop
  50. """
  51. kwargs['drop'] = 1 if kwargs['drop'] else 0
  52. kwargs['verbose'] = int(kwargs['verbose'])
  53. config_data, dsl_data = preprocess(**kwargs)
  54. if config_data.get('use_local_data', 1):
  55. file_name = check_abs_path(config_data.get('file'))
  56. if os.path.exists(file_name):
  57. with open(file_name, 'rb') as fp:
  58. data = MultipartEncoder(
  59. fields={'file': (os.path.basename(file_name), fp, 'application/octet-stream')}
  60. )
  61. tag = [0]
  62. def read_callback(monitor):
  63. if config_data.get('verbose') == 1:
  64. sys.stdout.write("\r UPLOADING:{0}{1}".format(
  65. "|" * (monitor.bytes_read * 100 // monitor.len), '%.2f%%' % (monitor.bytes_read * 100 // monitor.len)))
  66. sys.stdout.flush()
  67. if monitor.bytes_read / monitor.len == 1:
  68. tag[0] += 1
  69. if tag[0] == 2:
  70. sys.stdout.write('\n')
  71. data = MultipartEncoderMonitor(data, read_callback)
  72. access_server('post', ctx, 'data/upload', json_data=None, data=data,
  73. params=json.dumps(config_data), headers={'Content-Type': data.content_type})
  74. else:
  75. prettify(
  76. {
  77. "retcode": 100,
  78. "retmsg": "The file is obtained from the fate flow client machine, but it does not exist, "
  79. "please check the path: {}".format(file_name)
  80. }
  81. )
  82. else:
  83. access_server('post', ctx, 'data/upload', config_data)
  84. @data.command("download", short_help="Download Table Command")
  85. @cli_args.CONF_PATH
  86. @click.pass_context
  87. def download(ctx, **kwargs):
  88. """
  89. \b
  90. - DESCRIPTION:
  91. Download Data Table.
  92. \b
  93. - Usage:
  94. flow data download -c fateflow/examples/download/download_table.json
  95. """
  96. config_data, dsl_data = preprocess(**kwargs)
  97. access_server('post', ctx, "data/download", config_data)
  98. @data.command("writer", short_help="write Table Command")
  99. @cli_args.CONF_PATH
  100. @click.pass_context
  101. def writer(ctx, **kwargs):
  102. """
  103. \b
  104. - DESCRIPTION:
  105. Download Data Table.
  106. \b
  107. - Usage:
  108. flow data download -c fateflow/examples/writer/external_storage.json
  109. """
  110. config_data, dsl_data = preprocess(**kwargs)
  111. access_server('post', ctx, "data/writer", config_data)
  112. @data.command("upload-history", short_help="Upload History Command")
  113. @cli_args.LIMIT
  114. @cli_args.JOBID
  115. @click.pass_context
  116. def upload_history(ctx, **kwargs):
  117. """
  118. \b
  119. - DESCRIPTION:
  120. Query Upload Table History.
  121. \b
  122. - USAGE:
  123. flow data upload-history -l 20
  124. flow data upload-history --job-id $JOB_ID
  125. """
  126. config_data, dsl_data = preprocess(**kwargs)
  127. access_server('post', ctx, "data/upload/history", config_data)
  128. # @data.command(short_help="")
  129. @click.pass_context
  130. def download_history(ctx):
  131. """
  132. """
  133. pass