-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
services: create zenodo deposit through CAP
* creates a Zenodo deposit, with files from CAP * saves metadata about the Zenodo deposit, and attaches it to a CAP deposit * integration tests * addresses #1938 Signed-off-by: Ilias Koutsakis <[email protected]>
- Loading branch information
Showing
7 changed files
with
503 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of CERN Analysis Preservation Framework. | ||
# Copyright (C) 2018 CERN. | ||
# | ||
# CERN Analysis Preservation Framework is free software; you can redistribute | ||
# it and/or modify it under the terms of the GNU General Public License as | ||
# published by the Free Software Foundation; either version 2 of the | ||
# License, or (at your option) any later version. | ||
# | ||
# CERN Analysis Preservation Framework is distributed in the hope that it will | ||
# be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with CERN Analysis Preservation Framework; if not, write to the | ||
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, | ||
# MA 02111-1307, USA. | ||
# | ||
# In applying this license, CERN does not | ||
# waive the privileges and immunities granted to it by virtue of its status | ||
# as an Intergovernmental Organization or submit itself to any jurisdiction. | ||
"""Tasks.""" | ||
|
||
from __future__ import absolute_import, print_function | ||
|
||
import requests | ||
from flask import current_app | ||
from celery import shared_task | ||
from invenio_db import db | ||
from invenio_files_rest.models import FileInstance, ObjectVersion | ||
|
||
|
||
@shared_task(autoretry_for=(Exception, ), | ||
retry_kwargs={ | ||
'max_retries': 5, | ||
'countdown': 10 | ||
}) | ||
def upload_to_zenodo(record_uuid, files, bucket, token, | ||
zenodo_depid, zenodo_bucket_url): | ||
"""Upload code to zenodo.""" | ||
from cap.modules.deposit.api import CAPDeposit | ||
record = CAPDeposit.get_record(record_uuid) | ||
|
||
file_list = [] | ||
for filename in files: | ||
file_obj = ObjectVersion.get(bucket, filename) | ||
file_ins = FileInstance.get(file_obj.file_id) | ||
|
||
# upload each file in the deposit | ||
with open(file_ins.uri, 'rb') as fp: | ||
file = requests.put( | ||
url=f'{zenodo_bucket_url}/{filename}', | ||
data=fp, | ||
params=dict(access_token=token), | ||
) | ||
|
||
if file.ok: | ||
data = file.json() | ||
file_list.append({ | ||
'self': data['links']['self'], | ||
'key': data['key'], | ||
'size': data['size'] | ||
}) | ||
else: | ||
current_app.logger.error( | ||
f'Uploading file {filename} to deposit {zenodo_depid} ' | ||
f'failed with {file.status_code}.') | ||
|
||
# optionally add metadata | ||
# resp = requests.put( | ||
# url=f'{zenodo_server_url}/deposit/depositions/{depid}', | ||
# params=dict(access_token=token), | ||
# data=json.dumps({}), | ||
# headers={'Content-Type': 'application/json'} | ||
# ) | ||
|
||
if file_list: | ||
# get the specific deposit we wish to update with files | ||
deposit = list( | ||
filter(lambda d: d['id'] == zenodo_depid, record['_zenodo'])) | ||
|
||
deposit[0]['files'] += file_list | ||
record.commit() | ||
db.session.commit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
import tempfile | ||
from datetime import datetime, timedelta | ||
from uuid import uuid4 | ||
from six import BytesIO | ||
|
||
import pytest | ||
from flask import current_app | ||
|
@@ -108,7 +109,8 @@ def default_config(): | |
DEBUG=False, | ||
TESTING=True, | ||
APP_GITLAB_OAUTH_ACCESS_TOKEN='testtoken', | ||
MAIL_DEFAULT_SENDER="[email protected]") | ||
MAIL_DEFAULT_SENDER="[email protected]", | ||
ZENODO_SERVER_URL='https://zenodo-test.org') | ||
|
||
|
||
@pytest.fixture(scope='session') | ||
|
@@ -401,6 +403,21 @@ def deposit(example_user, create_deposit): | |
) | ||
|
||
|
||
@pytest.fixture | ||
def deposit_with_file(example_user, create_schema, create_deposit): | ||
"""New deposit with files.""" | ||
create_schema('test-schema', experiment='CMS') | ||
return create_deposit( | ||
example_user, | ||
'test-schema', | ||
{ | ||
'$ana_type': 'test-schema', | ||
'title': 'test title' | ||
}, | ||
files={'test-file.txt': BytesIO(b'Hello world!')}, | ||
experiment='CMS') | ||
|
||
|
||
@pytest.fixture | ||
def record(example_user, create_deposit): | ||
"""Example record.""" | ||
|
Oops, something went wrong.