-
Notifications
You must be signed in to change notification settings - Fork 0
/
zenodo-upload.py
562 lines (447 loc) · 15.9 KB
/
zenodo-upload.py
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Uploads to Zenodo via the REST API
"""
__version__ = "0.3.1"
import argparse
import hashlib
import json
import logging
import sys
from datetime import datetime, timezone
from pathlib import Path
import requests
from watchdog.events import LoggingEventHandler
from watchdog.observers import Observer
def read_access_token():
"""Reads the access token from `secrets.json` file."""
try:
# We store our various secrets in an aptly named file
with open("config/secrets.json", "r") as data_file:
return json.load(data_file)["access_token"]
except FileNotFoundError:
raise Exception(
"You must either provide an access token"
" on the commnad line or in the file"
" `config/secrets.json`"
)
def read_local_path():
"""
Reads the local path from `secrets.json` file.
Todo elimitate repeated code
"""
try:
# We store our various secrets in an aptly named file
with open("config/current-target.json", "r") as data_file:
return json.load(data_file)["local_path"]
except FileNotFoundError:
raise Exception(
"You must either provide a local path"
" on the commnad line or in the file"
" `config/current-target.json`"
)
parser = argparse.ArgumentParser(
description="Upload the given file(s) to Zenodo via the REST API."
" In development, so arguments and behavior my vary."
)
parser.add_argument(
"--path",
default=read_local_path(),
help="The file(s) to include in the upload. If no path is"
" specified, the path stored in the file `config/current-target.json`"
" is used ",
)
# parser.add_argument('-m', '--metadata-file',
# help="JSON file containing metadata for the upload.")
parser.add_argument(
"-t",
"--token",
default=read_access_token(),
help="Access token for the Zenodo account you wish"
" to upload to. If no token is specified, the token"
" stored in the file `config/secrets.json` is used.",
)
parser.add_argument(
"-c",
"--check",
action="store_true",
help="Check if the file(s) at <path> are"
" already uploaded to your Zenodo account,"
" but don't make any changes.",
)
parser.add_argument(
"-w",
"--watch",
action="store_true",
help="Instead of running once, run continuously"
" and watch for changes to the file(s) at the"
" specified path(s). An upload will be triggered"
" anytime a change is detected",
)
# parser.add_argument('-d', '--daemon',
# help="Daemon mode. Runs in the background,"
# " and checks once per day which files have"
# " not been uploaded to Zenodo. Those files"
# " are then added to the appropriate upload.")
parser.add_argument(
"-s",
"--sandbox",
action="store_true",
help="Use sandbox.zenodo.org instead of"
" zenodo.org. You will need to create an account on the"
" sandbox server, and an access token for that account,"
" if you do not already have these.",
)
parser.add_argument(
"-V",
"--version",
action="store_true",
dest="version",
help="Show the version number and exit.",
)
# parser.add_argument('--show-urls', action='store_true',
# help="Whenever the program output mentions a Zenodo"
# " file or upload, also output a URL to it.")
args = parser.parse_args()
if args.version:
print(__version__)
sys.exit()
if args.token == "":
None
if args.sandbox:
url = "https://sandbox.zenodo.org/api/deposit/depositions"
else:
url = "https://zenodo.org/api/deposit/depositions"
# read current-target.json
try:
with open("config/current-target.json", "r") as data_file:
target = json.load(data_file)
except FileNotFoundError:
raise Exception(
"You must provide a target in the file" " `config/current-target.json`"
)
print("Target id:", target["id"])
if args.sandbox != target["onZenodoSandboxServer"]:
raise Exception(
"The target"
+ (" is" if target["onZenodoSandboxServer"] else " is not")
+ " specified on the sandbox server, but -s/--sandbox"
+ (" was" if args.sandbox else " was not")
+ " passed.\nPlease change either the target or the"
+ " command line arguments."
)
target_url = url + "/" + str(target["id"])
if not Path(args.path).exists():
raise Exception(
"The local path does not" " exist on the system. This is likely" " an error."
)
def makeRequest(kind, url, params=None, **kwargs):
"""
Makes an http request of the specified kind, and does
a bunch of other useful things
Parameters
----------
kind : TYPE
DESCRIPTION.
url : TYPE
DESCRIPTION.
**kwargs : TYPE
DESCRIPTION.
Returns
-------
None.
"""
if params is None:
params = {}
params["access_token"] = args.token
print(kwargs)
response = getattr(requests, kind)(url, params=params, **kwargs)
print(response.url)
# raise exceptions if an unsuccessful HTTP code is returned
response.raise_for_status()
return response
def compareToTargetURL():
"""
Checks the local directory against the zenodo target_url.
Raises
------
Exception
When there is already an unpublished draft of this resource.
Returns
-------
to_be_updated : TYPE
DESCRIPTION.
to_be_uploaded : TYPE
DESCRIPTION.
"""
global target_url
# get the most recent version of the target
r = requests.get(target_url, params={"access_token": args.token})
# r = makeRequest("get", target_url)
if "latest_draft" in r.json()["links"]:
# target_url = r.json()["links"]["latest_draft"]
# there is already a new draft for this deposition
# the API will not allow more than one draft at a time,
# and this situation is likely unsafe anyway
raise Exception(
"The deposition already has an unpublished draft."
" Please deal with this on the Zenodo website:\n"
+ r.json()["links"]["latest_draft_html"]
)
else:
latest_target_id = r.json()["links"]["latest"].rsplit("/", 1)[-1]
target_url = url + "/" + str(latest_target_id)
# get files for deposition
request = requests.get(target_url + "/files", params={"access_token": args.token})
# request = makeRequest("get", target_url + "/files")
BUF_SIZE = 65536 # lets read stuff in 64kb chunks!
# get the files in the current directory that do not
# start with '.'
local_files = [
path
for path in Path(args.path).glob("*")
if path.is_file() and path.name[0] != "."
]
print(local_files)
# find the checksums of all the files
checksums = {}
for file in local_files:
if not file.exists():
raise Exception(file.name, "does not exist")
md5 = hashlib.md5()
with open(file, "rb") as f:
while True:
data = f.read(BUF_SIZE)
if not data:
break
md5.update(data)
checksums[file] = md5.hexdigest()
# print(file.name + " " + md5.hexdigest())
# see which files are in the upload
# find the files that are not uploaded
to_be_updated = []
to_be_uploaded = []
for local_file in local_files:
matchedExact = False
matched = False
for zenodo_file in request.json():
if local_file.name.replace(" ", "_") == zenodo_file["filename"]:
matched = True
if checksums[local_file] == zenodo_file["checksum"]:
# print("Match! local_file: {0}, zenodo_file: {1}".format(local_file.name, zenodo_file["filename"]))
matchedExact = True
else:
None
# print("No match. local_file: {0}, zenodo_file: {1}".format(local_file.name, zenodo_file["filename"]))
if matchedExact:
print(local_file.name, "is uploaded in current form.")
elif matched:
print(local_file.name, "needs updating.")
to_be_updated.append(local_file)
to_be_uploaded.append(local_file)
else:
print(local_file.name, "has not been uploaded")
to_be_uploaded.append(local_file)
return to_be_updated, to_be_uploaded
def do():
"""Just do it!"""
to_be_updated, to_be_uploaded = compareToTargetURL() # could be renamed
if len(to_be_uploaded) == 0:
print("Nothing to upload.")
return
# create a new version of the deposition so that we can add files
# the body of r is not the new version, but the original resource
r = requests.post(
target_url + "/actions/newversion", params={"access_token": args.token}
)
# r = makeRequest("post", target_url + "/actions/newversion")
# pull out link to new version
new_target_url = r.json()["links"]["latest_draft"]
# get files in the new version
request = requests.get(
new_target_url + "/files", params={"access_token": args.token}
)
# delete deposition files for the files that will be updated
for file in to_be_updated:
for zenodo_file in request.json():
if file.name.replace(" ", "_") == zenodo_file["filename"]:
r = requests.delete(
zenodo_file["links"]["self"], params={"access_token": args.token}
)
print("Deleted deposition file with status code", r.status_code)
break
# upload the not-yet-uploaded files
for file in to_be_uploaded:
print("Uploading", file.name)
# create deposition file (upload a file)
r_df = requests.post(
new_target_url + "/files",
data={"name": file.name},
files={"file": file.read_bytes()},
params={"access_token": args.token},
)
# r_df = makeRequest("post",
# new_target_url + "/files",
# data={'name': file.name},
# files={'file': file.read_bytes()})
print("Sent file with status code", r_df.status_code)
if r_df.status_code == 400:
print(r_df.json())
# update date on the deposition
# a. get the existing deposition metadata
r = requests.get(new_target_url, params={"access_token": args.token})
metadata = r.json()["metadata"]
# b. update time
now = datetime.now(timezone.utc)
metadata["publication_date"] = now.isoformat()
data = {"metadata": metadata}
# update the date on the deposition
r = requests.put(
new_target_url,
data=json.dumps(data),
params={"access_token": args.token},
headers={"Content-Type": "application/json"},
)
print("Changed time with status code", r.status_code)
if r.status_code != 200:
print(r.json())
# publish the new deposiiton version
r = requests.post(
new_target_url + "/actions/publish", params={"access_token": args.token}
)
# r = makeRequest("post", new_target_url + "/actions/publish")
print("Published deposition with status code", r.status_code)
if r.status_code == 400:
print(r.json())
# description_file = "C:\\Users\\aidan\\Documents\\W8EDU\\psws-zenodo\\test-files\\Readme_5_Mhz Cleveland Heights.txt"
# description_text = ""
# with open(description_file, 'r') as f:
# description_text = f.read()
# # create deposition
# data = {
# "metadata": {
# "title": "target for WWV 5MHz Frequency Measurements from Cleveland, OH, USA",
# "upload_type": "dataset",
# "description": description_text,
# "creators": [
# {"name": "Kazdan, David", "affiliation": "Case Western Reserve University"}
# ],
# "keywords": ["wwv"],
# "communities": [{"identifier": "hamsci-test"}],
# "locations": [
# {
# "lat": 41.493744,
# "lon": -81.578039,
# "place": "location where recordings are made"
# }
# ]
# }
# }
# headers = {"Content-Type": "application/json"}
# r = requests.post(url, data=json.dumps(data), headers=headers,
# params={'access_token': args.token})
# print("Created upload with status code", r.status_code)
# # create deposition file (upload a file)
# file_name = os.path.basename(args.path)
# data = {'name': file_name}
# files = {'file': open(args.path, 'rb')}
# r_df = requests.post(r.json()['links']['files'], data=data, files=files,
# params={'access_token': args.token})
# print("Sent file with status code", r_df.status_code)
# # publish deposition
# r_p = requests.post(r.json()['links']['publish'],
# params={'access_token': args.token})
# print("Published upload with status code", r_p.status_code)
# print()
# print("Response:")
# print(json.dumps(r_p.json(), indent=2))
# webbrowser.open(r_p.json()['links']['record_html'])
def getAllDepositionsOfUser():
"""Get all the depositions of the user thus far, and return
a simplified list of deposition objects"""
page = 1 # page of the depositions list we are looking at
cache = []
while True:
request = requests.get(url, params={"access_token": args.token, "page": page})
if request.status_code != 200:
print("API fail with status code", request.status_code)
break
if not request.json():
break
for deposition in request.json():
cache.append({"id": deposition["id"]})
if deposition["submitted"]:
cache[len(cache) - 1]["submitted"] = True
cache[len(cache) - 1]["link"] = deposition["links"]["record_html"]
else:
cache[len(cache) - 1]["submitted"] = False
cache[len(cache) - 1]["link"] = deposition["links"]["html"]
if "files" in deposition:
cache[len(cache) - 1]["files"] = []
for file in deposition["files"]:
cache[len(cache) - 1]["files"].append(
{"filename": file["filename"], "checksum": file["checksum"]}
)
page += 1
return cache
def doWrap():
global observer
observer.stop()
do()
observer.start()
class UploadingEventHandler(LoggingEventHandler):
"""
Logs all the events captured, and triggers the upload process
to start
"""
def on_created(self, event):
super(UploadingEventHandler, self).on_created(event)
do()
def on_modified(self, event):
super(UploadingEventHandler, self).on_modified(event)
do()
def watch():
"""Implementation of -w/--watch option."""
print("Starting watchdog")
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
event_handler = UploadingEventHandler()
observer = Observer()
observer.schedule(event_handler, args.path, recursive=True)
observer.start()
print("Awaiting new changes")
try:
while observer.isAlive():
print("loop started")
observer.join()
except KeyboardInterrupt:
print("Stopping")
observer.stop()
observer.join()
if args.watch:
watch()
elif args.check:
print("Comparing local to remote...")
print("Local:", args.path)
print("Remote:", target_url)
to_be_updated, to_be_uploaded = compareToTargetURL()
print()
if to_be_updated:
print("Old version will be removed:")
for file in to_be_updated:
print(file.name)
else:
print("No existing upload files to modify.")
print()
if to_be_uploaded:
print("Will be uploaded:")
for file in to_be_uploaded:
print(file.name)
else:
print("No new files to upload.")
else:
do()