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