Example #1
0
    def post(self, request):
        user = self.request.user
        data = json.loads(request.body)
        service = BundleService(self.request.user)
        if data.get('raw_command', None):
            data['command'] = service.get_command(data['raw_command'])
        if not data.get('worksheet_uuid', None) or not data.get(
                'command', None):
            return Response("Must have worksheet uuid and command",
                            status=status.HTTP_400_BAD_REQUEST)

        service = BundleService(self.request.user)

        # If 'autocomplete' field is set, return a list of completions instead
        if data.get('autocomplete', False):
            return Response({
                'completions':
                service.complete_command(data['worksheet_uuid'],
                                         data['command'])
            })

        result = service.general_command(data['worksheet_uuid'],
                                         data['command'])
        if result['exception'] is None:
            return Response({
                'success': True,
                'data': result,
                'input_data': data
            })
        else:
            return Response(result['exception'], status=500)
Example #2
0
 def get(self, request, uuid, path=''):
     user_id = self.request.user.id
     logger.debug("BundleContent: user_id=%s; uuid=%s; path=%s.", user_id,
                  uuid, path)
     service = BundleService(self.request.user)
     try:
         target = (uuid, path)
         info = service.get_target_info(target,
                                        2)  # 2 is the depth to retrieve
         info['stdout'] = None
         info['stderr'] = None
         #if we have std out or err update it.
         contents = info.get('contents')
         if contents:
             contents = sorted(contents, key=lambda r: r['name'])
             for item in contents:
                 if item['name'] in ['stdout', 'stderr']:
                     lines = service.head_target((uuid, item['name']), 100)
                     if lines:
                         import base64
                         lines = ' '.join(map(base64.b64decode, lines))
                         info[item['name']] = lines
         return Response(info)
     except Exception as e:
         tb = traceback.format_exc()
         log_exception(self, e, tb)
         return Response({'error': smart_str(e)})
Example #3
0
    def get_context_data(self, **kwargs):
        context = super(BundleListView, self).get_context_data(**kwargs)
        service = BundleService(self.request.user)
        results = service.items()
        context['bundles'] = results

        bundles = results
        items = []
        for bundle in bundles:
            item = {'uuid': bundle['uuid'],
                    'details_url': '/bundles/{0}'.format(bundle['uuid']),
                    'name': '',
                    'title': '<title not specified>',
                    'creator': '<creator not specified>',
                    'description': '<description not specified>'}
            if 'metadata' in bundle:
                metadata = bundle['metadata']
                for (key1, key2) in [('title', 'name'), ('creator', None), ('description', None)]:
                    if key2 is None:
                        key2 = key1
                    if key2 in metadata:
                        item[key1] = metadata[key2]
            items.append(item)
        context['items'] = items
        context['items_label'] = 'bundles'

        return context
Example #4
0
 def get_context_data(self, **kwargs):
     context = super(BundleDetailView, self).get_context_data(**kwargs)
     uuid = kwargs.get('uuid')
     service = BundleService(self.request.user)
     results = service.item(uuid)
     context['bundle'] = results
     return context
Example #5
0
    def get(self, request, uuid):
        user_id = self.request.user.id
        logger.debug("BundleInfo: user_id=%s; uuid=%s.", user_id, uuid)
        service = BundleService(self.request.user)
        try:
            bundle_info = service.get_bundle_info(uuid)
            target = (uuid, '')
            info = service.get_target_info(target,
                                           2)  # 2 is the depth to retrieve
            bundle_info['stdout'] = None
            bundle_info['stderr'] = None
            #if we have std out or err update it.
            contents = info.get('contents')
            if contents:
                for item in contents:
                    if item['name'] in ['stdout', 'stderr']:
                        lines = service.head_target((uuid, item['name']), 100)
                        if lines:
                            import base64
                            lines = ' '.join(map(base64.b64decode, lines))
                            bundle_info[item['name']] = lines

            bundle_info['edit_permission'] = False
            if bundle_info['owner_id'] == str(self.request.user.id):
                bundle_info['edit_permission'] = True
            return Response(bundle_info, content_type="application/json")
        except Exception as e:
            tb = traceback.format_exc()
            log_exception(self, e, tb)
            return Response({"error": smart_str(e)}, status=500)
Example #6
0
 def get(self, request):
     user_id = self.request.user.id
     logger.debug("WorksheetsListApi: user_id=%s.", user_id)
     service = BundleService(self.request.user)
     try:
         worksheets = service.worksheets()
         user_ids = []
         user_id_to_worksheets = {}
         for worksheet in worksheets:
             owner_id = worksheet['owner_id']
             if owner_id in user_id_to_worksheets:
                 user_id_to_worksheets[owner_id].append(worksheet)
             else:
                 user_id_to_worksheets[owner_id] = [worksheet]
                 user_ids.append(owner_id)
         if len(user_ids) > 0:
             users = ClUser.objects.filter(id__in=user_ids)
             for user in users:
                 for worksheet in user_id_to_worksheets[str(user.id)]:
                     worksheet['owner'] = user.username
         return Response(worksheets)
     except Exception as e:
         tb = traceback.format_exc()
         log_exception(self, e, tb)
         return Response({"error": smart_str(e)}, status=500)
Example #7
0
 def get(self, request, uuid, path):
     user_id = self.request.user.id
     service = BundleService(self.request.user)
     try:
         content_type = BundleFileContentApi._content_type(path)
         return StreamingHttpResponse(service.read_file(uuid, path), content_type=content_type)
     except Exception as e:
         return Response(status=service.http_status_from_exception(e))
Example #8
0
 def get(self, request):
     user_id = self.request.user.id
     logger.debug("WorksheetsListApi: user_id=%s.", user_id)
     service = BundleService()
     try:
         worksheets = service.worksheets()
         return Response(worksheets)
     except Exception as e:
         return Response(status=service.http_status_from_exception(e))
Example #9
0
 def get(self, request, uuid):
     user_id = self.request.user.id
     logger.debug("BundleInfo: user_id=%s; uuid=%s.", user_id, uuid)
     service = BundleService(self.request.user)
     try:
         item = service.item(uuid)
         return Response(item, content_type="application/json")
     except Exception as e:
         return Response(status=service.http_status_from_exception(e))
Example #10
0
 def get(self, request, uuid):
     user_id = self.request.user.id
     logger.debug("WorksheetContent: user_id=%s; uuid=%s.", user_id, uuid)
     service = BundleService()
     try:
         worksheet = service.worksheet(uuid)
         return Response(worksheet)
     except Exception as e:
         return Response(status=service.http_status_from_exception(e))
Example #11
0
 def get(self, request, uuid, path):
     user_id = self.request.user.id
     logger.debug("BundleContent: user_id=%s; uuid=%s; path=%s.", user_id, uuid, path)
     service = BundleService(self.request.user)
     try:
         items = service.ls(uuid, path)
         return Response(items)
     except Exception as e:
         return Response(status=service.http_status_from_exception(e))
Example #12
0
 def get(self, request, uuid):
     user_id = self.request.user.id
     logger.debug("WorksheetContent: user_id=%s; uuid=%s.", user_id, uuid)
     service = BundleService(self.request.user)
     try:
         worksheet = service.worksheet(uuid)
         owner = ClUser.objects.filter(id=worksheet['owner_id'])[0]
         worksheet['owner'] = owner.username
         return Response(worksheet)
     except Exception as e:
         return Response(status=service.http_status_from_exception(e))
Example #13
0
 def get(self, request):
     user_id = self.request.user.id
     logger.debug("WorksheetsListApi: user_id=%s.", user_id)
     service = BundleService(self.request.user)
     try:
         worksheets = service.worksheets()
         return Response(worksheets)
     except Exception as e:
         tb = traceback.format_exc()
         log_exception(self, e, tb)
         return Response({"error": smart_str(e)}, status=500)
Example #14
0
 def get(self, request, uuid):
     user_id = self.request.user.id
     logger.debug("WorksheetContent: user_id=%s; uuid=%s.", user_id, uuid)
     service = BundleService(self.request.user)
     try:
         worksheet = service.full_worksheet(uuid)
         return Response(worksheet)
     except Exception as e:
         tb = traceback.format_exc()
         log_exception(self, e, tb)
         return Response({"error": smart_str(e)}, status=500)
Example #15
0
 def get(self, request, uuid, path):
     service = BundleService(self.request.user)
     try:
         stream, name, content_type = service.read_target((uuid, path))
         response = StreamingHttpResponse(stream, content_type=content_type)
         response['Content-Disposition'] = 'filename="%s"' % name
         return response
     except Exception as e:
         tb = traceback.format_exc()
         log_exception(self, e, tb)
         return Response({"error": smart_str(e)}, status=500)
Example #16
0
 def get(self, request):
     user_id = self.request.user.id
     worksheet_uuid = request.GET.get('worksheet_uuid', '')
     logger.debug("WorksheetsGetBundleListApi: user_id=%s; worksheet_uuid=%s.", user_id, worksheet_uuid)
     service = BundleService(self.request.user)
     try:
         bundle_list = service.get_worksheet_bundles(worksheet_uuid)
         return Response({'bundles': bundle_list}, content_type="application/json")
     except Exception as e:
         tb = traceback.format_exc()
         log_exception(self, e, tb)
         return Response({"error": smart_str(e)}, status=500)
Example #17
0
def recent_worksheets(request_user, limit=3):
    """Used for worksheets in competitions. Issue 1014"""
    service = BundleService(request_user)
    unsorted_worksheets = service.worksheets()


    #if not worksheets:
    #   return worksheets  # just incase the list is empty

    sorted_worksheets = sorted(unsorted_worksheets, key=lambda k: k['id'], reverse=True)
    worksheets = [(val['uuid'], val['name'], val['owner_name']) for val in sorted_worksheets]
    return worksheets[0:limit]
Example #18
0
 def get(self, request):
     user_id = self.request.user.id
     search_string = request.GET.get('spec', '')
     logger.debug("WorksheetsGetUUIDApi: user_id=%s; spec=%s.", user_id, search_string)
     service = BundleService(self.request.user)
     try:
         worksheet_uuid = service.get_worksheet_uuid(search_string)
         return Response({'uuid': worksheet_uuid}, content_type="application/json")
     except Exception as e:
         tb = traceback.format_exc()
         log_exception(self, e, tb)
         return Response({"error": smart_str(e)}, status=500)
Example #19
0
 def get(self, request, uuid):
     user_id = self.request.user.id
     logger.debug("BundleInfo: user_id=%s; uuid=%s.", user_id, uuid)
     service = BundleService(self.request.user)
     try:
         bundle_info = service.get_bundle_info(uuid)
         bundle_info.update(service.get_bundle_contents(uuid))
         return Response(bundle_info, content_type="application/json")
     except Exception as e:
         tb = traceback.format_exc()
         log_exception(self, e, tb)
         return Response({"error": smart_str(e)}, status=500)
Example #20
0
 def get(self, request, uuid, path):
     # user_id = self.request.user.id
     service = BundleService(self.request.user)
     try:
         content_type, _encoding = mimetypes.guess_type(path)
         if not content_type:
             content_type = 'text/plain'
         return StreamingHttpResponse(service.read_file(uuid, path),
                                      content_type=content_type)
     except Exception as e:
         tb = traceback.format_exc()
         log_exception(self, e, tb)
         return Response({"error": smart_str(e)}, status=500)
Example #21
0
 def post(self, request):
     user_id = self.request.user.id
     service = BundleService(self.request.user)
     try:
         source_file = request.FILES['file']
         bundle_type = request.POST['bundle_type']
         worksheet_uuid = request.POST['worksheet_uuid']
         new_bundle_uuid = service.upload_bundle(source_file, bundle_type, worksheet_uuid)
         return Response({'uuid': new_bundle_uuid}, content_type="application/json")
     except Exception as e:
         tb = traceback.format_exc()
         log_exception(self, e, tb)
         return Response({"error": smart_str(e)}, status=500)
Example #22
0
 def get(self, request):
     user_id = self.request.user.id
     bundle_spec = request.GET.get('spec', '')
     worksheet_uuid = request.GET.get('worksheet_uuid', None)
     logger.debug("BundleGetUUIDApi: user_id=%s; spec=%s. worksheet_uuid=%s", user_id, bundle_spec, worksheet_uuid)
     service = BundleService(self.request.user)
     try:
         bundle_uuid = service.get_bundle_uuid(bundle_spec, worksheet_uuid)
         return Response({'uuid': bundle_uuid}, content_type="application/json")
     except Exception as e:
         tb = traceback.format_exc()
         log_exception(self, e, tb)
         return Response({"error": smart_str(e)}, status=500)
Example #23
0
 def get(self, request, uuid, path=''):
     user_id = self.request.user.id
     logger.debug("BundleContent: user_id=%s; uuid=%s; path=%s.", user_id,
                  uuid, path)
     service = BundleService(self.request.user)
     try:
         target = (uuid, path)
         info = service.get_target_info(target)
         return Response(info)
     except Exception as e:
         tb = traceback.format_exc()
         log_exception(self, e, tb)
         return Response({'error': smart_str(e)})
Example #24
0
 def get(self, request):
     user_id = self.request.user.id
     search_string = request.GET.get('search_string', '')
     logger.debug("WorksheetSearch: user_id=%s; search_string=%s.", user_id,
                  search_string)
     service = BundleService(self.request.user)
     try:
         worksheet_infos = service.search_worksheets(
             search_string.split(' '))
         return Response(worksheet_infos, content_type="application/json")
     except Exception as e:
         tb = traceback.format_exc()
         log_exception(self, e, tb)
         return Response({"error": smart_str(e)}, status=500)
Example #25
0
    def post(self, request, uuid):
        user_id = self.request.user.id
        logger.debug("BundleInfo: user_id=%s; uuid=%s.", user_id, uuid)
        service = BundleService(self.request.user)

        try:
            bundle_info = service.get_bundle_info(uuid)
            if bundle_info['owner_id'] == str(self.request.user.id):
                data = json.loads(request.body)
                new_metadata = data['metadata']
                #clean up stuff
                if new_metadata.get('data_size', None):
                    new_metadata.pop('data_size')
                if new_metadata.get('created', None):
                    new_metadata.pop('created')
                if new_metadata.get('time', None):
                    new_metadata.pop('time')

                if new_metadata.get('tags', None):
                    tags = new_metadata['tags']
                    tags = tags.split(',')
                    new_metadata['tags'] = tags
                else:
                    new_metadata['tags'] = []

                if new_metadata.get('language', None) or new_metadata.get('language') ==  u'':
                    language = new_metadata['language']
                    language = language.split(',')
                    new_metadata['language'] = language

                if new_metadata.get('architectures', None):
                    architectures = new_metadata['architectures']
                    architectures = architectures.split(',')
                    new_metadata['architectures'] = architectures

                # update and return
                # json load only gives string, convert them into needed type
                new_metadata['request_cpus'] = int(new_metadata['request_cpus']);
                new_metadata['request_gpus'] = int(new_metadata['request_gpus']);
                new_metadata['request_priority'] = int(new_metadata['request_priority']);
                new_metadata['actions'] = new_metadata['actions'].split();
                new_metadata['exitcode'] = int(new_metadata['exitcode']);
                print new_metadata
                service.update_bundle_metadata(uuid, new_metadata)
                bundle_info = service.get_bundle_info(uuid)
            return Response(bundle_info, content_type="application/json")
        except Exception as e:
            tb = traceback.format_exc()
            log_exception(self, e, tb)
            return Response({'error': smart_str(e)})
Example #26
0
 def post(self, request):
     user_id = self.request.user.id
     postdata = json.loads(request.body)
     # logger.debug("BundleSearch: user_id=%s; search_string=%s.", user_id, search_string)
     service = BundleService(self.request.user)
     try:
         #TODO CHECKING
         info = {}
         new_bundle_uuid = service.upload_bundle_url(
             postdata['url'], info, postdata['worksheet_uuid'])
         return Response({'uuid': new_bundle_uuid},
                         content_type="application/json")
     except Exception as e:
         tb = traceback.format_exc()
         log_exception(self, e, tb)
         return Response({"error": smart_str(e)}, status=500)
Example #27
0
 def post(self, request):
     user = self.request.user
     data = json.loads(request.body)
     if not data.get('worksheet_uuid', None) or not data.get('command', None):
         return Response("Must have worksheet uuid and command", status=status.HTTP_400_BAD_REQUEST)
     service = BundleService(self.request.user)
     try:
         data = service.general_command(data['worksheet_uuid'], data['command'])
         return Response({
             'success': True,
             'data': data
             })
     except Exception as e:
         tb = traceback.format_exc()
         log_exception(self, e, tb)
         return Response({"error": smart_str(e)}, status=500)
Example #28
0
 def post(self, request):
     owner = self.request.user
     if not owner.id:
         return Response(None, status=401)
     data = json.loads(request.body)
     worksheet_name = data['name'] if 'name' in data else ''
     logger.debug("WorksheetCreation: owner=%s; name=%s", owner.id, worksheet_name)
     if len(worksheet_name) <= 0:
         return Response("Invalid name.", status=status.HTTP_400_BAD_REQUEST)
     service = BundleService(self.request.user)
     try:
         data["uuid"] = service.create_worksheet(worksheet_name)
         logger.debug("WorksheetCreation def: owner=%s; name=%s; uuid", owner.id, data["uuid"])
         return Response(data)
     except Exception as e:
         return Response(status=service.http_status_from_exception(e))
Example #29
0
 def post(self, request):
     user_id = self.request.user.id
     postdata = json.loads(request.body)
     # logger.debug("BundleSearch: user_id=%s; search_string=%s.", user_id, search_string)
     service = BundleService(self.request.user)
     try:
         #TODO CHECKING
         command = postdata['data'][-1].strip("'")
         items = postdata['data'][:-1]
         items.append(command)
         new_bundle_uuid = service.create_run_bundle(items, postdata['worksheet_uuid'])
         return Response({'uuid': new_bundle_uuid}, content_type="application/json")
     except Exception as e:
         tb = traceback.format_exc()
         log_exception(self, e, tb)
         return Response({"error": smart_str(e)}, status=500)
Example #30
0
    def post(self, request, uuid):
        '''
        Save metadata information for a bundle.
        '''
        user_id = self.request.user.id
        logger.debug("BundleInfo: user_id=%s; uuid=%s.", user_id, uuid)
        service = BundleService(self.request.user)
        try:
            bundle_info = service.get_bundle_info(uuid)
            # Save only if we're the owner.
            if bundle_info['edit_permission']:
                data = json.loads(request.body)
                new_metadata = data['metadata']

                # TODO: do this generally based on the CLI specs.
                # Remove generated fields.
                for key in [
                        'data_size', 'created', 'time', 'memory', 'exitcode',
                        'actions'
                ]:
                    if key in new_metadata:
                        del new_metadata[key]

                # Convert to arrays
                for key in ['tags', 'language', 'architectures']:
                    if key in new_metadata and isinstance(
                            new_metadata[key], basestring):
                        new_metadata[key] = new_metadata[key].split(',')

                # Convert to ints
                for key in [
                        'request_cpus', 'request_gpus', 'request_priority'
                ]:
                    if key in new_metadata:
                        new_metadata[key] = int(new_metadata[key])

                service.update_bundle_metadata(uuid, new_metadata)
                bundle_info = service.get_bundle_info(uuid)
                return Response(bundle_info, content_type="application/json")
            else:
                return Response(
                    {'error': 'Can\'t save unless you\'re the owner'})
        except Exception as e:
            tb = traceback.format_exc()
            log_exception(self, e, tb)
            return Response({'error': smart_str(e)})