-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzget.py
430 lines (377 loc) · 12.2 KB
/
zget.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
#!/usr/bin/env python3
from contextlib import contextmanager
from fnmatch import fnmatch
import hashlib
from importlib.metadata import version
from optparse import OptionParser
import os
from pathlib import Path
import signal
import sys
import time
from urllib.parse import unquote
import humanize
import requests
import wget
import zenodo_get as zget
# see https://stackoverflow.com/questions/431684/how-do-i-change-the-working-directory-in-python/24176022#24176022
@contextmanager
def cd(newdir):
prevdir = os.getcwd()
os.chdir(os.path.expanduser(newdir))
try:
yield
finally:
os.chdir(prevdir)
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def ctrl_c(func):
signal.signal(signal.SIGINT, func)
return func
abort_signal = False
abort_counter = 0
exceptions = False
@ctrl_c
def handle_ctrl_c(*args, **kwargs):
global abort_signal
global abort_counter
global exceptions
abort_signal = True
abort_counter += 1
if abort_counter >= 2:
eprint()
eprint("Immediate abort. There might be unfinished files.")
if exceptions:
raise Exception("Immediate abort")
else:
sys.exit(1)
def check_hash(filename, checksum):
algorithm = "md5"
value = checksum.strip()
if not os.path.exists(filename):
return value, "invalid"
h = hashlib.new(algorithm)
with open(filename, "rb") as f:
while True:
data = f.read(4096)
if not data:
break
h.update(data)
digest = h.hexdigest()
return value, digest
def zenodo_get(argv=None):
global exceptions
if argv is None:
argv = sys.argv[1:]
exceptions = False
else:
exceptions = True
parser = OptionParser(
usage="%prog [options] RECORD_OR_DOI", version=f"%prog {version('zenodo_get')}"
)
parser.add_option(
"-c",
"--cite",
dest="cite",
action="store_true",
default=False,
help="print citation information",
)
parser.add_option(
"-r",
"--record",
action="store",
type="string",
dest="record",
help="Zenodo record ID",
default=None,
)
parser.add_option(
"-d",
"--doi",
action="store",
type="string",
dest="doi",
help="Zenodo DOI",
default=None,
)
parser.add_option(
"-m",
"--md5",
action="store_true",
# ~type=bool,
dest="md5",
help="Create md5sums.txt for verification.",
default=False,
)
parser.add_option(
"-w",
"--wget",
action="store",
type="string",
dest="wget",
help="Create URL list for download managers. "
"(Files will not be downloaded.)",
default=None,
)
parser.add_option(
"-e",
"--continue-on-error",
action="store_true",
dest="error",
help="Continue with next file if error happens.",
default=False,
)
parser.add_option(
"-k",
"--keep",
action="store_true",
dest="keep",
help="Keep files with invalid checksum." " (Default: delete them.)",
default=False,
)
parser.add_option(
"-n",
"--do-not-continue",
action="store_false",
dest="cont",
help="Do not continue previous download attempt. (Default: continue.)",
default=True,
)
parser.add_option(
"-R",
"--retry",
action="store",
type=int,
dest="retry",
help="Retry on error N more times.",
default=0,
)
parser.add_option(
"-p",
"--pause",
action="store",
type=float,
dest="pause",
help="Wait N second before retry attempt, e.g. 0.5",
default=0.5,
)
parser.add_option(
"-t",
"--time-out",
action="store",
type=float,
dest="timeout",
help="Set connection time-out. Default: 15 [sec].",
default=15.0,
)
parser.add_option(
"-o",
"--output-dir",
action="store",
type=str,
dest="outdir",
default=".",
help="Output directory, created if necessary. Default: current directory.",
)
parser.add_option(
"-s",
"--sandbox",
action="store_true",
dest="sandbox",
help="Use Zenodo Sandbox URL.",
default=False,
)
parser.add_option(
"-a",
"--access-token",
action="store",
type=str,
dest="access_token",
default=None,
help="Optional access token for the requests query.",
)
parser.add_option(
"-g",
"--glob",
action="store",
type=str,
dest="glob",
default="*",
help="Optional glob expression for files.",
)
(options, args) = parser.parse_args(argv)
if options.cite:
print("Reference for this software:")
print(zget.__reference__)
print()
print("Bibtex format:")
print(zget.__bibtex__)
if exceptions:
return
else:
sys.exit(0)
# create directory, if necessary, then change to it
options.outdir = Path(options.outdir)
options.outdir.mkdir(parents=True, exist_ok=True)
with cd(options.outdir):
if len(args) > 0:
try:
options.record = str(int(args[0]))
except ValueError:
options.doi = args[0]
elif options.doi is None and options.record is None:
parser.print_help()
if exceptions:
return
else:
sys.exit(0)
if options.doi is not None:
url = options.doi
if not url.startswith("http"):
url = "https://doi.org/" + url
try:
r = requests.get(url, timeout=options.timeout)
except requests.exceptions.ConnectTimeout:
eprint("Connection timeout.")
if exceptions:
raise
else:
sys.exit(1)
except Exception:
eprint("Connection error.")
if exceptions:
raise
else:
sys.exit(1)
if not r.ok:
eprint("DOI could not be resolved. Try again, or use record ID.")
if exceptions:
raise ValueError("DOI", options.doi)
else:
sys.exit(1)
recordID = r.url.split("/")[-1]
else:
recordID = options.record
recordID = recordID.strip()
if not options.sandbox:
url = "https://zenodo.org/api/records/"
else:
url = "https://sandbox.zenodo.org/api/records/"
params = {}
if options.access_token:
params["access_token"] = options.access_token
try:
r = requests.get(url + recordID, params=params, timeout=options.timeout)
except requests.exceptions.ConnectTimeout:
eprint("Connection timeout during metadata reading.")
if exceptions:
raise
else:
sys.exit(1)
except Exception:
eprint("Connection error during metadata reading.")
if exceptions:
raise
else:
sys.exit(1)
if r.ok:
js = r.json()
files = [
f
for f in js["files"]
if fnmatch(f.get("filename") or f["key"], options.glob)
]
if not files:
eprint("Files {} not found in metadata".format(options.glob))
total_size = sum((f.get("filesize") or f["size"]) for f in files)
if options.md5 is not None:
with open("md5sums.txt", "wt") as md5file:
for f in files:
fname = f.get("filename") or f["key"]
checksum = f["checksum"].split(":")[-1]
md5file.write(f"{checksum} {fname}\n")
if options.wget is not None:
if options.wget == "-":
for f in files:
fname = f.get("filename") or f["key"]
link = "https://zenodo.org/records/{}/files/{}".format(
recordID, fname
)
print(link)
else:
with open(options.wget, "wt") as wgetfile:
for f in files:
fname = f.get("filename") or f["key"]
link = "https://zenodo.org/records/{}/files/{}".format(
recordID, fname
)
wgetfile.write(link + "\n")
else:
eprint("Title: {}".format(js["metadata"]["title"]))
eprint("Keywords: " + (", ".join(js["metadata"].get("keywords", []))))
eprint("Publication date: " + js["metadata"]["publication_date"])
eprint("DOI: " + js["metadata"]["doi"])
eprint(f"Total size: {humanize.naturalsize(total_size)}")
for f in files:
if abort_signal:
eprint("Download aborted with CTRL+C.")
eprint("Already successfully downloaded files are kept.")
break
fname = f.get("filename") or f["key"]
link = "https://zenodo.org/records/{}/files/{}".format(
recordID, fname
)
size = humanize.naturalsize(f.get("filesize") or f["size"])
eprint()
eprint(f"Link: {link} size: {size}")
fname = f.get("filename") or f["key"]
checksum = f["checksum"].split(":")[-1]
remote_hash, local_hash = check_hash(fname, checksum)
if remote_hash == local_hash and options.cont:
eprint(f"{fname} is already downloaded correctly.")
continue
for _ in range(options.retry + 1):
try:
link = url = unquote(link)
filename = wget.download(
f"{link}?access_token={options.access_token}"
)
except Exception:
eprint(f" Download error. Original link: {link}")
time.sleep(options.pause)
else:
break
else:
eprint(" Too many errors.")
if not options.error:
eprint(" Download is aborted.")
if exceptions:
raise Exception("too many errors")
else:
sys.exit(1)
eprint(" Download continues with the next file.")
continue
if fname != filename:
os.rename(filename, fname)
eprint()
h1, h2 = check_hash(fname, checksum)
if h1 == h2:
eprint(f"Checksum is correct. ({h1})")
else:
eprint(f"Checksum is INCORRECT!({h1} got:{h2})")
if not options.keep:
eprint(" File is deleted.")
os.remove(fname)
else:
eprint(" File is NOT deleted!")
if not options.error:
sys.exit(1)
else:
eprint("All files have been downloaded.")
else:
eprint("Record could not get accessed.")
if exceptions:
raise Exception("Record could not get accessed.")
else:
sys.exit(1)