def _upload_archive(request): """ Handles archive upload. The uploaded file is stored in memory. We need to extract it and rename it. """ form = UploadArchiveForm(request.POST, request.FILES) response = {'status': -1, 'data': ''} if form.is_valid(): uploaded_file = request.FILES['archive'] # Always a dir if request.fs.isdir(form.cleaned_data['dest']) and posixpath.sep in uploaded_file.name: raise PopupException(_('No "%(sep)s" allowed in the filename %(name)s.' % {'sep': posixpath.sep, 'name': uploaded_file.name})) dest = request.fs.join(form.cleaned_data['dest'], uploaded_file.name) try: # Extract if necessary # Make sure dest path is without the extension if dest.endswith('.zip'): temp_path = archive_factory(uploaded_file, 'zip').extract() if not temp_path: raise PopupException(_('Could not extract contents of file.')) # Move the file to where it belongs dest = dest[:-4] elif dest.endswith('.tar.gz') or dest.endswith('.tgz'): print uploaded_file temp_path = archive_factory(uploaded_file, 'tgz').extract() if not temp_path: raise PopupException(_('Could not extract contents of file.')) # Move the file to where it belongs dest = dest[:-7] else: raise PopupException(_('Could not interpret archive type.')) request.fs.copyFromLocal(temp_path, dest) shutil.rmtree(temp_path) response['status'] = 0 except IOError, ex: already_exists = False try: already_exists = request.fs.exists(dest) except Exception: pass if already_exists: msg = _('Destination %(name)s already exists.') % {'name': dest} else: msg = _('Copy to %(name)s failed: %(error)s') % {'name': dest, 'error': ex} raise PopupException(msg) response.update({ 'path': dest, 'result': _massage_stats(request, request.fs.stats(dest)), 'next': request.GET.get("next") }) return response
def _upload_archive(request): """ Handles archive upload. The uploaded file is stored in memory. We need to extract it and rename it. """ form = UploadArchiveForm(request.POST, request.FILES) response = {'status': -1, 'data': ''} if form.is_valid(): uploaded_file = request.FILES['archive'] # Always a dir if request.fs.isdir(form.cleaned_data['dest']) and posixpath.sep in uploaded_file.name: raise PopupException(_('No "%(sep)s" allowed in the filename %(name)s.' % {'sep': posixpath.sep, 'name': uploaded_file.name})) dest = request.fs.join(form.cleaned_data['dest'], uploaded_file.name) try: # Extract if necessary # Make sure dest path is without the extension if dest.endswith('.zip'): temp_path = archive_factory(uploaded_file, 'zip').extract() if not temp_path: raise PopupException(_('Could not extract contents of file.')) # Move the file to where it belongs dest = dest[:-4] elif dest.endswith('.tar.gz'): print uploaded_file temp_path = archive_factory(uploaded_file, 'tgz').extract() if not temp_path: raise PopupException(_('Could not extract contents of file.')) # Move the file to where it belongs dest = dest[:-7] else: raise PopupException(_('Could not interpret archive type.')) request.fs.copyFromLocal(temp_path, dest) shutil.rmtree(temp_path) response['status'] = 0 except IOError, ex: already_exists = False try: already_exists = request.fs.exists(dest) except Exception: pass if already_exists: msg = _('Destination %(name)s already exists.') % {'name': dest} else: msg = _('Copy to %(name)s failed: %(error)s') % {'name': dest, 'error': ex} raise PopupException(msg) response.update({ 'path': dest, 'result': _massage_stats(request, request.fs.stats(dest)), 'next': request.GET.get("next") }) return response
def import_coordinator(request): coordinator = Coordinator(owner=request.user, schema_version="uri:oozie:coordinator:0.2") if request.method == 'POST': coordinator_form = ImportCoordinatorForm(request.POST, request.FILES, instance=coordinator, user=request.user) if coordinator_form.is_valid(): coordinator_definition = coordinator_form.cleaned_data['definition_file'].read() try: _import_coordinator(coordinator=coordinator, coordinator_definition=coordinator_definition) coordinator.managed = True coordinator.name = coordinator_form.cleaned_data.get('name') coordinator.save() except Exception, e: request.error(_('Could not import coordinator: %s' % e)) raise PopupException(_('Could not import coordinator.'), detail=e) if coordinator_form.cleaned_data.get('resource_archive'): # Upload resources to workspace source = coordinator_form.cleaned_data.get('resource_archive') if source.name.endswith('.zip'): temp_path = archive_factory(source).extract() request.fs.copyFromLocal(temp_path, coordinator.deployment_dir) shutil.rmtree(temp_path) else: Coordinator.objects.filter(id=coordinator.id).delete() raise PopupException(_('Archive should be a Zip.')) Document.objects.link(coordinator, owner=request.user, name=coordinator.name, description=coordinator.description) request.info(_('Coordinator imported')) return redirect(reverse('oozie:edit_coordinator', kwargs={'coordinator': coordinator.id})) else: request.error(_('Errors on the form'))
def import_workflow(request): if ENABLE_V2.get(): raise PopupException( '/oozie/import_workflow is deprecated in the version 2 of Editor') workflow = Workflow.objects.new_workflow(request.user) if request.method == 'POST': workflow_form = ImportWorkflowForm(request.POST, request.FILES, instance=workflow) if workflow_form.is_valid(): if workflow_form.cleaned_data.get('resource_archive'): # Upload resources to workspace source = workflow_form.cleaned_data.get('resource_archive') if source.name.endswith('.zip'): workflow.save() Workflow.objects.initialize(workflow, request.fs) temp_path = archive_factory(source).extract() request.fs.copyFromLocal(temp_path, workflow.deployment_dir) shutil.rmtree(temp_path) else: raise PopupException(_('Archive should be a Zip.')) else: workflow.save() Workflow.objects.initialize(workflow, request.fs) workflow.managed = True workflow.save() workflow_definition = workflow_form.cleaned_data[ 'definition_file'].read() try: _import_workflow(fs=request.fs, workflow=workflow, workflow_definition=workflow_definition) request.info(_('Workflow imported')) return redirect( reverse('oozie:edit_workflow', kwargs={'workflow': workflow.id})) except Exception as e: request.error(_('Could not import workflow: %s' % e)) Workflow.objects.destroy(workflow, request.fs) raise PopupException(_('Could not import workflow.'), detail=e) else: request.error(_('Errors on the form: %s') % workflow_form.errors) else: workflow_form = ImportWorkflowForm(instance=workflow) return render('editor/import_workflow.mako', request, { 'workflow_form': workflow_form, 'workflow': workflow, })
def import_workflow(request): if ENABLE_V2.get(): raise PopupException('/oozie/import_workflow is deprecated in the version 2 of Editor') workflow = Workflow.objects.new_workflow(request.user) if request.method == 'POST': workflow_form = ImportWorkflowForm(request.POST, request.FILES, instance=workflow) if workflow_form.is_valid(): if workflow_form.cleaned_data.get('resource_archive'): # Upload resources to workspace source = workflow_form.cleaned_data.get('resource_archive') if source.name.endswith('.zip'): workflow.save() Workflow.objects.initialize(workflow, request.fs) temp_path = archive_factory(source).extract() request.fs.copyFromLocal(temp_path, workflow.deployment_dir) shutil.rmtree(temp_path) else: raise PopupException(_('Archive should be a Zip.')) else: workflow.save() Workflow.objects.initialize(workflow, request.fs) workflow.managed = True workflow.save() workflow_definition = workflow_form.cleaned_data['definition_file'].read() try: _import_workflow(fs=request.fs, workflow=workflow, workflow_definition=workflow_definition) request.info(_('Workflow imported')) return redirect(reverse('oozie:edit_workflow', kwargs={'workflow': workflow.id})) except Exception, e: request.error(_('Could not import workflow: %s' % e)) Workflow.objects.destroy(workflow, request.fs) raise PopupException(_('Could not import workflow.'), detail=e) else: request.error(_('Errors on the form: %s') % workflow_form.errors)
def import_workflow(request): workflow = Workflow.objects.new_workflow(request.user) if request.method == "POST": workflow_form = ImportWorkflowForm(request.POST, request.FILES, instance=workflow) if workflow_form.is_valid(): if workflow_form.cleaned_data.get("resource_archive"): # Upload resources to workspace source = workflow_form.cleaned_data.get("resource_archive") if source.name.endswith(".zip"): workflow.save() Workflow.objects.initialize(workflow, request.fs) temp_path = archive_factory(source).extract() request.fs.copyFromLocal(temp_path, workflow.deployment_dir) shutil.rmtree(temp_path) else: raise PopupException(_("Archive should be a Zip.")) else: workflow.save() Workflow.objects.initialize(workflow, request.fs) workflow.managed = True workflow.save() workflow_definition = workflow_form.cleaned_data["definition_file"].read() try: _import_workflow(fs=request.fs, workflow=workflow, workflow_definition=workflow_definition) request.info(_("Workflow imported")) return redirect(reverse("oozie:edit_workflow", kwargs={"workflow": workflow.id})) except Exception, e: request.error(_("Could not import workflow: %s" % e)) Workflow.objects.destroy(workflow, request.fs) raise PopupException(_("Could not import workflow."), detail=e) else: request.error(_("Errors on the form: %s") % workflow_form.errors)