/
__init__.py
456 lines (359 loc) · 14 KB
/
__init__.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
from parcel import const, manifest
from ..download.client import GDCHTTPDownloadClient
from ..upload import read_manifest, GDCUploadClient
from .. import defaults
from ..version import __version__
from ..argparser import subparsers
from cmd2 import Cmd, options, make_option
import types
import os
import shlex
class GDCREPL(Cmd):
HEADER = """
Type 'help' for a list of commands or 'help <topic>' for detailed usage.
""".format(version=__version__)
BASIC_HELP = """Basic help:
This tool is for downloading data from the GDC. A few simple usage examples:
- To simply download an open access file (file_id_1):
> download file_id_1
- To simply download a controlled access file (file_id_1) by
providing the path to a token downloaded from the GDC Portal
(type 'help token' for more information):
> download file_id_1 -t path/token.txt
- To download files from a manifest by providing the path to the
manifest:
> download -t path/manifest.txt
- To upload files from manifest:
> upload path/manifest.txt
> upload path/resume_manifest.txt
- To delete files from manifest:
> delete path/manifest.txt
- To abort a previous partial upload
> abort path/resume_manifest.txt
"""
TIPS = """TIPS:
- Rather than type out path names, try dragging and dropping manifest and token files into the terminal.
- You can execute shell commands by prepending '!', i.e. !ls.
- You can run the gdc-client binary with advanced options from the command line (gdc-client --help).
"""
BASIC_COMMANDS = """Basic commands are:
- download (download files in registry)
- add (adds ids to registry)
- list (lists file ids already registered)
- manifest (add ids from a GDC manifest file to registry)
- remove (remove ids from registry)
- token (load an authorization token file)
- cd (move to directory you want to download to)
- pwd (print the current working directory)
- set (set advanced configuration setting)
- settings (list advanced configuration settings)
- upload (upload files to object storage)
- delete (delete files from object storage)
- abort (abort a previous partial upload)
"""
def __init__(self, *args, **kwargs):
self.file_ids = set()
self.token = None
Cmd.__init__(self, *args, **kwargs)
print(self.HEADER)
print(self.BASIC_COMMANDS)
print(self.TIPS)
self.settings = dict(
server=defaults.tcp_url,
protocol='tcp',
processes=defaults.processes,
save_interval=const.SAVE_INTERVAL,
http_chunk_size=const.HTTP_CHUNK_SIZE,
download_related_files=True,
download_annotations=True,
part_size=defaults.part_size,
multipart=True,
verify=True,
)
def get(self, setting, stype):
val = self.settings[setting]
if isinstance(val, str) and stype == bool:
return val.lower in ['true', 't']
else:
try:
val = stype(val)
except:
raise RuntimeError(
"Unable to convert setting '{}'='{}' to type {}".format(
setting, val, stype))
return val
def format_docstring(self, doc):
doc = doc.strip() or 'No help available.'
lines = ['']+[l.strip() for l in doc.split('\n')]
doc = "\n| ".join(lines) + '\n'
return doc
def _add_ids(self, ids):
"""Adds ids to the instance id list.
"""
if not ids:
return
start_len = len(self.file_ids)
map(self.file_ids.add, ids)
end_len = len(self.file_ids)
print(("Loaded {} new file ids. There are {} file ids to download.\n"
"Start download with 'download'. List ids with 'list'").format(
end_len - start_len, end_len))
def _remove_ids(self, ids):
"""Removes ids from the instance id list.
"""
if not ids:
return
start_len = len(self.file_ids)
for fid in ids:
try:
self.file_ids.remove(fid)
except Exception as msg:
print('Unable to remove id {}: {}'.format(fid, msg))
end_len = len(self.file_ids)
print(("Removed {} file ids from registry.\n"
"There are {} file ids left to download.\n"
"Start download with 'download'. List ids with 'list'").format(
start_len - end_len, end_len))
def do_manifest(self, manifest_path):
"""Loads a manifest file and adds each id to the instace id list to
download later
usage: manifest <path_to_file>
"""
if not manifest_path:
print('No manifest specified to load.')
self.do_help('manifest')
return
with open(manifest_path, 'r') as fd:
self._add_ids([f['id'] for f in manifest.parse(fd)])
def do_token(self, token_path):
"""Load your token from a file. This token will be used to
authenticate you when downloading protected data. You can
download a token after logging into the GDC Portal at
https://gdc-portal.nci.nih.gov/
You can clear the token with the 'clear_token' command.
useage: token <path_to_file>
"""
if not token_path:
print('No token specified to load.')
if self.token:
print('Previously loaded token ({} bytes).'.format(
len(self.token)))
else:
print('No token previously loaded')
return
with open(token_path, 'r') as f:
self.token = f.read().strip()
print('Loaded token ({} bytes).'.format(len(self.token)))
def do_list(self, arg):
"""List all ids that have been registered to download. You can
download with the 'download' command. You can add ids to the
registry with the 'add' command.
ussage: list
"""
if not self.file_ids:
print("No files to download. Add files with 'manifest' or 'add'.")
else:
print('File ids schedule to download:')
for fid in self.file_ids:
print(' - {}'.format(fid))
def do_add(self, arg):
"""Add an id to the registry to be downloaded with the 'download'
command. Enter each id separated by a space.
You can list the ids in the registry with the 'list' command.
You can remove ids form the registry with the 'remove'
command. You can clear all ids from the registry with the
'clear' command.
usage: add <id1> [<id2> ...]
"""
ids = shlex.split(arg)
if not ids:
print('No ids specified.')
self.do_help('add')
return
self._add_ids(ids)
def do_remove(self, arg):
"""Remove ids from the id registry. Enter each id separated by a
space.
You can clear all ids from the registry with the 'clear'
command.
usage: add <id1> [<id2> ...]
"""
ids = shlex.split(arg)
if not ids:
print('No ids specified.')
self.do_help('remove')
return
self._remove_ids(ids)
def do_clear(self, arg):
"""Remove all ids from the id registry.
You can selectively remove ids from the registry with the 'remove'
command.
usage: clear
"""
self.file_ids = set()
print('Cleared registered file ids.')
def do_clear_token(self, arg):
"""Clears the authorization token.
usage: clear_token
"""
self.token = None
print("Cleared authorization token.")
def do_cd(self, path):
"""Command to change the directory where the files will be downloaded
to.
unix usage : cd temp\downloads
windosx usage: cd temp/downloads
"""
os.chdir(os.path.expanduser(path))
print('Changed working directory to {}'.format(os.getcwd()))
def do_pwd(self, path):
"""Prints out the current working directory. This is where the data
will be downloaded.
You can use the 'cd' command to change directories.
usage: pwd
"""
print(os.getcwd())
@options([
make_option('-m', '--manifest', help="a manifest file to load ids from"),
make_option('-t', '--token', help="a token file to load"),
])
def do_download(self, arg, opts=None):
"""Download files that have been registered from (via 'manifest' or
'add').
You can specify additional file ids to download in this
command separated by spaces.
You can specify a manifest to load from and download using the
'-m' flag.
You can specify a token to load from and authenticate with using
'-t' flag.
usage: download [<additional_id1> <additional_id2> ... ]
usage: download -m manifest.txt -t token.txt
"""
manifest_path = opts.get('manifest')
token_path = opts.get('token')
if arg:
self._add_ids(arg.split())
if manifest_path:
self.do_manifest(manifest_path)
if token_path:
self.do_token(token_path)
if not self.file_ids:
self.do_list(None)
return
if self.settings['protocol'] == 'tcp':
kwargs = dict(
uri=self.settings['server'],
token=self.token,
n_procs=self.get('processes', int),
directory=os.path.abspath(os.getcwd()),
http_chunk_size=self.get('http_chunk_size', int),
save_interval=self.get('save_interval', int),
download_related_files=self.get('download_related_files', bool),
download_annotations=self.get('download_annotations', bool),
)
client = GDCHTTPDownloadClient(**kwargs)
elif self.settings['protocol'].lower() == 'udt':
raise RuntimeError(
("UDT protocol not supported in interactive mode. "
"Try running 'gdc-client download -u'"))
else:
raise RuntimeError('Protocol ({}) not recognized'.format(
self.settings['protocol']))
try:
downloaded, errors = client.download_files(self.file_ids)
except Exception as e:
print('Download aborted: {}'.format(str(e)))
self._remove_ids(downloaded)
def _get_upload_client(self, manifest):
with open(manifest, 'r') as fd:
files = read_manifest(fd)
client = GDCUploadClient(
token=self.token, processes=self.get('processes', int),
multipart=self.get('multipart', bool),
part_size=self.get('part_size', int),
server=self.settings['server'],
files=files, verify=self.get('verify', bool), manifest_name=manifest)
return client
def do_upload(self, manifest):
'''upload files given a manifest path'''
client = self._get_upload_client(manifest)
client.upload()
def do_abort(self, manifest):
'''abort a partially uploaded file'''
client = self._get_upload_client(manifest)
client.abort()
def do_delete(self, manifest):
'''delete files given a manifest path'''
client = self._get_upload_client(manifest)
client.delete()
def do_help(self, arg):
"""Command to print help message to user.
"""
if not arg:
print(self.HEADER)
print(self.BASIC_COMMANDS)
print(self.BASIC_HELP)
else:
fname = 'do_{}'.format(arg)
f = getattr(self, fname, None)
if not f:
print('help: {} is not a command'.format(fname))
print self.format_docstring(f.__doc__)
def do_set(self, arg):
"""Change the value of a setting.
You can use the 'settings' command to see current settings.
usage: set <SETTING> <NEW VALUE>
"""
try:
attr, value = shlex.split(arg)
except:
print('invalid syntax.')
return self.do_help('set')
if attr not in self.settings:
raise ValueError(
"{} not a valid setting. Try 'settings'.".format(attr))
print("Updating {} from '{}' to '{}'".format(
attr, self.settings[attr], value))
self.settings[attr] = value
def complete_set(self, text, line, start_index, end_index):
if text:
return [key+' ' for key in self.settings if key.startswith(text)]
else:
return self.settings.keys()
def do_settings(self, arg):
"""Lists the current settings and their values.
You can update the value of these settings with the 'set' command.
usage: settings
"""
print('-- Settings --')
pad = max([len(key) for key in self.settings])+1
for key, val in self.settings.iteritems():
print('{}: {}'.format(key.ljust(pad), val))
def do_show(self, arg):
"""Alias for 'settings' commands.
"""
self.do_settings(arg)
def do_commands(self, arg):
"""Alias for 'help' command.
"""
self.do_help(arg)
def run_repl(args):
r = GDCREPL()
r.prompt = '\ngdc-client repl > '
r.cmdloop()
def main():
args = subparsers.parse_args()
if args.verbose:
logging.root.setLevel(logging.DEBUG)
# If there are arguments other than subcommand, run cli
if sys.argv[2:]:
try:
run_cli(args)
except Exception as e:
if args.debug:
raise
else:
print('Process aborted: {}'.format(str(e)))
# Else, run as a repl
else:
run_repl(args)