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