From: Rishabh Dave Date: Thu, 15 Aug 2019 06:22:12 +0000 (+0530) Subject: cephfs-shell: extend to_bytes() X-Git-Tag: v15.1.0~1555^2~7 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=e92b11c5a73b7253eb0befa6a395c167fc963c75;p=ceph-ci.git cephfs-shell: extend to_bytes() Make to_bytes() handle lists, None as well as other simple types as well. In case of None, to_bytes() returns None instead of returning b'None'. Signed-off-by: Rishabh Dave --- diff --git a/src/tools/cephfs/cephfs-shell b/src/tools/cephfs/cephfs-shell index dd86cb4830a..67337c7398f 100755 --- a/src/tools/cephfs/cephfs-shell +++ b/src/tools/cephfs/cephfs-shell @@ -102,9 +102,21 @@ def get_chunks(file_size): yield(chunk_start, final_chunk_size) -def to_bytes(string): - return bytes(string, encoding='utf-8') - +def to_bytes(param): + # don't convert as follows as it can lead unusable results like coverting + # [1, 2, 3, 4] to '[1, 2, 3, 4]' - + # str(param).encode('utf-8') + if isinstance(param, bytes): + return param + elif isinstance(param, str): + return bytes(param, encoding='utf-8') + elif isinstance(param, list): + return [i.encode('utf-8') if isinstance(i, str) else to_bytes(i) for \ + i in param] + elif isinstance(param, int) or isinstance(param, float): + return str(param).encode('utf-8') + elif param is None: + return None def ls(path, opts=''): # opts tries to be like /bin/ls opts