Add -l and --lh switches to swift 'list' command

Also updated man page and added an example in 'list'
command help message

Change-Id: Icf38de9bab6d387a9a1deed444a91f68b4389cfe
This commit is contained in:
Mark Seger 2013-05-22 18:45:42 +00:00 committed by Mark Seger
parent 2d12f09c66
commit 17bb3fa779
2 changed files with 76 additions and 6 deletions

View File

@ -26,7 +26,7 @@ from Queue import Queue
from random import shuffle
from sys import argv, exc_info, exit, stderr, stdout
from threading import enumerate as threading_enumerate, Thread
from time import sleep, time
from time import sleep, time, gmtime, strftime
from traceback import format_exception
from urllib import quote, unquote
@ -492,24 +492,60 @@ def st_download(parser, args, print_queue, error_queue):
put_errors_from_threads(object_threads, error_queue)
def prt_bytes(bytes, human_flag):
"""
convert a number > 1024 to printable format, either in 4 char -h format as
with ls -lh or return as 12 char right justified string (up to 999GB)
"""
if human_flag:
suffix = ''
mods = 'KMGTPEZY'
temp = float(bytes)
if temp > 0:
while (temp > 1023):
temp /= 1024.0
suffix = mods[0]
mods = mods[1:]
if suffix != '':
if temp > 9:
bytes = '%3d%s' % (temp, suffix)
else:
bytes = '%.1f%s' % (temp, suffix)
if suffix == '': # must be < 1024
bytes = '%4s' % bytes
else:
bytes = '%12s' % bytes
return(bytes)
st_list_help = '''
list [options] [container]
Lists the containers for the account or the objects for a container. -p or
--prefix is an option that will only list items beginning with that prefix.
-l produces output formatted like 'ls -l' and --lh like 'ls -lh'.
-d or --delimiter is option (for container listings only) that will roll up
items with the given delimiter (see Cloud Files general documentation for
what this means).
items with the given delimiter (see http://docs.openstack.org/
api/openstack-object-storage/1.0/content/list-objects.html)
'''.strip('\n')
def st_list(parser, args, print_queue, error_queue):
parser.add_option(
'-l', '--long', dest='long', help='Long listing '
'similar to ls -l command', action='store_true', default=False)
parser.add_option(
'--lh', dest='human', help='report sizes as human '
"similar to ls -lh switch, but -h taken", action='store_true',
default=False)
parser.add_option(
'-p', '--prefix', dest='prefix',
help='Will only list items beginning with the prefix')
parser.add_option(
'-d', '--delimiter', dest='delimiter',
help='Will roll up items with the given delimiter'
' (see Cloud Files general documentation for what this means)')
' (see Cloud Files general documentation for what this means)')
(options, args) = parse_args(parser, args)
args = args[1:]
if options.delimiter and not args:
@ -533,7 +569,35 @@ def st_list(parser, args, print_queue, error_queue):
if not items:
break
for item in items:
print_queue.put(item.get('name', item.get('subdir')))
item_name = item.get('name')
if not options.long and not options.human:
print_queue.put(item.get('name', item.get('subdir')))
else:
if len(args) == 0: # listing containers
bytes = prt_bytes(item.get('bytes'), options.human)
count = item.get('count')
try:
meta = conn.head_container(item_name)
utc = gmtime(float(meta.get('x-timestamp')))
datestamp = strftime('%Y-%m-%d %H:%M:%S', utc)
except ClientException:
datestamp = '????-??-?? ??:??:??'
print_queue.put("%5s %s %s %s" %
(count, bytes, datestamp, item_name))
else: # list container contents
subdir = item.get('subdir')
if subdir is None:
bytes = prt_bytes(item.get('bytes'), options.human)
date, xtime = item.get('last_modified').split('T')
xtime = xtime.split('.')[0]
else:
bytes = prt_bytes(0, options.human)
date = xtime = ''
item_name = subdir
print_queue.put("%s %10s %8s %s" %
(bytes, date, xtime, item_name))
marker = items[-1].get('name', items[-1].get('subdir'))
except ClientException as err:
if err.http_status != 404:
@ -543,7 +607,6 @@ def st_list(parser, args, print_queue, error_queue):
else:
error_queue.put('Container %s not found' % repr(args[0]))
st_stat_help = '''
stat [container] [object]
Displays information for the account, container, or object depending on the
@ -1160,6 +1223,8 @@ Examples:
%%prog --os-auth-token 6ee5eb33efad4e45ab46806eac010566 \\
--os-storage-url https://10.1.5.2:8080/v1/AUTH_ced809b6a4baea7aeab61a \\
list
%%prog list --lh
'''.strip('\n') % globals())
parser.add_option('-s', '--snet', action='store_true', dest='snet',
default=False, help='Use SERVICENET internal network')

View File

@ -50,6 +50,11 @@ The -p or --prefix is an option that will only list items beginning
with that prefix. The -d or --delimiter is option (for container listings only)
that will roll up items with the given delimiter (see Cloud Files general
documentation for what this means).
The -l and --lh options provide more detail, similar to ls -l and ls -lh, the latter
providing sizes in human readable format (eg 3K, 12M, etc). These latter 2 switches
use more overhead to get those details, which is directly proportional to the number
of container or objects being listed.
.RE
\fBupload\fR [\fIcommand-options\fR] container file_or_directory [\fIfile_or_directory\fR] [...]