Skip to content

Commit

Permalink
Zenodo tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Szilveszter Juhos committed Sep 21, 2018
1 parent b48b2fe commit 39a75bb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 32 deletions.
6 changes: 3 additions & 3 deletions scripts/zenodo_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"upload_type": "dataset",
"publication_type": "other",
"publication_date": "2018-09-14",
"title": "Eljen II Rakoczi Feco",
"creators": [ { "name": "Szilveszter, Juhos", "affiliation": "SciLifeLab" }],
"description": "This is my first upload",
"title": "References and test data for Sarek",
"creators": [ { "name": "Maxime Garcia", "affiliation": "SciLifeLab" },{ "name": "Szilveszter, Juhos", "affiliation": "SciLifeLab"}],
"description": "Reference files necessary to run somatic/germline variation discovery for https://github.com/SciLifeLab/Sarek",
"access_right": "open",
"license": { "id":"cc-by" }
}
Expand Down
77 changes: 48 additions & 29 deletions scripts/zenodo_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,75 @@
import json
import codecs

# global static header
headers = {"Content-Type": "application/json"}

@click.command(context_settings = dict( help_option_names = ['-h', '--help'] ))
@click.option('--filename', '-f', type=str, help='Files to upload to Zenodo.org', required=True)
@click.option('--filename', '-f', type=str, help='Files to upload to Zenodo.org', required=False)
@click.option('--token', '-t', type=str, help='Access token', required=True)
@click.option('--metadata', '-m', type=str, help='Metadata file', required=True)
@click.option('--metadata', '-m', type=str, help='Metadata file', required=False)
@click.option('--deposition', '-d', type=str, help='Deposition ID', required=False)
@click.option('--sandbox', '-s', is_flag=True, help='Use sandbox', required=False, default=True)

# This is the surrogate for main(): everything happens here
def uploadToZenodo(filename,token,metadata,sandbox,deposition):
print "Processing file ",filename
zenodoHost='zenodo.org'
if sandbox:
zenodoHost='sandbox.' + zenodoHost
print "Using host: " + zenodoHost

# reading metadata:
with codecs.open(metadata, 'r', 'utf-8') as f:
# The URL for the actual upload and a global
zenodoURL='https://' + zenodoHost + '/api/deposit/depositions'

# if there is no deposition ID, create one
newDeposition = False
if not deposition:
deposition = new_deposition(zenodoURL, token)
newDeposition = True
# add file - in some cases we are only updating metadata, but
# to create a new deposition you must have a file
if not newDeposition and filename is not None:
upload_file(filename, deposition, zenodoURL, token)

if metadata:
print "Uploading metadata %s" % metadata
r = upload_metadata(zenodoURL, metadata, deposition, token)

def new_deposition(zURL,token):
r = requests.post( zURL,
params={'access_token': token},
json={},
headers=headers)
depo_id = r.json()['id']
print "New deposition created with ID %s at %s" % (depo_id, zURL +"/"+str(depo_id))
print "Refer to this at further file uploads"
return depo_id

def upload_file(filename, deposition_id, zURL, token):
data = {'filename': os.path.basename(filename)}
files = {'file': open(filename, 'rb')}
r = requests.post(zURL + '/%s/files' % deposition_id,
params={'access_token': token},
data=data,
files=files)
# TODO: actualy it is not true: if the filename is already there, it is not uploaded, the JSON
# returns with a warning and does nothing
print "New file %s uploaded" % filename

def upload_metadata(zURL, mdFile, deposition_id, token):
# reading metadata:
with codecs.open(mdFile, 'r', 'utf-8') as f:
metadataStr = f.read()
if not _is_valid_json(metadataStr):
return
else:
metadataJSON = json.loads(metadataStr)
print metadataJSON

zenodoURL='https://' + zenodoHost + '/api/deposit/depositions'
headers = {"Content-Type": "application/json"}
# r = requests.post( zenodoURL,
# params={'access_token': token},
# json={},
# headers=headers)

# deposition_id = r.json()['id']
# data = {'filename': os.path.basename(filename)}
# files = {'file': open(filename, 'rb')}
# r = requests.post(zenodoURL + '/%s/files' % deposition_id,
# params={'access_token': token},
# data=data,
# files=files)

# print r.json()

deposition_id = deposition

r = requests.put(zenodoURL + '/%s' % deposition_id,
return requests.put(zURL + '/%s' % deposition_id,
params={'access_token': token},
json=metadataJSON,
headers=headers)

print r.json()


def _is_valid_json(text):
try:
json.loads(text)
Expand Down

0 comments on commit 39a75bb

Please sign in to comment.