From 23e530a135fc419fba401448290f8b1809e23f53 Mon Sep 17 00:00:00 2001 From: Shwetha K Acharya Date: Mon, 10 Dec 2018 13:45:34 +0530 Subject: glusterfind: python2 to python3 compat Made necessary modifications to ensure python3 compatibilty. fixes: bz#1658116 Change-Id: I5cf1d0447eaf3c44eb444245d1f67aadd60705c3 Signed-off-by: Shwetha K Acharya --- tools/glusterfind/glusterfind.in | 3 +- tools/glusterfind/src/Makefile.am | 2 +- tools/glusterfind/src/brickfind.py | 4 +-- tools/glusterfind/src/changelog.py | 6 ++-- tools/glusterfind/src/conf.py | 6 ++-- tools/glusterfind/src/gfind_py2py3.py | 63 +++++++++++++++++++++++++++++++++ tools/glusterfind/src/libgfchangelog.py | 16 +++++---- tools/glusterfind/src/main.py | 15 ++++---- tools/glusterfind/src/nodeagent.py | 4 +-- 9 files changed, 94 insertions(+), 25 deletions(-) create mode 100644 tools/glusterfind/src/gfind_py2py3.py (limited to 'tools/glusterfind') diff --git a/tools/glusterfind/glusterfind.in b/tools/glusterfind/glusterfind.in index 79f06faeae0..569bf412105 100644 --- a/tools/glusterfind/glusterfind.in +++ b/tools/glusterfind/glusterfind.in @@ -1,4 +1,4 @@ -#!/usr/bin/python3 +#!/usr/bin/env python # Copyright (c) 2015 Red Hat, Inc. # This file is part of GlusterFS. @@ -10,6 +10,7 @@ import sys sys.path.insert(1, '@GLUSTERFS_LIBEXECDIR@/') +sys.path.insert(1, '@GLUSTERFS_LIBEXECDIR@/glusterfind') from glusterfind.main import main diff --git a/tools/glusterfind/src/Makefile.am b/tools/glusterfind/src/Makefile.am index c180f051933..43b6141b01c 100644 --- a/tools/glusterfind/src/Makefile.am +++ b/tools/glusterfind/src/Makefile.am @@ -2,7 +2,7 @@ glusterfinddir = $(GLUSTERFS_LIBEXECDIR)/glusterfind if WITH_SERVER glusterfind_PYTHON = conf.py utils.py __init__.py \ - main.py libgfchangelog.py changelogdata.py + main.py libgfchangelog.py changelogdata.py gfind_py2py3.py glusterfind_SCRIPTS = changelog.py nodeagent.py \ brickfind.py diff --git a/tools/glusterfind/src/brickfind.py b/tools/glusterfind/src/brickfind.py index 95b7a3d991d..1e72c00e521 100644 --- a/tools/glusterfind/src/brickfind.py +++ b/tools/glusterfind/src/brickfind.py @@ -1,4 +1,4 @@ -#!/usr/bin/python3 +#!/usr/bin/env python # -*- coding: utf-8 -*- # Copyright (c) 2015 Red Hat, Inc. @@ -113,6 +113,6 @@ if __name__ == "__main__": time_to_update = int(time.time()) brickfind_crawl(args.brick, args) if not args.only_query: - with open(status_file_pre, "w", buffering=0) as f: + with open(status_file_pre, "w") as f: f.write(str(time_to_update)) sys.exit(0) diff --git a/tools/glusterfind/src/changelog.py b/tools/glusterfind/src/changelog.py index 437612ff0a4..62669e9507e 100644 --- a/tools/glusterfind/src/changelog.py +++ b/tools/glusterfind/src/changelog.py @@ -1,4 +1,4 @@ -#!/usr/bin/python3 +#!/usr/bin/env python # -*- coding: utf-8 -*- # Copyright (c) 2015 Red Hat, Inc. @@ -339,7 +339,7 @@ def changelog_crawl(brick, start, end, args): # WORKING_DIR/BRICKHASH/OUTFILE working_dir = os.path.dirname(args.outfile) - brickhash = hashlib.sha1(brick) + brickhash = hashlib.sha1(brick.encode()) brickhash = str(brickhash.hexdigest()) working_dir = os.path.join(working_dir, brickhash) @@ -420,7 +420,7 @@ if __name__ == "__main__": end)) actual_end = changelog_crawl(args.brick, start, end, args) if not args.only_query: - with open(status_file_pre, "w", buffering=0) as f: + with open(status_file_pre, "w") as f: f.write(str(actual_end)) logger.info("%s Finished Changelog Crawl - End: %s" % (args.brick, diff --git a/tools/glusterfind/src/conf.py b/tools/glusterfind/src/conf.py index ab766f85c0f..3849ba5dd1f 100644 --- a/tools/glusterfind/src/conf.py +++ b/tools/glusterfind/src/conf.py @@ -10,11 +10,11 @@ import os try: - from configparser import ConfigParser + from ConfigParser import ConfigParser except ImportError: - import ConfigParser + from configparser import ConfigParser -config = ConfigParser.ConfigParser() +config = ConfigParser() config.read(os.path.join(os.path.dirname(os.path.abspath(__file__)), "tool.conf")) diff --git a/tools/glusterfind/src/gfind_py2py3.py b/tools/glusterfind/src/gfind_py2py3.py new file mode 100644 index 00000000000..1d41ec5aa22 --- /dev/null +++ b/tools/glusterfind/src/gfind_py2py3.py @@ -0,0 +1,63 @@ +# +# Copyright (c) 2018 Red Hat, Inc. +# This file is part of GlusterFS. + +# This file is licensed to you under your choice of the GNU Lesser +# General Public License, version 3 or any later version (LGPLv3 or +# later), or the GNU General Public License, version 2 (GPLv2), in all +# cases as published by the Free Software Foundation. +# + +# All python2/python3 compatibility routines + +import os +import sys +from ctypes import create_string_buffer + +if sys.version_info >= (3,): + + # Raw conversion of bytearray to string. Used in the cases where + # buffer is created by create_string_buffer which is a 8-bit char + # array and passed to syscalls to fetch results. Using encode/decode + # doesn't work as it converts to string altering the size. + # def bytearray_to_str(byte_arr): + def bytearray_to_str(byte_arr): + return ''.join([chr(b) for b in byte_arr]) + + def gf_create_string_buffer(size): + return create_string_buffer(b'\0', size) + + def gfind_history_changelog(libgfc, changelog_path, start, end, num_parallel, + actual_end): + return libgfc.gf_history_changelog(changelog_path.encode(), start, end, num_parallel, + actual_end) + + def gfind_changelog_register(libgfc, brick, path, log_file, log_level, + retries): + return libgfc.gf_changelog_register(brick.encode(), path.encode(), log_file.encode(), + log_level, retries) + + def gfind_history_changelog_done(libgfc, clfile): + return libgfc.gf_history_changelog_done(clfile.encode()) + +else: + + # Raw conversion of bytearray to string + def bytearray_to_str(byte_arr): + return byte_arr + + def gf_create_string_buffer(size): + return create_string_buffer('\0', size) + + def gfind_history_changelog(libgfc, changelog_path, start, end, num_parallel, + actual_end): + return libgfc.gf_history_changelog(changelog_path, start, end, + num_parallel, actual_end) + + def gfind_changelog_register(libgfc, brick, path, log_file, log_level, + retries): + return libgfc.gf_changelog_register(brick, path, log_file, + log_level, retries) + + def gfind_history_changelog_done(libgfc, clfile): + return libgfc.gf_history_changelog_done(clfile) diff --git a/tools/glusterfind/src/libgfchangelog.py b/tools/glusterfind/src/libgfchangelog.py index afb3387db2d..1ef177ab2a5 100644 --- a/tools/glusterfind/src/libgfchangelog.py +++ b/tools/glusterfind/src/libgfchangelog.py @@ -11,6 +11,9 @@ import os from ctypes import CDLL, get_errno, create_string_buffer, c_ulong, byref from ctypes import RTLD_GLOBAL +from gfind_py2py3 import bytearray_to_str, gf_create_string_buffer +from gfind_py2py3 import gfind_history_changelog, gfind_changelog_register +from gfind_py2py3 import gfind_history_changelog_done class ChangelogException(OSError): @@ -33,8 +36,7 @@ def cl_init(): def cl_register(brick, path, log_file, log_level, retries=0): - ret = libgfc.gf_changelog_register(brick, path, log_file, - log_level, retries) + ret = gfind_changelog_register(libgfc, brick, path, log_file,log_level, retries) if ret == -1: raise_oserr(prefix="gf_changelog_register") @@ -49,7 +51,7 @@ def cl_history_scan(): def cl_history_changelog(changelog_path, start, end, num_parallel): actual_end = c_ulong() - ret = libgfc.gf_history_changelog(changelog_path, start, end, + ret = gfind_history_changelog(libgfc,changelog_path, start, end, num_parallel, byref(actual_end)) if ret == -1: @@ -70,13 +72,15 @@ def cl_history_getchanges(): return f.split('.')[-1] changes = [] - buf = create_string_buffer('\0', 4096) + buf = gf_create_string_buffer(4096) while True: ret = libgfc.gf_history_changelog_next_change(buf, 4096) if ret in (0, -1): break - changes.append(buf.raw[:ret - 1]) + # py2 and py3 compatibility + result = bytearray_to_str(buf.raw[:ret - 1]) + changes.append(result) if ret == -1: raise_oserr(prefix="gf_history_changelog_next_change") @@ -84,6 +88,6 @@ def cl_history_getchanges(): def cl_history_done(clfile): - ret = libgfc.gf_history_changelog_done(clfile) + ret = gfind_history_changelog_done(libgfc, clfile) if ret == -1: raise_oserr(prefix="gf_history_changelog_done") diff --git a/tools/glusterfind/src/main.py b/tools/glusterfind/src/main.py index 8719b786127..1145fc1e2e8 100644 --- a/tools/glusterfind/src/main.py +++ b/tools/glusterfind/src/main.py @@ -1,4 +1,4 @@ -#!/usr/bin/python3 +#!/usr/bin/env python # -*- coding: utf-8 -*- # Copyright (c) 2015 Red Hat, Inc. @@ -322,6 +322,7 @@ def _get_args(): parser = ArgumentParser(formatter_class=RawDescriptionHelpFormatter, description=PROG_DESCRIPTION) subparsers = parser.add_subparsers(dest="mode") + subparsers.required = True # create [--debug] [--force] parser_create = subparsers.add_parser('create') @@ -511,15 +512,15 @@ def write_output(outfile, outfilemerger, field_separator): continue if row_2_rep and row_2_rep != "": - f.write("{0}{1}{2}{3}{4}\n".format(row[0], + f.write(u"{0}{1}{2}{3}{4}\n".format(row[0], field_separator, p_rep, field_separator, - row_2_rep)) + row_2_rep).encode()) else: - f.write("{0}{1}{2}\n".format(row[0], + f.write(u"{0}{1}{2}\n".format(row[0], field_separator, - p_rep)) + p_rep).encode()) def mode_create(session_dir, args): @@ -559,7 +560,7 @@ def mode_create(session_dir, args): run_cmd_nodes("create", args, time_to_update=str(time_to_update)) if not os.path.exists(status_file) or args.reset_session_time: - with open(status_file, "w", buffering=0) as f: + with open(status_file, "w") as f: f.write(str(time_to_update)) sys.stdout.write("Session %s created with volume %s\n" % @@ -712,7 +713,7 @@ def mode_pre(session_dir, args): run_cmd_nodes("cleanup", args, tmpfilename=gtmpfilename) - with open(status_file_pre, "w", buffering=0) as f: + with open(status_file_pre, "w") as f: f.write(str(endtime_to_update)) sys.stdout.write("Generated output file %s\n" % args.outfile) diff --git a/tools/glusterfind/src/nodeagent.py b/tools/glusterfind/src/nodeagent.py index c36341f216e..2ddc77affd2 100644 --- a/tools/glusterfind/src/nodeagent.py +++ b/tools/glusterfind/src/nodeagent.py @@ -1,4 +1,4 @@ -#!/usr/bin/python3 +#!/usr/bin/env python # -*- coding: utf-8 -*- # Copyright (c) 2015 Red Hat, Inc. @@ -58,7 +58,7 @@ def mode_create(args): logger=logger) if not os.path.exists(status_file) or args.reset_session_time: - with open(status_file, "w", buffering=0) as f: + with open(status_file, "w") as f: f.write(args.time_to_update) sys.exit(0) -- cgit