1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 from __future__ import print_function
6 from __future__ import division
15 import arvados.commands._util as arv_cmd
17 from arvados._version import __version__
19 FileInfo = collections.namedtuple('FileInfo', ['stream_name', 'name', 'size'])
22 parser = argparse.ArgumentParser(
23 description='List contents of a manifest',
24 parents=[arv_cmd.retry_opt])
26 parser.add_argument('locator', type=str,
27 help="""Collection UUID or locator, optionally with a subdir path.""")
28 parser.add_argument('-s', action='store_true',
29 help="""List file sizes, in KiB.""")
30 parser.add_argument('--version', action='version',
31 version="%s %s" % (sys.argv[0], __version__),
32 help='Print version and exit.')
34 return parser.parse_args(args)
36 def size_formatter(coll_file):
37 return "{:>10}".format((coll_file.size + 1023) // 1024)
39 def name_formatter(coll_file):
40 return "{}/{}".format(coll_file.stream_name, coll_file.name)
42 def main(args, stdout, stderr, api_client=None, logger=None):
43 args = parse_args(args)
45 if api_client is None:
46 api_client = arvados.api('v1')
49 logger = logging.getLogger('arvados.arv-ls')
52 r = re.search(r'^(.*?)(/.*)?$', args.locator)
53 collection = r.group(1)
54 get_prefix = r.group(2)
56 cr = arvados.CollectionReader(collection, api_client=api_client,
57 num_retries=args.retries)
59 if get_prefix[-1] == '/':
60 get_prefix = get_prefix[:-1]
61 stream_name = '.' + get_prefix
62 reader = cr.find(stream_name)
63 if not (isinstance(reader, arvados.CollectionReader) or
64 isinstance(reader, arvados.collection.Subcollection)):
65 logger.error("'{}' is not a subdirectory".format(get_prefix))
70 except (arvados.errors.ApiError,
71 arvados.errors.ArgumentError,
72 arvados.errors.NotFoundError) as error:
73 logger.error("error fetching collection: {}".format(error))
78 formatters.append(size_formatter)
79 formatters.append(name_formatter)
81 for f in files_in_collection(reader, stream_name):
82 print(*(info_func(f) for info_func in formatters), file=stdout)
86 def files_in_collection(c, stream_name='.'):
87 # Sort first by file type, then alphabetically by file path.
88 for i in sorted(c.keys(),
90 isinstance(c[k], arvados.collection.Subcollection),
92 if isinstance(c[i], arvados.arvfile.ArvadosFile):
93 yield FileInfo(stream_name=stream_name,
96 elif isinstance(c[i], arvados.collection.Subcollection):
97 for f in files_in_collection(c[i], "{}/{}".format(stream_name, i)):