]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
cephfs-shell: refactor do_du()
authorRishabh Dave <ridave@redhat.com>
Fri, 16 Aug 2019 17:37:32 +0000 (23:07 +0530)
committerRishabh Dave <ridave@redhat.com>
Fri, 13 Sep 2019 05:15:42 +0000 (10:45 +0530)
Improve code's readability and keep all paths as bytes.
Specifically, define a function within do do_du() to simplify the code
that handles recursion, rename args.dirs to args.paths, get rid of
dir_passed and use args.paths directly instead,  change the default
value of args.dirs to a list with one member that contains current
working directory, rewrite the docstring, and few more similar changes.

Signed-off-by: Rishabh Dave <ridave@redhat.com>
src/tools/cephfs/cephfs-shell

index 174d118b802852058a82a92dfa6a6dcc3294aed1..e38cc809679b2e15ea3b989bdbf29539c1787f4c 100755 (executable)
@@ -1135,58 +1135,47 @@ sub-directories, files')
 
     du_parser = argparse.ArgumentParser(
         description='Disk Usage of a Directory')
-    du_parser.add_argument('dirs', type=str, action=path_to_bytes,
+    du_parser.add_argument('paths', type=str, action=path_to_bytes,
                            help='Name of the directory.', nargs='*',
-                           default='')
+                           default=[b'.'])
     du_parser.add_argument('-r', action='store_true',
                            help='Recursive Disk usage of all directories.')
 
     @with_argparser(du_parser)
     def do_du(self, args):
         """
-        Disk Usage of a Directory
+        Print disk usage of a given path(s).
         """
-        dir_passed = args.dirs
-        if args.dirs == b'':
-            dir_passed = cephfs.getcwd()
-            if len(dir_passed) > 1:
-                dir_passed = dir_passed.split()
+        def print_disk_usage(files):
+            if isinstance(files, bytes):
+                files = (files, )
 
-        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('.')
-
-            for i in dir_trav:
+            for f in files:
                 try:
-                    i_path = os.path.normpath(i)
-                    if (i is not '.') and (i_path[0] is '/'):
-                        i = '.' + i_path
-
-                    st = cephfs.lstat(i_path)
+                    st = cephfs.lstat(f)
 
                     if stat.S_ISDIR(st.st_mode):
-                        dusage = int(cephfs.getxattr(i_path,
-                            'ceph.dir.rbytes').decode('utf-8'))
-                    elif stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode):
+                        dusage = int(cephfs.getxattr(f,
+                                     'ceph.dir.rbytes').decode('utf-8'))
+                    else:
                         dusage = st.st_size
 
+                    # print path in local context
+                    f = os.path.normpath(f)
+                    if f[0] is ord('/'):
+                        f = b'.' + f
                     self.poutput('{:10s} {}'.format(humansize(dusage),
-                                 i.decode('utf-8')))
-                    # Assigning dusage None to avoid cases where its value
-                    # from previous iterations ends up being reused.
-                    dusage = None
+                        f.decode('utf-8')))
                 except (libcephfs.Error, OSError):
-                    self.perror('{}: no such directory exists'.format(
-                                dir_), False)
+                    self.perror('{} : no such directory exists'.format(f.\
+                        decode('utf-8')), False)
                     continue
+
+        for path in args.paths:
+            if args.r:
+                print_disk_usage(sorted(set(dirwalk(path)).union({path})))
             else:
-                dir_ = os.path.normpath(dir_)
-                self.poutput('{:10s} {}'.format(humansize(int(cephfs.getxattr(
-                    dir_, 'ceph.dir.rbytes').decode('utf-8'))), '.'
-                    + dir_.decode('utf-8')))
+                print_disk_usage(path)
 
     quota_parser = argparse.ArgumentParser(
         description='Quota management for a Directory')