233933
From 9d10b1fd102dc2d5bfa71891ded52b7a8f5e08d8 Mon Sep 17 00:00:00 2001
233933
From: Kotresh HR <khiremat@redhat.com>
233933
Date: Thu, 6 Jun 2019 12:54:04 +0530
233933
Subject: [PATCH 225/255] tests/utils: Fix py2/py3 util python scripts
233933
233933
Following files are fixed.
233933
233933
tests/bugs/distribute/overlap.py
233933
tests/utils/changelogparser.py
233933
tests/utils/create-files.py
233933
tests/utils/gfid-access.py
233933
tests/utils/libcxattr.py
233933
233933
> upstream patch link : https://review.gluster.org/#/c/glusterfs/+/22829/
233933
233933
>Change-Id: I3db857cc19e19163d368d913eaec1269fbc37140
233933
>updates: bz#1193929
233933
>Signed-off-by: Kotresh HR <khiremat@redhat.com>
233933
233933
Change-Id: I3db857cc19e19163d368d913eaec1269fbc37140
233933
BUG: 1704562
233933
Signed-off-by: Kotresh HR <khiremat@redhat.com>
233933
Reviewed-on: https://code.engineering.redhat.com/gerrit/175483
233933
Tested-by: RHGS Build Bot <nigelb@redhat.com>
233933
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
233933
---
233933
 tests/bugs/distribute/overlap.py  |   2 +-
233933
 tests/bugs/glusterfs/bug-902610.t |   2 +-
233933
 tests/utils/changelogparser.py    |   5 +-
233933
 tests/utils/create-files.py       |   9 +-
233933
 tests/utils/gfid-access.py        |  62 +++++++++----
233933
 tests/utils/libcxattr.py          |  22 +++--
233933
 tests/utils/py2py3.py             | 186 ++++++++++++++++++++++++++++++++++++++
233933
 7 files changed, 258 insertions(+), 30 deletions(-)
233933
 create mode 100644 tests/utils/py2py3.py
233933
233933
diff --git a/tests/bugs/distribute/overlap.py b/tests/bugs/distribute/overlap.py
233933
index 0941d37..2813979 100755
233933
--- a/tests/bugs/distribute/overlap.py
233933
+++ b/tests/bugs/distribute/overlap.py
233933
@@ -17,7 +17,7 @@ def calculate_one (ov, nv):
233933
 
233933
 def calculate_all (values):
233933
     total = 0
233933
-    nv_index = len(values) / 2
233933
+    nv_index = len(values) // 2
233933
     for old_val in values[:nv_index]:
233933
         new_val = values[nv_index]
233933
         nv_index += 1
233933
diff --git a/tests/bugs/glusterfs/bug-902610.t b/tests/bugs/glusterfs/bug-902610.t
233933
index b45e92b..112c947 100755
233933
--- a/tests/bugs/glusterfs/bug-902610.t
233933
+++ b/tests/bugs/glusterfs/bug-902610.t
233933
@@ -28,7 +28,7 @@ function get_layout()
233933
 	fi
233933
 
233933
 	# Figure out where the join point is.
233933
-	target=$( $PYTHON -c "print '%08x' % (0x$layout1_e + 1)")
233933
+	target=$( $PYTHON -c "print('%08x' % (0x$layout1_e + 1))")
233933
 	#echo "target for layout2 = $target" > /dev/tty
233933
 
233933
 	# The second layout should cover everything that the first doesn't.
233933
diff --git a/tests/utils/changelogparser.py b/tests/utils/changelogparser.py
233933
index e8e252d..3b8f81d 100644
233933
--- a/tests/utils/changelogparser.py
233933
+++ b/tests/utils/changelogparser.py
233933
@@ -125,7 +125,10 @@ class Record(object):
233933
             return repr(self.__dict__)
233933
 
233933
     def __str__(self):
233933
-        return unicode(self).encode('utf-8')
233933
+        if sys.version_info >= (3,):
233933
+            return self.__unicode__()
233933
+        else:
233933
+            return unicode(self).encode('utf-8')
233933
 
233933
 
233933
 def get_num_tokens(data, tokens, version=Version.V11):
233933
diff --git a/tests/utils/create-files.py b/tests/utils/create-files.py
233933
index b2a1961..04736e9 100755
233933
--- a/tests/utils/create-files.py
233933
+++ b/tests/utils/create-files.py
233933
@@ -19,6 +19,11 @@ import argparse
233933
 datsiz = 0
233933
 timr = 0
233933
 
233933
+def get_ascii_upper_alpha_digits():
233933
+    if sys.version_info > (3,0):
233933
+        return string.ascii_uppercase+string.digits
233933
+    else:
233933
+        return string.uppercase+string.digits
233933
 
233933
 def setLogger(filename):
233933
     global logger
233933
@@ -111,7 +116,7 @@ def create_tar_file(fil, size, mins, maxs, rand):
233933
 
233933
 def get_filename(flen):
233933
     size = flen
233933
-    char = string.uppercase+string.digits
233933
+    char = get_ascii_upper_alpha_digits()
233933
     st = ''.join(random.choice(char) for i in range(size))
233933
     ti = str((hex(int(str(time.time()).split('.')[0])))[2:])
233933
     return ti+"%%"+st
233933
@@ -175,7 +180,7 @@ def tar_files(files, file_count, inter, size, mins, maxs,
233933
 
233933
 
233933
 def setxattr_files(files, randname, dir_path):
233933
-    char = string.uppercase+string.digits
233933
+    char = get_ascii_upper_alpha_digits()
233933
     if not randname:
233933
         for k in range(files):
233933
             v = ''.join(random.choice(char) for i in range(10))
233933
diff --git a/tests/utils/gfid-access.py b/tests/utils/gfid-access.py
233933
index 556d2b4..c35c122 100755
233933
--- a/tests/utils/gfid-access.py
233933
+++ b/tests/utils/gfid-access.py
233933
@@ -33,23 +33,51 @@ def _fmt_mkdir(l):
233933
 def _fmt_symlink(l1, l2):
233933
     return "!II%dsI%ds%ds" % (37, l1+1, l2+1)
233933
 
233933
-def entry_pack_reg(gf, bn, mo, uid, gid):
233933
-    blen = len(bn)
233933
-    return struct.pack(_fmt_mknod(blen),
233933
-                       uid, gid, gf, mo, bn,
233933
-                       stat.S_IMODE(mo), 0, umask())
233933
-
233933
-def entry_pack_dir(gf, bn, mo, uid, gid):
233933
-    blen = len(bn)
233933
-    return struct.pack(_fmt_mkdir(blen),
233933
-                       uid, gid, gf, mo, bn,
233933
-                       stat.S_IMODE(mo), umask())
233933
-
233933
-def entry_pack_symlink(gf, bn, lnk, mo, uid, gid):
233933
-    blen = len(bn)
233933
-    llen = len(lnk)
233933
-    return struct.pack(_fmt_symlink(blen, llen),
233933
-                       uid, gid, gf, mo, bn, lnk)
233933
+
233933
+if sys.version_info > (3,):
233933
+    def entry_pack_reg(gf, bn, mo, uid, gid):
233933
+        bn_encoded = bn.encode()
233933
+        blen = len(bn_encoded)
233933
+        return struct.pack(_fmt_mknod(blen),
233933
+                           uid, gid, gf.encode(), mo, bn_encoded,
233933
+                           stat.S_IMODE(mo), 0, umask())
233933
+
233933
+    # mkdir
233933
+    def entry_pack_dir(gf, bn, mo, uid, gid):
233933
+        bn_encoded = bn.encode()
233933
+        blen = len(bn_encoded)
233933
+        return struct.pack(_fmt_mkdir(blen),
233933
+                           uid, gid, gf.encode(), mo, bn_encoded,
233933
+                           stat.S_IMODE(mo), umask())
233933
+    # symlink
233933
+    def entry_pack_symlink(gf, bn, lnk, st):
233933
+        bn_encoded = bn.encode()
233933
+        blen = len(bn_encoded)
233933
+        lnk_encoded = lnk.encode()
233933
+        llen = len(lnk_encoded)
233933
+        return struct.pack(_fmt_symlink(blen, llen),
233933
+                           st['uid'], st['gid'],
233933
+                           gf.encode(), st['mode'], bn_encoded,
233933
+                           lnk_encoded)
233933
+
233933
+else:
233933
+    def entry_pack_reg(gf, bn, mo, uid, gid):
233933
+        blen = len(bn)
233933
+        return struct.pack(_fmt_mknod(blen),
233933
+                           uid, gid, gf, mo, bn,
233933
+                           stat.S_IMODE(mo), 0, umask())
233933
+
233933
+    def entry_pack_dir(gf, bn, mo, uid, gid):
233933
+        blen = len(bn)
233933
+        return struct.pack(_fmt_mkdir(blen),
233933
+                           uid, gid, gf, mo, bn,
233933
+                           stat.S_IMODE(mo), umask())
233933
+
233933
+    def entry_pack_symlink(gf, bn, lnk, mo, uid, gid):
233933
+        blen = len(bn)
233933
+        llen = len(lnk)
233933
+        return struct.pack(_fmt_symlink(blen, llen),
233933
+                           uid, gid, gf, mo, bn, lnk)
233933
 
233933
 if __name__ == '__main__':
233933
     if len(sys.argv) < 9:
233933
diff --git a/tests/utils/libcxattr.py b/tests/utils/libcxattr.py
233933
index fd0b083..3f3ed1f 100644
233933
--- a/tests/utils/libcxattr.py
233933
+++ b/tests/utils/libcxattr.py
233933
@@ -10,7 +10,9 @@
233933
 
233933
 import os
233933
 import sys
233933
-from ctypes import CDLL, c_int, create_string_buffer
233933
+from ctypes import CDLL, c_int
233933
+from py2py3 import bytearray_to_str, gr_create_string_buffer
233933
+from py2py3 import gr_query_xattr, gr_lsetxattr, gr_lremovexattr
233933
 
233933
 
233933
 class Xattr(object):
233933
@@ -47,20 +49,23 @@ class Xattr(object):
233933
     @classmethod
233933
     def _query_xattr(cls, path, siz, syscall, *a):
233933
         if siz:
233933
-            buf = create_string_buffer('\0' * siz)
233933
+            buf = gr_create_string_buffer(siz)
233933
         else:
233933
             buf = None
233933
         ret = getattr(cls.libc, syscall)(*((path,) + a + (buf, siz)))
233933
         if ret == -1:
233933
             cls.raise_oserr()
233933
         if siz:
233933
-            return buf.raw[:ret]
233933
+            # py2 and py3 compatibility. Convert bytes array
233933
+            # to string
233933
+            result = bytearray_to_str(buf.raw)
233933
+            return result[:ret]
233933
         else:
233933
             return ret
233933
 
233933
     @classmethod
233933
     def lgetxattr(cls, path, attr, siz=0):
233933
-        return cls._query_xattr(path, siz, 'lgetxattr', attr)
233933
+        return gr_query_xattr(cls, path, siz, 'lgetxattr', attr)
233933
 
233933
     @classmethod
233933
     def lgetxattr_buf(cls, path, attr):
233933
@@ -74,20 +79,21 @@ class Xattr(object):
233933
 
233933
     @classmethod
233933
     def llistxattr(cls, path, siz=0):
233933
-        ret = cls._query_xattr(path, siz, 'llistxattr')
233933
+        ret = gr_query_xattr(cls, path, siz, 'llistxattr')
233933
         if isinstance(ret, str):
233933
-            ret = ret.split('\0')
233933
+            ret = ret.strip('\0')
233933
+            ret = ret.split('\0') if ret else []
233933
         return ret
233933
 
233933
     @classmethod
233933
     def lsetxattr(cls, path, attr, val):
233933
-        ret = cls.libc.lsetxattr(path, attr, val, len(val), 0)
233933
+        ret = gr_lsetxattr(cls, path, attr, val)
233933
         if ret == -1:
233933
             cls.raise_oserr()
233933
 
233933
     @classmethod
233933
     def lremovexattr(cls, path, attr):
233933
-        ret = cls.libc.lremovexattr(path, attr)
233933
+        ret = gr_lremovexattr(cls, path, attr)
233933
         if ret == -1:
233933
             cls.raise_oserr()
233933
 
233933
diff --git a/tests/utils/py2py3.py b/tests/utils/py2py3.py
233933
new file mode 100644
233933
index 0000000..63aca10
233933
--- /dev/null
233933
+++ b/tests/utils/py2py3.py
233933
@@ -0,0 +1,186 @@
233933
+#
233933
+# Copyright (c) 2018 Red Hat, Inc. <http://www.redhat.com>
233933
+# This file is part of GlusterFS.
233933
+
233933
+# This file is licensed to you under your choice of the GNU Lesser
233933
+# General Public License, version 3 or any later version (LGPLv3 or
233933
+# later), or the GNU General Public License, version 2 (GPLv2), in all
233933
+# cases as published by the Free Software Foundation.
233933
+#
233933
+
233933
+# All python2/python3 compatibility routines
233933
+
233933
+import sys
233933
+import os
233933
+import stat
233933
+import struct
233933
+from ctypes import create_string_buffer
233933
+
233933
+def umask():
233933
+    return os.umask(0)
233933
+
233933
+if sys.version_info >= (3,):
233933
+    def pipe():
233933
+        (r, w) = os.pipe()
233933
+        os.set_inheritable(r, True)
233933
+        os.set_inheritable(w, True)
233933
+        return (r, w)
233933
+
233933
+    # Raw conversion of bytearray to string. Used in the cases where
233933
+    # buffer is created by create_string_buffer which is a 8-bit char
233933
+    # array and passed to syscalls to fetch results. Using encode/decode
233933
+    # doesn't work as it converts to string altering the size.
233933
+    def bytearray_to_str(byte_arr):
233933
+        return ''.join([chr(b) for b in byte_arr])
233933
+
233933
+    # Raw conversion of string to bytes. This is required to convert
233933
+    # back the string into bytearray(c char array) to use in struc
233933
+    # pack/unpacking. Again encode/decode can't be used as it
233933
+    # converts it alters size.
233933
+    def str_to_bytearray(string):
233933
+        return bytes([ord(c) for c in string])
233933
+
233933
+    def gr_create_string_buffer(size):
233933
+        return create_string_buffer(b'\0', size)
233933
+
233933
+    def gr_query_xattr(cls, path, size, syscall, attr=None):
233933
+        if attr:
233933
+            return cls._query_xattr(path.encode(), size, syscall,
233933
+                                    attr.encode())
233933
+        else:
233933
+            return cls._query_xattr(path.encode(), size, syscall)
233933
+
233933
+    def gr_lsetxattr(cls, path, attr, val):
233933
+        return cls.libc.lsetxattr(path.encode(), attr.encode(), val,
233933
+                                  len(val), 0)
233933
+
233933
+    def gr_lremovexattr(cls, path, attr):
233933
+        return cls.libc.lremovexattr(path.encode(), attr.encode())
233933
+
233933
+    def gr_cl_register(cls, brick, path, log_file, log_level, retries):
233933
+        return cls._get_api('gf_changelog_register')(brick.encode(),
233933
+                                                     path.encode(),
233933
+                                                     log_file.encode(),
233933
+                                                     log_level, retries)
233933
+
233933
+    def gr_cl_done(cls, clfile):
233933
+        return cls._get_api('gf_changelog_done')(clfile.encode())
233933
+
233933
+    def gr_cl_history_changelog(cls, changelog_path, start, end, num_parallel,
233933
+                                actual_end):
233933
+        return cls._get_api('gf_history_changelog')(changelog_path.encode(),
233933
+                                                    start, end, num_parallel,
233933
+                                                    actual_end)
233933
+
233933
+    def gr_cl_history_done(cls, clfile):
233933
+        return cls._get_api('gf_history_changelog_done')(clfile.encode())
233933
+
233933
+    # regular file
233933
+
233933
+    def entry_pack_reg(cls, gf, bn, mo, uid, gid):
233933
+        bn_encoded = bn.encode()
233933
+        blen = len(bn_encoded)
233933
+        return struct.pack(cls._fmt_mknod(blen),
233933
+                           uid, gid, gf.encode(), mo, bn_encoded,
233933
+                           stat.S_IMODE(mo), 0, umask())
233933
+
233933
+    def entry_pack_reg_stat(cls, gf, bn, st):
233933
+        bn_encoded = bn.encode()
233933
+        blen = len(bn_encoded)
233933
+        mo = st['mode']
233933
+        return struct.pack(cls._fmt_mknod(blen),
233933
+                           st['uid'], st['gid'],
233933
+                           gf.encode(), mo, bn_encoded,
233933
+                           stat.S_IMODE(mo), 0, umask())
233933
+    # mkdir
233933
+
233933
+    def entry_pack_mkdir(cls, gf, bn, mo, uid, gid):
233933
+        bn_encoded = bn.encode()
233933
+        blen = len(bn_encoded)
233933
+        return struct.pack(cls._fmt_mkdir(blen),
233933
+                           uid, gid, gf.encode(), mo, bn_encoded,
233933
+                           stat.S_IMODE(mo), umask())
233933
+    # symlink
233933
+
233933
+    def entry_pack_symlink(cls, gf, bn, lnk, st):
233933
+        bn_encoded = bn.encode()
233933
+        blen = len(bn_encoded)
233933
+        lnk_encoded = lnk.encode()
233933
+        llen = len(lnk_encoded)
233933
+        return struct.pack(cls._fmt_symlink(blen, llen),
233933
+                           st['uid'], st['gid'],
233933
+                           gf.encode(), st['mode'], bn_encoded,
233933
+                           lnk_encoded)
233933
+else:
233933
+    def pipe():
233933
+        (r, w) = os.pipe()
233933
+        return (r, w)
233933
+
233933
+    # Raw conversion of bytearray to string
233933
+    def bytearray_to_str(byte_arr):
233933
+        return byte_arr
233933
+
233933
+    # Raw conversion of string to bytearray
233933
+    def str_to_bytearray(string):
233933
+        return string
233933
+
233933
+    def gr_create_string_buffer(size):
233933
+        return create_string_buffer('\0', size)
233933
+
233933
+    def gr_query_xattr(cls, path, size, syscall, attr=None):
233933
+        if attr:
233933
+            return cls._query_xattr(path, size, syscall, attr)
233933
+        else:
233933
+            return cls._query_xattr(path, size, syscall)
233933
+
233933
+    def gr_lsetxattr(cls, path, attr, val):
233933
+        return cls.libc.lsetxattr(path, attr, val, len(val), 0)
233933
+
233933
+    def gr_lremovexattr(cls, path, attr):
233933
+        return cls.libc.lremovexattr(path, attr)
233933
+
233933
+    def gr_cl_register(cls, brick, path, log_file, log_level, retries):
233933
+        return cls._get_api('gf_changelog_register')(brick, path, log_file,
233933
+                                                     log_level, retries)
233933
+
233933
+    def gr_cl_done(cls, clfile):
233933
+        return cls._get_api('gf_changelog_done')(clfile)
233933
+
233933
+    def gr_cl_history_changelog(cls, changelog_path, start, end, num_parallel,
233933
+                                actual_end):
233933
+        return cls._get_api('gf_history_changelog')(changelog_path, start, end,
233933
+                                                    num_parallel, actual_end)
233933
+
233933
+    def gr_cl_history_done(cls, clfile):
233933
+        return cls._get_api('gf_history_changelog_done')(clfile)
233933
+
233933
+    # regular file
233933
+
233933
+    def entry_pack_reg(cls, gf, bn, mo, uid, gid):
233933
+        blen = len(bn)
233933
+        return struct.pack(cls._fmt_mknod(blen),
233933
+                           uid, gid, gf, mo, bn,
233933
+                           stat.S_IMODE(mo), 0, umask())
233933
+
233933
+    def entry_pack_reg_stat(cls, gf, bn, st):
233933
+        blen = len(bn)
233933
+        mo = st['mode']
233933
+        return struct.pack(cls._fmt_mknod(blen),
233933
+                           st['uid'], st['gid'],
233933
+                           gf, mo, bn,
233933
+                           stat.S_IMODE(mo), 0, umask())
233933
+    # mkdir
233933
+
233933
+    def entry_pack_mkdir(cls, gf, bn, mo, uid, gid):
233933
+        blen = len(bn)
233933
+        return struct.pack(cls._fmt_mkdir(blen),
233933
+                           uid, gid, gf, mo, bn,
233933
+                           stat.S_IMODE(mo), umask())
233933
+    # symlink
233933
+
233933
+    def entry_pack_symlink(cls, gf, bn, lnk, st):
233933
+        blen = len(bn)
233933
+        llen = len(lnk)
233933
+        return struct.pack(cls._fmt_symlink(blen, llen),
233933
+                           st['uid'], st['gid'],
233933
+                           gf, st['mode'], bn, lnk)
233933
-- 
233933
1.8.3.1
233933