This repository was archived by the owner on Feb 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublish.py
More file actions
executable file
·197 lines (160 loc) · 7.24 KB
/
publish.py
File metadata and controls
executable file
·197 lines (160 loc) · 7.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#! /usr/bin/env python3
# -----------------------------------------------------------------------------
# Copyright 2018 ReScience C - BSD two-clauses licence
#
# This script takes care of uploading a new ReScience article to Zenodo.
# It requires the acticle (PDF) article and the metadata (YAML).
# Zenodo REST API at http://developers.zenodo.org
# -----------------------------------------------------------------------------
import json
import os
import os.path
import requests
from article import Article
def upload_content(server, token, article_id, filename):
""" Upload content to server """
data = {'filename': filename}
files = {'file': open(filename, 'rb')}
url = 'https://%s/api/deposit/depositions/%s/files' % (server, article_id)
response = requests.post(url, params={'access_token': token},
data=data, files=files)
if response.status_code != 201:
raise IOError("%s: " % response.status_code + response.json()["message"])
def update_metadata(server, token, article_id, article):
""" Upload content metadata to server """
# ! Any empty entry will ake the upload to fail with a cryptic error message
headers = {"Content-Type": "application/json"}
url = 'https://%s/api/deposit/depositions/%s' % (server, article_id)
data = {
'metadata': {
'title': article.title,
'upload_type': 'publication',
'publication_type': 'article',
'description' : article.type,
'creators': [ {'name': author.name,
'orcid': author.orcid} for author in article.authors],
'access_right' : 'open',
'license' : 'cc-by',
'keywords' : article.keywords.split(),
'contributors' : [
{'name': article.editors[0].name, 'type': 'Editor' },
{'name': article.reviewers[0].name, 'type': 'Other' },
{'name': article.reviewers[1].name, 'type': 'Other' }
],
'related_identifiers' : [
# {'relation': 'isSupplementedBy', 'identifier': article.code.doi},
# {'relation': 'cites', 'identifier': article.replication.doi}
],
'journal_title' : "ReScience C",
'journal_volume' : "%s" % article.journal_volume,
'journal_issue' : "%s" % article.journal_issue,
'journal_pages' : "%s" % article.article_number,
'communities' : [{'identifier': 'rescience'}],
}
}
if article.type in ["replication", "Replication"]:
if article.code.doi is not None:
data['related_identifiers'].append(
{'relation': 'isSupplementedBy', 'identifier': article.code.doi})
if article.replication.doi is not None:
data['related_identifiers'].append(
{'relation': 'cites', 'identifier': article.replication.doi})
response = requests.put(url, params={'access_token': token},
data=json.dumps(data), headers=headers)
if response.status_code != 200:
raise IOError("%s: " % response.status_code +
response.json()["message"])
def publish(server, token, article_id):
""" Publish entry """
url = 'https://%s/api/deposit/depositions/%s/actions/publish' % (server, article_id)
response = requests.post(url, params={'access_token': token})
if response.status_code != 202:
raise IOError("%s: " % response.status_code +
response.json()["message"])
# -----------------------------------------------------------------------------
if __name__ == '__main__':
import sys
import argparse
# Argument parsing
parser = argparse.ArgumentParser(description='DOI pre-reservation on Zenodo')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--sandbox', action='store_true',
help='Use the sandbox server')
group.add_argument('--zenodo', action='store_true',
help='Use the production server')
parser.add_argument('--metadata', action='store', required=True,
help="Article metadata (YAML format)")
parser.add_argument('--pdf', action='store', required=True,
help="Article (PDF format)")
args = parser.parse_args()
# Check if we have connection information
if args.sandbox:
server = "sandbox.zenodo.org"
token_name = "ZENODO_SANDBOX_TOKEN"
else:
server = "zenodo.org"
token_name = "ZENODO_TOKEN"
token = os.getenv(token_name)
if token is None:
url = "".format(server)
print("No token found ({0})".format(token_name))
print("You can request one from https://{0}/account/settings/applications/tokens/new/".format(server))
sys.exit(0)
# Check for metadata and article files
metadata_file = args.metadata
if not os.path.exists(metadata_file):
print("Metadata file not found ({0}).".format(metadata_file))
sys.exit(0)
article_file = args.pdf
if not os.path.exists(article_file):
print("Article file not found ({0}).".format(article_file))
sys.exit(0)
# Check if metadata file is newer than pdf
if os.path.getmtime(metadata_file) > os.path.getmtime(article_file):
print("Metadata is newer than PDF, probably PDF needs to be rebuild")
sys.exit(0)
# Read article metadata
with open(metadata_file) as file:
article = Article(file.read())
# Extract doi from metadata
article_doi = article.article_doi
article_id = article_doi.split('.')[-1]
# Upload content
print("Uploading content... ", end="")
upload_content(server, token, article_id, article_file)
print("done!")
# Update metadata
print("Updating metadata... ", end="")
update_metadata(server, token, article_id, article)
print("done!")
# Publish entry
print("Publishing... ", end="")
publish(server, token, article_id)
print("done!")
print("Entry is online at ", end="")
print("https://%s/record/%s" % (server, article_id))
print()
# Create a new local directory containing article and metadata
# This is done in a new branch
branch = article_doi.replace('/','_')
directory = branch
src_pdf = article_file
dst_pdf = os.path.join(directory, "article.pdf")
src_yaml = metadata_file
dst_yaml = os.path.join(directory, "article.yaml")
src_bib = metadata_file
dst_bib = os.path.join(directory, "article.bib")
os.system("git stash")
os.system("git checkout -b {0}".format(branch))
os.system("mkdir {0}".format(directory))
os.system("cp {0} {1}".format(src_pdf, dst_pdf))
os.system("cp {0} {1}".format(src_yaml, dst_yaml))
os.system("./yaml-to-bibtex.py -i {0} -o {1}".format(src_bib, dst_bib))
os.system("git add {0}".format(dst_pdf))
os.system("git add {0}".format(dst_yaml))
os.system("git add {0}".format(dst_bib))
os.system("git commit -m 'Added entry {0}'".format(article_doi))
os.system("git checkout master")
print()
print("Local entry has been created in {0}.".format(directory))
print("A new git branch ({0}) has been created.".format(branch))