瀏覽代碼

HUE-5202 Move extract functionality to desktop/lib/tasks/extract_archive

krish 9 年之前
父節點
當前提交
0ed940a

+ 1 - 23
apps/filebrowser/src/filebrowser/views.py

@@ -54,6 +54,7 @@ from desktop.lib.django_util import JsonResponse
 from desktop.lib.exceptions_renderable import PopupException
 from desktop.lib.fs import splitpath
 from desktop.lib.i18n import smart_str
+from desktop.lib.tasks.extract_archive.extract_utils import extract_archive_in_hdfs
 from hadoop.fs.hadoopfs import Hdfs
 from hadoop.fs.exceptions import WebHdfsException
 from hadoop.fs.fsutils import do_overwrite_save
@@ -1352,29 +1353,6 @@ def _upload_archive(request):
     else:
         raise PopupException(_("Error in upload form: %s") % (form.errors,))
 
-def extract_archive_in_hdfs(request, upload_path, file_name):
-
-  _upload_extract_archive_script_to_hdfs(request.fs)
-
-  from notebook.connectors.base import Notebook
-
-  shell_notebook = Notebook()
-  shell_notebook.add_shell_snippet(shell_command='extract_archive_in_hdfs.sh',
-                                   arguments=[{'value': '-u=' + upload_path}, {'value': '-f=' + file_name}],
-                                   archives=[],
-                                   files=[{'value': '/user/hue/common/extract_archive_in_hdfs.sh'}, {"value": upload_path + '/' + file_name}],
-                                   env_var=[{'value': 'HADOOP_USER_NAME=${wf:user()}'}])
-  return shell_notebook.execute(request, batch=True)
-
-def _upload_extract_archive_script_to_hdfs(fs):
-  if not fs.exists('/user/hue/common/'):
-    fs.do_as_user('hue', fs.mkdir, '/user/hue/common/')
-    fs.do_as_user('hue', fs.chmod, '/user/hue/common/', 0755)
-
-  if not fs.do_as_user('hue', fs.exists, '/user/hue/common/extract_archive_in_hdfs.sh'):
-    fs.do_as_user('hue', fs.copyFromLocal, 'desktop/core/src/desktop/lib/extract_archive_in_hdfs.sh',
-                          '/user/hue/common/extract_archive_in_hdfs.sh')
-    fs.do_as_user('hue', fs.chmod, '/user/hue/common/', 0755)
 
 def status(request):
     status = request.fs.status()

+ 16 - 0
desktop/core/src/desktop/lib/tasks/__init__.py

@@ -0,0 +1,16 @@
+#!/usr/bin/env python
+# Licensed to Cloudera, Inc. under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  Cloudera, Inc. licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.

+ 16 - 0
desktop/core/src/desktop/lib/tasks/extract_archive/__init__.py

@@ -0,0 +1,16 @@
+#!/usr/bin/env python
+# Licensed to Cloudera, Inc. under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  Cloudera, Inc. licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.

+ 5 - 5
desktop/core/src/desktop/lib/extract_archive_in_hdfs.sh → desktop/core/src/desktop/lib/tasks/extract_archive/extract_in_hdfs.sh

@@ -55,7 +55,7 @@ then
 	echo "ERROR: Missing Arguments"
 	usage
 	exit 1
-fi 
+fi
 
 exit_status=0
 
@@ -66,15 +66,15 @@ temp_output_dir=`mktemp -d 2>/dev/null || mktemp -d -t 'mytmpdir'`
 echo 'Created temporary output directory: '$temp_output_dir
 
 set -x
-if [[ $FILE_NAME =~ \.zip$ ]] 
+if [[ $FILE_NAME =~ \.zip$ ]]
 then
 	unzip $FILE_NAME -d $temp_output_dir
 	exit_status=$(echo $?)
-elif [[ $FILE_NAME =~ \.tar\.gz$ ]] || [[ $FILE_NAME =~ \.tgz$ ]] 
+elif [[ $FILE_NAME =~ \.tar\.gz$ ]] || [[ $FILE_NAME =~ \.tgz$ ]]
 then
 	tar -xvzf $FILE_NAME -C $temp_output_dir
 	exit_status=$(echo $?)
-elif [[ $FILE_NAME =~ \.bz2$ ]] || [[ $FILE_NAME =~ \.bzip2$ ]] 
+elif [[ $FILE_NAME =~ \.bz2$ ]] || [[ $FILE_NAME =~ \.bzip2$ ]]
 then
 	bzip2 -dc $FILE_NAME > $temp_output_dir/$filename_without_extension
 	exit_status=$(echo $?)
@@ -98,4 +98,4 @@ fi
 rm -rf $temp_output_dir
 echo 'Deleted temporary output directory: '$temp_output_dir
 
-exit $exit_status
+exit $exit_status

+ 42 - 0
desktop/core/src/desktop/lib/tasks/extract_archive/extract_utils.py

@@ -0,0 +1,42 @@
+#!/usr/bin/env python
+# Licensed to Cloudera, Inc. under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  Cloudera, Inc. licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from desktop.conf import DEFAULT_USER
+from notebook.connectors.base import Notebook
+
+def extract_archive_in_hdfs(request, upload_path, file_name):
+
+  _upload_extract_archive_script_to_hdfs(request.fs)
+
+  shell_notebook = Notebook()
+  shell_notebook.add_shell_snippet(
+      shell_command='extract_archive_in_hdfs.sh',
+      arguments=[{'value': '-u=' + upload_path}, {'value': '-f=' + file_name}],
+      archives=[],
+      files=[{'value': '/user/hue/common/extract_archive_in_hdfs.sh'}, {"value": upload_path + '/' + file_name}],
+      env_var=[{'value': 'HADOOP_USER_NAME=${wf:user()}'}])
+  return shell_notebook.execute(request, batch=True)
+
+def _upload_extract_archive_script_to_hdfs(fs):
+  if not fs.exists('/user/hue/common/'):
+    fs.do_as_user(DEFAULT_USER.get(), fs.mkdir, '/user/hue/common/')
+    fs.do_as_user(DEFAULT_USER.get(), fs.chmod, '/user/hue/common/', 0755)
+
+  if not fs.do_as_user(DEFAULT_USER.get(), fs.exists, '/user/hue/common/extract_archive_in_hdfs.sh'):
+    fs.do_as_user(DEFAULT_USER.get(), fs.copyFromLocal, 'desktop/core/src/desktop/lib/extract_archive_in_hdfs.sh',
+                          '/user/hue/common/extract_archive_in_hdfs.sh')
+    fs.do_as_user(DEFAULT_USER.get(), fs.chmod, '/user/hue/common/', 0755)