Просмотр исходного кода

HUE-9116 [hive] Check if destination empty before export data to hdfs

Ying Chen 5 лет назад
Родитель
Сommit
69beb299e9
2 измененных файлов с 23 добавлено и 0 удалено
  1. 3 0
      desktop/libs/notebook/src/notebook/api.py
  2. 20 0
      desktop/libs/notebook/src/notebook/tests.py

+ 3 - 0
desktop/libs/notebook/src/notebook/api.py

@@ -33,6 +33,7 @@ from azure.abfs.__init__ import abfspath
 from desktop.conf import TASK_SERVER
 from desktop.conf import TASK_SERVER
 from desktop.lib.i18n import smart_str
 from desktop.lib.i18n import smart_str
 from desktop.lib.django_util import JsonResponse
 from desktop.lib.django_util import JsonResponse
+from desktop.lib.exceptions_renderable import PopupException
 from desktop.models import Document2, Document, __paginate, _get_gist_document
 from desktop.models import Document2, Document, __paginate, _get_gist_document
 from indexer.file_format import HiveFormat
 from indexer.file_format import HiveFormat
 from indexer.fields import Field
 from indexer.fields import Field
@@ -800,6 +801,8 @@ def export_result(request):
   elif data_format == 'hdfs-directory':
   elif data_format == 'hdfs-directory':
     if destination.lower().startswith("abfs"):
     if destination.lower().startswith("abfs"):
       destination = abfspath(destination)
       destination = abfspath(destination)
+    if request.fs.exists(destination) and request.fs.listdir_stats(destination):
+      raise PopupException(_('The destination is not an empty directory!'))
     if is_embedded:
     if is_embedded:
       sql, success_url = api.export_large_data_to_hdfs(notebook, snippet, destination)
       sql, success_url = api.export_large_data_to_hdfs(notebook, snippet, destination)
 
 

+ 20 - 0
desktop/libs/notebook/src/notebook/tests.py

@@ -348,8 +348,15 @@ class MockFs(object):
     return ''
     return ''
 
 
   def exists(self, path):
   def exists(self, path):
+    if path == '/user/hue/non_exists_directory':
+      return False
     return True
     return True
 
 
+  def listdir_stats(self, path):
+    if path == '/user/hue/non_empty_directory':
+      return ['mock_dir', 'mock_file']
+    return []
+
   def isdir(self, path):
   def isdir(self, path):
     return path == '/user/hue'
     return path == '/user/hue'
 
 
@@ -456,6 +463,19 @@ class TestNotebookApiMocked(object):
       assert_equal('adl:/user/hue/path.csv', data['watch_url']['destination'], data)
       assert_equal('adl:/user/hue/path.csv', data['watch_url']['destination'], data)
 
 
 
 
+    response = self.client.post(reverse('notebook:export_result'), {
+      'notebook': notebook_json,
+      'snippet': json.dumps(json.loads(notebook_json)['snippets'][0]),
+      'format': json.dumps('hdfs-directory'),
+      'destination': json.dumps('/user/hue/non_empty_directory'),
+      'overwrite': json.dumps(False)
+    })
+
+    data = json.loads(response.content)
+    assert_equal(-1, data['status'], data)
+    assert_equal('The destination is not a empty directory!', data['message'], data)
+
+
   def test_download_result(self):
   def test_download_result(self):
     notebook_json = """
     notebook_json = """
       {
       {