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