def validate_remote_path(request, remote_host_id): response = {} path = request.GET.get('path', None) tardis_object_type = request.GET.get('object_type', None) tardis_object_id = request.GET.get('object_id', None) if tardis_object_type and tardis_object_id: try: response['object_size'] = get_object_size(tardis_object_type, tardis_object_id) except (Experiment.DoesNotExist, Dataset.DoesNotExist, TypeError): response['message'] = "Experiment/dataset/datafile does not exist." return HttpResponseNotFound(json.dumps(response), content_type='application/json') try: remote_host = RemoteHost.objects.get(pk=remote_host_id) credential = get_credential(request, remote_host) ssh = credential.get_client_for_host(remote_host) sftp_client = ssh.open_sftp() response['default'] = {} response['default']['path'] = get_default_push_location(sftp_client) response['default']['free_space'] = bytes_available( ssh, response['default']['path']) response['default']['valid_children'] = list_subdirectories( sftp_client, response['default']['path']) if 'object_size' in response: response['default']['sufficient_space'] = response['default'][ 'free_space'] > \ response['object_size'] if path is not None: path_parts = path.split('/') valid_parts = [] invalid_parts = [] for part in path_parts: if not invalid_parts: test_path = '/'.join(valid_parts + [part]) try: sftp_client.chdir(test_path) valid_parts.append(part) except IOError: invalid_parts.append(part) else: invalid_parts.append(part) response[path] = {} response[path]['valid_parts'] = '/'.join(valid_parts) response[path]['invalid_parts'] = '/'.join(invalid_parts) response[path]['valid_children'] = list_subdirectories( sftp_client, response[path]['valid_parts']) response[path]['free_space'] = bytes_available( ssh, response[path]['valid_parts']) if 'object_size' in response: response[path]['sufficient_space'] = \ response[path]['free_space'] > response['object_size'] except NoSuitableCredential: response['message'] = "You don't have access to this host." return HttpResponseForbidden(json.dumps(response), content_type='application/json') except RemoteHost.DoesNotExist: response['message'] = "Remote host does not exist." return HttpResponseNotFound(json.dumps(response), content_type='application/json') return HttpResponse(json.dumps(response), content_type='application/json')
def validate_remote_path(request, remote_host_id): response = {} path = request.GET.get('path', None) tardis_object_type = request.GET.get('object_type', None) tardis_object_id = request.GET.get('object_id', None) if tardis_object_type and tardis_object_id: try: response['object_size'] = get_object_size(tardis_object_type, tardis_object_id) except (Experiment.DoesNotExist, Dataset.DoesNotExist, TypeError): response['message'] = "Experiment/dataset/datafile does not exist." return HttpResponseNotFound(json.dumps(response), content_type='application/json') try: remote_host = RemoteHost.objects.get(pk=remote_host_id) credential = get_credential(request, remote_host) ssh = credential.get_client_for_host(remote_host) sftp_client = ssh.open_sftp() response['default'] = {} response['default']['path'] = get_default_push_location(sftp_client) response['default']['free_space'] = bytes_available(ssh, response[ 'default']['path']) response['default']['valid_children'] = list_subdirectories( sftp_client, response['default']['path']) if 'object_size' in response: response['default']['sufficient_space'] = response['default'][ 'free_space'] > \ response['object_size'] if path is not None: path_parts = path.split('/') valid_parts = [] invalid_parts = [] for part in path_parts: if not invalid_parts: test_path = '/'.join(valid_parts + [part]) try: sftp_client.chdir(test_path) valid_parts.append(part) except IOError: invalid_parts.append(part) else: invalid_parts.append(part) response[path] = {} response[path]['valid_parts'] = '/'.join(valid_parts) response[path]['invalid_parts'] = '/'.join(invalid_parts) response[path]['valid_children'] = list_subdirectories( sftp_client, response[path]['valid_parts']) response[path]['free_space'] = bytes_available( ssh, response[path]['valid_parts']) if 'object_size' in response: response[path]['sufficient_space'] = \ response[path]['free_space'] > response['object_size'] except NoSuitableCredential: response['message'] = "You don't have access to this host." return HttpResponseForbidden(json.dumps(response), content_type='application/json') except RemoteHost.DoesNotExist: response['message'] = "Remote host does not exist." return HttpResponseNotFound(json.dumps(response), content_type='application/json') return HttpResponse(json.dumps(response), content_type='application/json')