du_parser = argparse.ArgumentParser(
description='Disk Usage of a Directory')
du_parser.add_argument('dirs', type=str, action=path_to_bytes,
- help='Name of the directory.', nargs='?',
- default='.')
+ help='Name of the directory.', nargs='*',
+ default='')
du_parser.add_argument('-r', action='store_true',
help='Recursive Disk usage of all directories.')
"""
Disk Usage of a Directory
"""
+ dir_passed = args.dirs
if args.dirs == b'':
- args.dirs = cephfs.getcwd()
- for dir_ in args.dirs:
+ dir_passed = cephfs.getcwd()
+ if len(dir_passed) > 1:
+ dir_passed = dir_passed.split()
+
+ for dir_ in dir_passed:
+ dir_trav = sorted(set(dirwalk(dir_)))
+ if args.dirs and not dir_trav:
+ dir_trav.append(dir_)
+ else:
+ dir_trav.append('.')
+
if args.r:
- for i in reversed(sorted(set(dirwalk(dir_)))):
- i = os.path.normpath(i)
- try:
- xattr = cephfs.getxattr(i, 'ceph.dir.rbytes')
- self.poutput('{:10s} {}'.format(
- humansize(int(xattr.decode('utf-8'))), '.'
- + i.decode('utf-8')))
- except libcephfs.Error:
- continue
+ dir_trav = reversed(dir_trav)
+
+ for i in dir_trav:
+ try:
+ i_path = os.path.normpath(i)
+ if (i != '.') and (i_path[0] == '/'):
+ i = '.' + i_path
+
+ xattr = cephfs.getxattr(i_path,
+ 'ceph.dir.rbytes')
+ self.poutput('{:10s} {}'.format(humansize(int(
+ xattr.decode('utf-8'))), i.decode('utf-8'))
+ except (libcephfs.Error, OSError):
+ self.perror('{}: no such directory exists'.format(dir_),
+ False)
+ continue
else:
dir_ = os.path.normpath(dir_)
self.poutput('{:10s} {}'.format(humansize(int(cephfs.getxattr(