예제 #1
0
    def on_post(self, req, resp, dataset):
        """
        Commmit a draft change.

        This adds all files in the working tree.
        """
        if dataset:
            # Record if this was done on behalf of a user
            name, email = get_user_info(req)
            media_dict = {}
            if name and email:
                media_dict['name'] = name
                media_dict['email'] = email
            if 'validate' in req.params and req.params['validate'] == 'false':
                validate = False
            else:
                validate = True
            try:
                commit = commit_files(self.store,
                                      dataset,
                                      files=None,
                                      name=name,
                                      email=email,
                                      cookies=req.cookies)
                # Attach the commit hash to response
                media_dict['ref'] = commit
                resp.media = media_dict
                resp.status = falcon.HTTP_OK
            except:
                resp.status = falcon.HTTP_INTERNAL_SERVER_ERROR
        else:
            resp.media = {
                'error': 'Missing or malformed dataset parameter in request.'
            }
            resp.status = falcon.HTTP_UNPROCESSABLE_ENTITY
예제 #2
0
    def on_delete(self, req, resp, dataset, filename):
        """Delete an existing file from a dataset"""
        queue = dataset_queue(dataset)
        if filename:
            ds_path = self.store.get_dataset_path(dataset)
            file_path = os.path.join(ds_path, filename)
            if os.path.exists(file_path):
                ds = self.store.get_dataset(dataset)
                media_dict = {'deleted': filename}
                name, email = get_user_info(req)
                if name and email:
                    media_dict['name'] = name
                    media_dict['email'] = email

                # unlock = unlock_files.apply_async(queue=queue, args=(self.annex_path, dataset), kwargs={'files': [filename]})
                # unlock.wait()

                remove = remove_files.apply_async(queue=queue, args=(self.annex_path, dataset), kwargs={
                                                  'files': [filename], 'name': name, 'email': email})
                remove.wait()

                resp.media = media_dict
                resp.status = falcon.HTTP_OK
            else:
                resp.media = {'error': 'no such file'}
                resp.status = falcon.HTTP_NOT_FOUND
        else:
            resp.media = {'error': 'filename is missing'}
            resp.status = falcon.HTTP_BAD_REQUEST
예제 #3
0
 def on_post(self, req, resp, dataset, upload):
     """Copy uploaded data into dataset"""
     name, email = get_user_info(req)
     gevent.spawn(self._finish_upload, dataset, upload, name, email,
                  req.cookies)
     resp.media = {}
     resp.status = falcon.HTTP_OK
예제 #4
0
파일: files.py 프로젝트: adswa/openneuro
    def on_post(self, req, resp, dataset, filename):
        """Post will create new files and adds them to the annex if they do not exist, else update existing files."""
        if filename:
            ds_path = self.store.get_dataset_path(dataset)
            file_path = os.path.join(ds_path, filename)
            if os.path.exists(file_path):
                ds = self.store.get_dataset(dataset)
                media_dict = {'updated': filename}
                # Record if this was done on behalf of a user
                name, email = get_user_info(req)
                if name and email:
                    media_dict['name'] = name
                    media_dict['email'] = email
                unlock_files(self.store, dataset, files=[filename])
                self._update_file(file_path, req.stream)
                resp.media = media_dict
                resp.status = falcon.HTTP_OK
            else:
                try:
                    # Make any missing parent directories

                    os.makedirs(os.path.dirname(file_path), exist_ok=True)
                    # Begin writing stream to disk
                    self._update_file(file_path, req.stream)
                    # Add to dataset
                    ds = self.store.get_dataset(dataset)
                    media_dict = {'created': filename}
                    resp.media = media_dict
                    resp.status = falcon.HTTP_OK
                except PermissionError:
                    resp.media = {'error': 'file already exists'}
                    resp.status = falcon.HTTP_CONFLICT
        else:
            resp.media = {'error': 'filename is missing'}
            resp.status = falcon.HTTP_BAD_REQUEST
예제 #5
0
 def on_post(self, req, resp, dataset, hexsha):
     """Run validation for a given commit"""
     if dataset and hexsha:
         # Record if this was done on behalf of a user
         name, email = get_user_info(req)
         media_dict = {}
         if name and email:
             media_dict['name'] = name
             media_dict['email'] = email
         try:
             dataset_path = self.store.get_dataset_path(dataset)
             # Run the validator but don't block on the request
             validate_dataset(dataset,
                              dataset_path,
                              hexsha,
                              req.cookies,
                              user=name)
             resp.status = falcon.HTTP_OK
         except:
             resp.status = falcon.HTTP_INTERNAL_SERVER_ERROR
     else:
         resp.media = {
             'error': 'Missing or malformed dataset parameter in request.'
         }
         resp.status = falcon.HTTP_UNPROCESSABLE_ENTITY
예제 #6
0
    def on_post(self, req, resp, dataset):
        """
        Commit a draft change.

        This adds all files in the working tree.
        """
        if dataset:
            # Record if this was done on behalf of a user
            name, email = get_user_info(req)
            media_dict = {}
            if name and email:
                media_dict['name'] = name
                media_dict['email'] = email
            try:
                dataset_path = self.store.get_dataset_path(dataset)
                repo = pygit2.Repository(dataset_path)
                # Add all changes to the index
                if name and email:
                    author = pygit2.Signature(name, email)
                    media_dict['ref'] = git_commit(repo, ['.'], author).hex
                else:
                    media_dict['ref'] = git_commit(repo, ['.']).hex
                resp.media = media_dict
                resp.status = falcon.HTTP_OK
            except:
                raise
                resp.status = falcon.HTTP_INTERNAL_SERVER_ERROR
        else:
            resp.media = {
                'error': 'Missing or malformed dataset parameter in request.'}
            resp.status = falcon.HTTP_UNPROCESSABLE_ENTITY
예제 #7
0
    def on_post(self, req, resp, dataset):
        """
        Commmit a draft change.

        This adds all files in the working tree.
        """
        if dataset:
            queue = dataset_queue(dataset)
            # Record if this was done on behalf of a user
            name, email = get_user_info(req)
            media_dict = {}
            if name and email:
                media_dict['name'] = name
                media_dict['email'] = email
            commit = commit_files.apply_async(queue=queue,
                                              args=(self.annex_path, dataset),
                                              kwargs={
                                                  'files': None,
                                                  'name': name,
                                                  'email': email,
                                                  'cookies': req.cookies
                                              })
            commit.wait()
            if not commit.failed():
                # Attach the commit hash to response
                media_dict['ref'] = commit.get()
                resp.media = media_dict
                resp.status = falcon.HTTP_OK
        else:
            resp.media = {
                'error': 'Missing or malformed dataset parameter in request.'
            }
            resp.status = falcon.HTTP_UNPROCESSABLE_ENTITY
예제 #8
0
 def on_post(self, req, resp, dataset, import_id):
     name, email = get_user_info(req)
     dataset_path = self.store.get_dataset_path(dataset)
     upload_path = self.store.get_upload_path(dataset, import_id)
     url = req.media.get('url')
     gevent.spawn(remote_import, dataset_path,
                  upload_path, import_id, url, name, email, req.cookies)
     resp.status = falcon.HTTP_OK
예제 #9
0
    def on_delete(self, req, resp, dataset):
        """Delete an existing file from a dataset"""
        if req.media:
            ds_path = self.store.get_dataset_path(dataset)
            files_to_delete = []
            dirs_to_delete = []
            paths_not_found = []
            filenames = [
                filename.replace(':', '/')
                for filename in req.media['filenames']
            ]
            for filename in filenames:
                file_path = os.path.join(ds_path, filename)
                if os.path.exists(file_path):
                    if os.path.isdir(file_path):
                        dirs_to_delete.append(filename)
                    else:
                        files_to_delete.append(filename)
                else:
                    paths_not_found.append(filename)

            if len(paths_not_found) == 0:
                media_dict = {'deleted': dirs_to_delete + files_to_delete}
                name, email = get_user_info(req)
                if name and email:
                    media_dict['name'] = name
                    media_dict['email'] = email
                try:
                    if len(dirs_to_delete) > 0:
                        remove_files(self.store,
                                     dataset,
                                     dirs_to_delete,
                                     name=name,
                                     email=email,
                                     cookies=req.cookies)
                        resp.status = falcon.HTTP_INTERNAL_SERVER_ERROR
                    if len(files_to_delete) > 0:
                        remove_files(self.store,
                                     dataset,
                                     files_to_delete,
                                     name=name,
                                     email=email,
                                     cookies=req.cookies)
                    resp.media = media_dict
                    resp.status = falcon.HTTP_OK
                except:
                    resp.status = falcon.HTTP_INTERNAL_SERVER_ERROR
                    raise
            else:
                resp.media = {
                    'error':
                    f'the following files not found: {", ".join(paths_not_found)}'
                }
        else:
            resp.media = {
                'error': 'recursive query or request body is missing'
            }
            resp.status = falcon.HTTP_BAD_REQUEST
예제 #10
0
 def on_post(self, req, resp, dataset):
     ds_path = self.store.get_dataset_path(dataset)
     if (os.path.isdir(ds_path)):
         resp.media = {'error': 'dataset already exists'}
         resp.status = falcon.HTTP_CONFLICT
     else:
         # Record if this was done on behalf of a user
         name, email = get_user_info(req)
         hexsha = create_dataset(self.store, dataset, name, email)
         resp.media = {'hexsha': hexsha}
         resp.status = falcon.HTTP_OK
예제 #11
0
 def on_put(self, req, resp, dataset, filename):
     """Put will only update existing files and automatically unlocks them."""
     queue = dataset_queue(dataset)
     if filename:
         ds_path = self.store.get_dataset_path(dataset)
         file_path = os.path.join(ds_path, filename)
         if os.path.exists(file_path):
             ds = self.store.get_dataset(dataset)
             media_dict = {'updated': filename}
             # Record if this was done on behalf of a user
             name, email = get_user_info(req)
             if name and email:
                 media_dict['name'] = name
                 media_dict['email'] = email
             unlock = unlock_files.apply_async(queue=queue,
                                               args=(self.annex_path,
                                                     dataset),
                                               kwargs={'files': [filename]})
             unlock.wait()
             self._update_file(file_path, req.stream)
             commit = commit_files.apply_async(queue=queue,
                                               args=(self.annex_path,
                                                     dataset),
                                               kwargs={
                                                   'files': [filename],
                                                   'name': name,
                                                   'email': email,
                                                   'cookies': req.cookies
                                               })
             commit.wait()
             # ds.publish(to='github')
             if not commit.failed():
                 resp.media = media_dict
                 resp.status = falcon.HTTP_OK
             resp.media = media_dict
             resp.status = falcon.HTTP_OK
         else:
             resp.media = {'error': 'no such file'}
             resp.status = falcon.HTTP_NOT_FOUND
     else:
         resp.media = {'error': 'filename is missing'}
         resp.status = falcon.HTTP_BAD_REQUEST
예제 #12
0
    def on_post(self, req, resp, dataset):
        ds_path = self.store.get_dataset_path(dataset)
        if (os.path.isdir(ds_path)):
            resp.media = {'error': 'dataset already exists'}
            resp.status = falcon.HTTP_CONFLICT
        else:
            queue = dataset_queue(dataset)
            # Record if this was done on behalf of a user
            name, email = get_user_info(req)

            created = create_dataset.apply_async(queue=queue,
                                                 args=(self.store.annex_path,
                                                       dataset, name, email))
            created.wait()
            if created.failed():
                resp.media = {'error': 'dataset creation failed'}
                resp.status = falcon.HTTP_500
            else:
                resp.media = {}
                resp.status = falcon.HTTP_OK
예제 #13
0
파일: files.py 프로젝트: adswa/openneuro
 def on_delete(self, req, resp, dataset, filename):
     """Delete an existing file from a dataset"""
     if filename:
         ds_path = self.store.get_dataset_path(dataset)
         file_path = os.path.join(ds_path, filename)
         if os.path.exists(file_path):
             media_dict = {'deleted': filename}
             name, email = get_user_info(req)
             if name and email:
                 media_dict['name'] = name
                 media_dict['email'] = email
             try:
                 # The recursive flag removes the entire tree in one commit
                 if 'recursive' in req.params and req.params[
                         'recursive'] != 'false':
                     remove_recursive(self.store,
                                      dataset,
                                      filename,
                                      name=name,
                                      email=email,
                                      cookies=req.cookies)
                 else:
                     remove_files(self.store,
                                  dataset,
                                  files=[filename],
                                  name=name,
                                  email=email,
                                  cookies=req.cookies)
                 resp.media = media_dict
                 resp.status = falcon.HTTP_OK
             except:
                 resp.status = falcon.HTTP_INTERNAL_SERVER_ERROR
         else:
             resp.media = {'error': 'no such file'}
             resp.status = falcon.HTTP_NOT_FOUND
     else:
         resp.media = {'error': 'filename is missing'}
         resp.status = falcon.HTTP_BAD_REQUEST