From patchwork Tue Feb 25 19:59:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: William Tu X-Patchwork-Id: 1244502 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.137; helo=fraxinus.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=imOXbL3K; dkim-atps=neutral Received: from fraxinus.osuosl.org (smtp4.osuosl.org [140.211.166.137]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48RqXC4TPJz9sPK for ; Wed, 26 Feb 2020 06:59:39 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by fraxinus.osuosl.org (Postfix) with ESMTP id 9789C852CF; Tue, 25 Feb 2020 19:59:37 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from fraxinus.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id XpIcgtnsuFAi; Tue, 25 Feb 2020 19:59:35 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by fraxinus.osuosl.org (Postfix) with ESMTP id 91A12844E2; Tue, 25 Feb 2020 19:59:35 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 72504C18DA; Tue, 25 Feb 2020 19:59:35 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from hemlock.osuosl.org (smtp2.osuosl.org [140.211.166.133]) by lists.linuxfoundation.org (Postfix) with ESMTP id D32C4C0177 for ; Tue, 25 Feb 2020 19:59:34 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by hemlock.osuosl.org (Postfix) with ESMTP id BC20E86EB1 for ; Tue, 25 Feb 2020 19:59:34 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from hemlock.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id uF9w3EYigsnL for ; Tue, 25 Feb 2020 19:59:34 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-pg1-f195.google.com (mail-pg1-f195.google.com [209.85.215.195]) by hemlock.osuosl.org (Postfix) with ESMTPS id EACC986B58 for ; Tue, 25 Feb 2020 19:59:33 +0000 (UTC) Received: by mail-pg1-f195.google.com with SMTP id d9so78088pgu.3 for ; Tue, 25 Feb 2020 11:59:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=hq2XiRMpIxDlzizTdHj1odHwoOft0Ur2ApJGyJPdG5A=; b=imOXbL3KqExoH0tBF6jG9DZmM1ToNW6hzpYobGus66psZaetrtSvuoSzORbhXVnwkp vIe55bYVd9wOEW1SjKNi+ch3j4WR2ozOhPkHVqL94voFgZtKjTgPYEi0qmVPlKPQUtrz n/78XqFvug+NsEbqu38PGgNWyKomDQICiv6S/xUfzMj/vnVpkVcMB/M+Vh6a05IPKH73 7vLmEf6xIrapBNJty25Xp2RuTPi7JRwEWwjeWF6uBVNNiOzqrXsFhs/bh7x0eBw2wHzR ei4WQSny0yFSHQCPVl24wSelVys868+IowFo2/isK98mM7qoWHnYo5hw9BR2SoBvawZo N1Yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=hq2XiRMpIxDlzizTdHj1odHwoOft0Ur2ApJGyJPdG5A=; b=unk1mIAmfYGQwTp/ABEM9drTnn8f/8Y0ZsIMZp6Soj29IDD54Fps8ug46zpN4BBT2R QRmV8Z0y3vpj+ckfnwtWD3Wi9DDzLRGwbKcKdWLPGyyfc0jPo2jZqAjUKsLe/CjpfZ38 tq1Cm2SQdVcUZmR4eZRBJArCHjq5wZvezrxTOnkDzEv6E/VgN5LF5qgoLFOQem0wMYJ1 ztTTO5rbCsMrSH7OzyXMcnEyarcTuUN/7HwzS5l5V08CIMEYXbCocZAJk5i9/rtkr0v+ nuFL0sCa160Vr5IDKntKvB1+j+ecOeXqDUUvnRIEz3DaRkntMRPyvv9KFffYpm3YHwgV frCA== X-Gm-Message-State: APjAAAWTfYrfpb51Rgznl1Fm2uG+o8O19vHBPgiE7uR1rrm9zBQ8bJHl 2tA9+sKH8kWN9Tf+EkQas9bHhIOO X-Google-Smtp-Source: APXvYqybuxBxKgtaMF5+DiVF47MTZtMCKeTxt6/y0GjjFqHULOwMt7y4GGxy7xj56Thod3w3bxz/kg== X-Received: by 2002:aa7:82ce:: with SMTP id f14mr353832pfn.167.1582660772839; Tue, 25 Feb 2020 11:59:32 -0800 (PST) Received: from sc9-mailhost2.vmware.com (c-76-21-95-192.hsd1.ca.comcast.net. [76.21.95.192]) by smtp.gmail.com with ESMTPSA id q187sm17990842pfq.185.2020.02.25.11.59.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 25 Feb 2020 11:59:32 -0800 (PST) From: William Tu To: dev@openvswitch.org Date: Tue, 25 Feb 2020 11:59:06 -0800 Message-Id: <1582660746-81551-1-git-send-email-u9012063@gmail.com> X-Mailer: git-send-email 2.7.4 Cc: guru@ovn.org Subject: [ovs-dev] [PATCH] ovs-bugtool: Fix for python3. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" Currently ovs-bugtool does not work due to Python2 deprecated. When moving to Python3, a couple of things are broken in ovs-bugtool, ex: import libraries issues such as StringIO and commands. Also there are some bytes to string convertion in this patch because in Python3,strings are now always Unicode and a new type 'bytes' is added for handling binary strings. Signed-off-by: William Tu --- utilities/bugtool/ovs-bugtool.in | 73 ++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 37 deletions(-) diff --git a/utilities/bugtool/ovs-bugtool.in b/utilities/bugtool/ovs-bugtool.in index e55bfc2ed58e..c31bd6d77a24 100755 --- a/utilities/bugtool/ovs-bugtool.in +++ b/utilities/bugtool/ovs-bugtool.in @@ -33,8 +33,8 @@ # or func_output(). # -import StringIO -import commands +import io +import subprocess import fcntl import getopt import hashlib @@ -344,10 +344,10 @@ def collect_data(): while True: process_lists = {} - for (k, v) in data.items(): + for (k, v) in list(data.items()): cap = v['cap'] if 'cmd_args' in v: - if 'output' not in v.keys(): + if 'output' not in list(v.keys()): v['output'] = StringIOmtime() if v['repeat_count'] > 0: if cap not in process_lists: @@ -364,11 +364,11 @@ def collect_data(): time.sleep(command_delay) else: first_run = False - run_procs(process_lists.values()) + run_procs(list(process_lists.values())) else: break - for (k, v) in data.items(): + for (k, v) in list(data.items()): cap = v['cap'] if 'filename' in v and v['filename'].startswith('/proc/'): # proc files must be read into memory @@ -454,7 +454,7 @@ Output options: except: pass - entries = [e for e in caps.keys() if caps[e][CHECKED]] + entries = [e for e in list(caps.keys()) if caps[e][CHECKED]] for (k, v) in options: if k == '--capabilities': @@ -496,7 +496,7 @@ Output options: output_file = v elif k == '--all': - entries = caps.keys() + entries = list(caps.keys()) elif k == '--unlimited': unlimited_data = True elif k == '--debug': @@ -683,7 +683,7 @@ exclude those logs from the archive. CAP_OPENVSWITCH_LOGS, CAP_NETWORK_CONFIG] ovs_info_list = ['process-tree'] # We cannot use iteritems, since we modify 'data' as we pass through - for (k, v) in data.items(): + for (k, v) in list(data.items()): cap = v['cap'] if 'filename' in v: info = k[0] @@ -704,7 +704,7 @@ exclude those logs from the archive. # permit the user to filter out data # We cannot use iteritems, since we modify 'data' as we pass through - for (k, v) in sorted(data.items()): + for (k, v) in (data.items()): cap = v['cap'] if 'filename' in v: key = k[0] @@ -745,7 +745,7 @@ exclude those logs from the archive. if dbg: print("Category sizes (max, actual):\n", file=sys.stderr) - for c in caps.keys(): + for c in list(caps.keys()): print(" %s (%d, %d)" % (c, caps[c][MAX_SIZE], cap_sizes[c]), file=sys.stderr) @@ -782,7 +782,7 @@ def dump_scsi_hosts(cap): def module_info(cap): - output = StringIO.StringIO() + output = io.StringIO() modules = open(PROC_MODULES, 'r') procs = [] @@ -806,7 +806,7 @@ def multipathd_topology(cap): def dp_list(): - output = StringIO.StringIO() + output = io.StringIO() procs = [ProcOutput([OVS_DPCTL, 'dump-dps'], caps[CAP_NETWORK_STATUS][MAX_TIME], output)] @@ -828,7 +828,7 @@ def collect_ovsdb(): if os.path.isfile(OPENVSWITCH_COMPACT_DB): os.unlink(OPENVSWITCH_COMPACT_DB) - output = StringIO.StringIO() + output = io.StringIO() max_time = 5 procs = [ProcOutput(['ovsdb-tool', 'compact', OPENVSWITCH_CONF_DB, OPENVSWITCH_COMPACT_DB], @@ -863,15 +863,14 @@ def fd_usage(cap): fd_dict[num_fds].append(name.replace('\0', ' ').strip()) finally: fh.close() - keys = fd_dict.keys() - keys.sort(lambda a, b: int(b) - int(a)) + keys = list(fd_dict.keys()) for k in keys: output += "%s: %s\n" % (k, str(fd_dict[k])) return output def dump_rdac_groups(cap): - output = StringIO.StringIO() + output = io.StringIO() procs = [ProcOutput([MPPUTIL, '-a'], caps[cap][MAX_TIME], output)] run_procs([procs]) @@ -957,11 +956,11 @@ def load_plugins(just_capabilities=False, filter=None): if el.tagName == "files": newest_first = getBoolAttr(el, 'newest_first') if el.getAttribute("type") == "logs": - for fn in getText(el.childNodes).split(): + for fn in getText(el.childNodes).decode("ASCII").split(): prefix_output(dir, fn, newest_first=newest_first, last_mod_time=log_last_mod_time) else: - file_output(dir, getText(el.childNodes).split(), + file_output(dir, getText(el.childNodes).decode("ASCII").split(), newest_first=newest_first) elif el.tagName == "directory": pattern = el.getAttribute("pattern") @@ -970,12 +969,12 @@ def load_plugins(just_capabilities=False, filter=None): negate = getBoolAttr(el, 'negate') newest_first = getBoolAttr(el, 'newest_first') if el.getAttribute("type") == "logs": - tree_output(dir, getText(el.childNodes), + tree_output(dir, getText(el.childNodes).decode("ASCII"), pattern and re.compile(pattern) or None, negate=negate, newest_first=newest_first, last_mod_time=log_last_mod_time) else: - tree_output(dir, getText(el.childNodes), + tree_output(dir, getText(el.childNodes).decode("ASCII"), pattern and re.compile(pattern) or None, negate=negate, newest_first=newest_first) elif el.tagName == "command": @@ -987,15 +986,15 @@ def load_plugins(just_capabilities=False, filter=None): repeat_count = int(el.getAttribute("repeat")) if repeat_count > 1: cmd_output(dir, - DATE + ';' + getText(el.childNodes), + DATE + ';' + getText(el.childNodes).decode("ASCII"), label, binary=binary, repeat_count=repeat_count) else: - cmd_output(dir, getText(el.childNodes), + cmd_output(dir, [getText(el.childNodes).decode("ASCII")], label, binary=binary, repeat_count=repeat_count) except: - cmd_output(dir, getText(el.childNodes), label, + cmd_output(dir, [getText(el.childNodes).decode("ASCII")], label, binary=binary) @@ -1020,7 +1019,7 @@ def make_tar(subdir, suffix, output_fd, output_file): tf = tarfile.open(None, 'w', os.fdopen(output_fd, 'a')) try: - for (k, v) in data.items(): + for (k, v) in list(data.items()): try: tar_filename = os.path.join(subdir, construct_filename(k, v)) ti = tarfile.TarInfo(tar_filename) @@ -1061,7 +1060,7 @@ def make_zip(subdir, output_file): os.umask(old_umask) try: - for (k, v) in data.items(): + for (k, v) in list(data.items()): try: dest = os.path.join(subdir, construct_filename(k, v)) @@ -1095,11 +1094,11 @@ def make_inventory(inventory, subdir): s.setAttribute('date', time.strftime('%c')) s.setAttribute('hostname', platform.node()) s.setAttribute('uname', ' '.join(platform.uname())) - s.setAttribute('uptime', commands.getoutput(UPTIME)) + s.setAttribute('uptime', subprocess.getoutput(UPTIME)) document.getElementsByTagName(INVENTORY_XML_ROOT)[0].appendChild(s) - map(lambda k_v: inventory_entry(document, subdir, k_v[0], k_v[1]), - inventory.items()) + list(map(lambda k_v: inventory_entry(document, subdir, k_v[0], k_v[1]), + list(inventory.items()))) return document.toprettyxml() @@ -1190,8 +1189,8 @@ def size_of(f, pattern, negate): def print_capabilities(): document = getDOMImplementation().createDocument( "ns", CAP_XML_ROOT, None) - map(lambda key: capability(document, key), - [k for k in caps.keys() if not caps[k][HIDDEN]]) + list(map(lambda key: capability(document, key), + [k for k in list(caps.keys()) if not caps[k][HIDDEN]])) print(document.toprettyxml()) @@ -1210,12 +1209,12 @@ def capability(document, key): def prettyDict(d): - format = '%%-%ds: %%s' % max(map(len, [k for k, _ in d.items()])) - return '\n'.join([format % i for i in d.items()]) + '\n' + format = '%%-%ds: %%s' % max(list(map(len, [k for k, _ in list(d.items())]))) + return '\n'.join([format % i for i in list(d.items())]) + '\n' def yes(prompt): - yn = input(prompt) + yn = eval(input(prompt)) return len(yn) == 0 or yn.lower()[0] == 'y' @@ -1391,13 +1390,13 @@ def get_free_disk_space(path): return s.f_frsize * s.f_bfree -class StringIOmtime(StringIO.StringIO): +class StringIOmtime(io.StringIO): def __init__(self, buf=''): - StringIO.StringIO.__init__(self, buf) + io.StringIO.__init__(self, buf) self.mtime = time.time() def write(self, s): - StringIO.StringIO.write(self, s) + io.StringIO.write(self, str(s)) self.mtime = time.time()