Skip to content

Commit

Permalink
add du command
Browse files Browse the repository at this point in the history
  • Loading branch information
SerhoLiu committed Mar 26, 2017
1 parent 7613147 commit 0b97de8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
39 changes: 38 additions & 1 deletion coscli/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import glob
import posixpath

from coscli.cos import COS
from coscli.cos import COS, COSObject
from coscli.utils import COSUri, output
from coscli.utils import format_datetime, format_size, list_dir_files
from coscli.tools import Uploader, Downloader, Deleter, MoveCopyer
Expand Down Expand Up @@ -281,3 +281,40 @@ def cos_mv_copy(action, config, usrc, udst, force, recursive, p):
mover.parallel_move_copy(p)
else:
mover.simple_move_copy()


def cos_du(config, uri, s, human):
cos = COS(config.cos_config)
cos_uri = COSUri(uri)

if cos.file_exists(cos_uri.bucket, cos_uri.path):
cos_objs = [cos.stat_file(cos_uri.bucket, cos_uri.path)]
elif cos.dir_exists(cos_uri.bucket, cos_uri.path):
if not cos_uri.path.endswith("/"):
cos_uri.path += "/"
if s:
cos_objs = cos.iter_path(cos_uri.bucket, cos_uri.path)
else:
cos_objs = [COSObject(cos_uri.path)]
else:
output("Path '%s' not exists" % uri)
return

records = []
for cos_obj in cos_objs:
size = 0
if cos_obj.is_dir:
for obj in cos.walk_path(cos_uri.bucket, cos_obj.path):
size += obj.filesize
else:
size = cos_obj.filesize

records.append((cos_obj, size))

records.sort(key=lambda x: x[0].ls_cmp_key())
for obj, size in records:
size, coeff = format_size(size, human)
output("%10s %s" % (
"%s%s" % (size, coeff),
COSUri.compose_uri(cos_uri.bucket, obj.path)
))
16 changes: 16 additions & 0 deletions coscli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,19 @@ def copy_command(config, usrc, udst, force, recursive, p):
command.cos_mv_copy("copy", config, usrc, udst, force, recursive, p)
except Exception as e:
handle_exception(e, config.debug)


@cli.command(name="du")
@click.argument("uri", nargs=1)
@click.option("-s", is_flag=True,
help="Display an entry for each specified file")
@click.option("--human", "-h", is_flag=True, help="Enable human readable.")
@pass_config
def du_command(config, uri, s, human):
"""
Displays sizes of files and directories contained in the given directory
"""
try:
command.cos_du(config, uri, s, human)
except Exception as e:
handle_exception(e, config.debug)

0 comments on commit 0b97de8

Please sign in to comment.