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