- try:
- if not args.n:
- if not args.f and os.path.exists(args.destination):
- abort('Local file %s already exists.' % (args.destination,))
- with open(args.destination, 'wb') as f:
- try:
- c = api_client.collections().get(uuid=collection).execute()
- manifest = c['manifest_text']
- except Exception as e:
- logger.warning(
- "Collection %s not found. " +
- "Trying to fetch directly from Keep (deprecated).",
- collection)
- manifest = arvados.KeepClient(
- api_client=api_client).get(collection)
- f.write(manifest)
- sys.exit(0)
- except arvados.errors.NotFoundError as e:
- abort(e)
-
-reader = arvados.CollectionReader(collection)
+ if not args.n:
+ open_flags = os.O_CREAT | os.O_WRONLY
+ if not args.f:
+ open_flags |= os.O_EXCL
+ try:
+ out_fd = os.open(args.destination, open_flags)
+ with os.fdopen(out_fd, 'wb') as out_file:
+ out_file.write(reader.manifest_text())
+ except (IOError, OSError) as error:
+ abort("can't write to '{}': {}".format(args.destination, error))
+ except (arvados.errors.ApiError, arvados.errors.KeepReadError) as error:
+ abort("failed to download '{}': {}".format(collection, error))
+ sys.exit(0)