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