From patchwork Thu Apr 11 14:47:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1922611 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=YpHRcRxT; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::133; helo=smtp2.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=patchwork.ozlabs.org) Received: from smtp2.osuosl.org (smtp2.osuosl.org [IPv6:2605:bc80:3010::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VFjGm4r27z1yYB for ; Fri, 12 Apr 2024 00:47:44 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id AE7A141AB3; Thu, 11 Apr 2024 14:47:41 +0000 (UTC) X-Virus-Scanned: amavis at osuosl.org Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavis, port 10024) with ESMTP id dO2GKjimMjSw; Thu, 11 Apr 2024 14:47:39 +0000 (UTC) X-Comment: SPF check N/A for local connections - client-ip=2605:bc80:3010:104::8cd3:938; helo=lists.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver= DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org 1FCDF41A4B Authentication-Results: smtp2.osuosl.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=YpHRcRxT Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp2.osuosl.org (Postfix) with ESMTPS id 1FCDF41A4B; Thu, 11 Apr 2024 14:47:39 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 870AFC0DD5; Thu, 11 Apr 2024 14:47:38 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp2.osuosl.org (smtp2.osuosl.org [140.211.166.133]) by lists.linuxfoundation.org (Postfix) with ESMTP id 60F5FC0037 for ; Thu, 11 Apr 2024 14:47:37 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id 4F70C405EE for ; Thu, 11 Apr 2024 14:47:37 +0000 (UTC) X-Virus-Scanned: amavis at osuosl.org Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavis, port 10024) with ESMTP id 2bU7IxfpvqHd for ; Thu, 11 Apr 2024 14:47:36 +0000 (UTC) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=170.10.133.124; helo=us-smtp-delivery-124.mimecast.com; envelope-from=jmeng@redhat.com; receiver= DMARC-Filter: OpenDMARC Filter v1.4.2 smtp2.osuosl.org 2064140191 Authentication-Results: smtp2.osuosl.org; dmarc=pass (p=none dis=none) header.from=redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org 2064140191 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by smtp2.osuosl.org (Postfix) with ESMTPS id 2064140191 for ; Thu, 11 Apr 2024 14:47:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712846854; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=afoTsioWd9nuBVbXkU1NH96NwWqgUy5DymlTooVhfKM=; b=YpHRcRxTyEAoP9W4S7GW//qADjF2JXnkFbctrW7azhVRUbSQC25FORuvLql/x3XOTRr2iN Am6QdF07q7yRktjJFK0uB02Agw3nscGMgANfZf3PrHje8/jxYeFf0w8QsB10KsZ+Ay4a1B fr0l1SbYezz/YUvkdpWlli3Ij6bGVAI= Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-524-RuHCa7QaND29hiZ-AWNwlA-1; Thu, 11 Apr 2024 10:47:33 -0400 X-MC-Unique: RuHCa7QaND29hiZ-AWNwlA-1 Received: by mail-ej1-f71.google.com with SMTP id a640c23a62f3a-a467a6d4e3eso382862766b.3 for ; Thu, 11 Apr 2024 07:47:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712846852; x=1713451652; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=afoTsioWd9nuBVbXkU1NH96NwWqgUy5DymlTooVhfKM=; b=sI/2h2e70Zz/EOHKV8FyvVKLu3VxVd3Fhto4xYSgEVteIQF63p9F21pUoQHCnzvrCE 3nIjfwKEhMLzL9o+7aZpdssx5G6haWLU42vhiVA+C5EwN78fIysLaOromg6+s/ngu26j fiA6VH5s0w67L51x93b5MGO1QUBxmCVXLPqPQ9ZzzO/ZNWHz7QU00C2g+x07mmx06Rtg 7n88vSJivJVyhaU9Ja+rlIEu76mDzktMdF5aRmlZBKVIGVFhFPFGSgSUT5V5idmxjik3 ybFTsoLFo35vrBizsOUNhDfLXNWfCd9OkPWju8iiCH1ji9AHdgiWH7f9pAtKszxjENYr bl3g== X-Gm-Message-State: AOJu0YynpqkDMxqNjlIHpTY+tzYyY8Fua8kFQ0kaukrB8YFcjr9B+Ydi xWvGxficXnndo9Y0WZTgBu/cFXF/JNrWuReyTun1wuONjrLW16oWvG7NCy4zpqZt9pFIgJvWTyz jZtfqKANXmNCHttbyZRKE7BMemdH9D+yIgSuNMrcu/2t5F4qv0bfgnlnEUnhrqQCLTuTGLowxy6 JD+0CjOreo+xJ/o3+7OAfD+K3D+5KM X-Received: by 2002:a17:906:af1b:b0:a52:17f:e693 with SMTP id lx27-20020a170906af1b00b00a52017fe693mr3467101ejb.18.1712846852366; Thu, 11 Apr 2024 07:47:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHX6vuX6oWgMBHyO2Q7ci2T7gn9K5rPj4kHwfKYBBHFob2xtBNVPX4SQiwpe92xbiNgAfylKQ== X-Received: by 2002:a17:906:af1b:b0:a52:17f:e693 with SMTP id lx27-20020a170906af1b00b00a52017fe693mr3467079ejb.18.1712846851774; Thu, 11 Apr 2024 07:47:31 -0700 (PDT) Received: from positronik4lide.. ([87.122.56.49]) by smtp.gmail.com with ESMTPSA id lo17-20020a170906fa1100b00a466af74ef2sm816835ejb.2.2024.04.11.07.47.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 07:47:31 -0700 (PDT) From: jmeng@redhat.com To: dev@openvswitch.org, i.maximets@ovn.org, echaudro@redhat.com, ktraynor@redhat.com, aconole@redhat.com, rjarry@redhat.com Date: Thu, 11 Apr 2024 16:47:14 +0200 Message-Id: <20240411144718.1921869-3-jmeng@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240411144718.1921869-1-jmeng@redhat.com> References: <20240411144718.1921869-1-jmeng@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v8 2/6] python: Add global option for JSON output to Python tools. 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: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" From: Jakob Meng This patch introduces support for different output formats to the Python code, as did the previous commit for ovs-xxx tools like 'ovs-appctl --format json dpif/show'. In particular, tests/appctl.py gains a global option '-f,--format' which allows users to request JSON instead of plain-text for humans. Reported-at: https://bugzilla.redhat.com/1824861 Signed-off-by: Jakob Meng --- NEWS | 2 ++ python/ovs/unixctl/client.py | 14 ++++++++--- python/ovs/unixctl/server.py | 45 +++++++++++++++++++++++++++++++++--- python/ovs/util.py | 8 +++++++ tests/appctl.py | 19 ++++++++++++++- tests/unixctl-py.at | 3 +++ 6 files changed, 84 insertions(+), 7 deletions(-) diff --git a/NEWS b/NEWS index 03ef6486b..f18238159 100644 --- a/NEWS +++ b/NEWS @@ -10,6 +10,8 @@ Post-v3.3.0 - ovs-appctl: * Added new option [-f|--format] to choose the output format, e.g. 'json' or 'text' (by default). + - Python: + * Added support for choosing the output format, e.g. 'json' or 'text'. v3.3.0 - 16 Feb 2024 diff --git a/python/ovs/unixctl/client.py b/python/ovs/unixctl/client.py index 8283f99bb..000d261c0 100644 --- a/python/ovs/unixctl/client.py +++ b/python/ovs/unixctl/client.py @@ -14,6 +14,7 @@ import os +import ovs.json import ovs.jsonrpc import ovs.stream import ovs.util @@ -26,11 +27,12 @@ class UnixctlClient(object): assert isinstance(conn, ovs.jsonrpc.Connection) self._conn = conn - def transact(self, command, argv): + def transact(self, command, argv, fmt): assert isinstance(command, str) assert isinstance(argv, list) for arg in argv: assert isinstance(arg, str) + assert isinstance(fmt, ovs.util.OutputFormat) request = ovs.jsonrpc.Message.create_request(command, argv) error, reply = self._conn.transact_block(request) @@ -40,11 +42,17 @@ class UnixctlClient(object): % (self._conn.name, os.strerror(error))) return error, None, None + def to_string(body): + if fmt == ovs.util.OutputFormat.TEXT: + return str(body) + else: + return ovs.json.to_string(body) + if reply.error is not None: - return 0, str(reply.error), None + return 0, to_string(reply.error), None else: assert reply.result is not None - return 0, None, str(reply.result) + return 0, None, to_string(reply.result) def close(self): self._conn.close() diff --git a/python/ovs/unixctl/server.py b/python/ovs/unixctl/server.py index b9cb52fad..f95317c4a 100644 --- a/python/ovs/unixctl/server.py +++ b/python/ovs/unixctl/server.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import argparse import copy import errno import os @@ -35,6 +36,7 @@ class UnixctlConnection(object): assert isinstance(rpc, ovs.jsonrpc.Connection) self._rpc = rpc self._request_id = None + self._fmt = ovs.util.OutputFormat.TEXT def run(self): self._rpc.run() @@ -65,9 +67,15 @@ class UnixctlConnection(object): def reply(self, body): self._reply_impl(True, body) + def reply_json(self, body): + self._reply_impl_json(True, body) + def reply_error(self, body): self._reply_impl(False, body) + def reply_error_json(self, body): + self._reply_impl_json(False, body) + # Called only by unixctl classes. def _close(self): self._rpc.close() @@ -79,17 +87,27 @@ class UnixctlConnection(object): self._rpc.recv_wait(poller) def _reply_impl(self, success, body): - assert isinstance(success, bool) assert body is None or isinstance(body, str) - assert self._request_id is not None - if body is None: body = "" if body and not body.endswith("\n"): body += "\n" + if self._fmt == ovs.util.OutputFormat.JSON: + body = { + "reply-format": "plain", + "reply": body + } + + return self._reply_impl_json(success, body) + + def _reply_impl_json(self, success, body): + assert isinstance(success, bool) + + assert self._request_id is not None + if success: reply = Message.create_reply(body, self._request_id) else: @@ -136,6 +154,24 @@ def _unixctl_version(conn, unused_argv, version): conn.reply(version) +def _unixctl_set_options(conn, argv, unused_aux): + assert isinstance(conn, UnixctlConnection) + + parser = argparse.ArgumentParser() + parser.add_argument("--format", default="text", + choices=[fmt.name.lower() + for fmt in ovs.util.OutputFormat]) + + try: + args = parser.parse_args(args=argv) + except argparse.ArgumentError as e: + conn.reply_error(str(e)) + return + + conn._fmt = ovs.util.OutputFormat[args.format.upper()] + conn.reply(None) + + class UnixctlServer(object): def __init__(self, listener): assert isinstance(listener, ovs.stream.PassiveStream) @@ -210,4 +246,7 @@ class UnixctlServer(object): ovs.unixctl.command_register("version", "", 0, 0, _unixctl_version, version) + ovs.unixctl.command_register("set-options", "[--format text|json]", 2, + 2, _unixctl_set_options, None) + return 0, UnixctlServer(listener) diff --git a/python/ovs/util.py b/python/ovs/util.py index 3dba022f8..272ca683d 100644 --- a/python/ovs/util.py +++ b/python/ovs/util.py @@ -15,11 +15,19 @@ import os import os.path import sys +import enum PROGRAM_NAME = os.path.basename(sys.argv[0]) EOF = -1 +@enum.unique +# FIXME: Use @enum.verify(enum.NAMED_FLAGS) from Python 3.11 when available. +class OutputFormat(enum.IntFlag): + TEXT = 1 << 0 + JSON = 1 << 1 + + def abs_file_name(dir_, file_name): """If 'file_name' starts with '/', returns a copy of 'file_name'. Otherwise, returns an absolute path to 'file_name' considering it relative diff --git a/tests/appctl.py b/tests/appctl.py index b85b364fa..7ccf34cc3 100644 --- a/tests/appctl.py +++ b/tests/appctl.py @@ -49,14 +49,31 @@ def main(): help="Arguments to the command.") parser.add_argument("-T", "--timeout", metavar="SECS", help="wait at most SECS seconds for a response") + parser.add_argument("-f", "--format", metavar="FMT", + help="Output format.", default="text", + choices=[fmt.name.lower() + for fmt in ovs.util.OutputFormat]) args = parser.parse_args() signal_alarm(int(args.timeout) if args.timeout else None) ovs.vlog.Vlog.init() target = args.target + format = ovs.util.OutputFormat[args.format.upper()] client = connect_to_target(target) - err_no, error, result = client.transact(args.command, args.argv) + + if format != ovs.util.OutputFormat.TEXT: + err_no, error, _ = client.transact( + "set-options", ["--format", args.format], format) + + if err_no: + ovs.util.ovs_fatal(err_no, "%s: transaction error" % target) + elif error is not None: + sys.stderr.write(error) + ovs.util.ovs_error(0, "%s: server returned an error" % target) + sys.exit(2) + + err_no, error, result = client.transact(args.command, args.argv, format) client.close() if err_no: diff --git a/tests/unixctl-py.at b/tests/unixctl-py.at index 724006118..1440b30b7 100644 --- a/tests/unixctl-py.at +++ b/tests/unixctl-py.at @@ -100,6 +100,7 @@ The available commands are: exit help log [[arg ...]] + set-options [[--format text|json]] version vlog/close vlog/list @@ -112,6 +113,8 @@ AT_CHECK([PYAPPCTL_PY -t test-unixctl.py help], [0], [expout]) AT_CHECK([ovs-vsctl --version | sed 's/ovs-vsctl/test-unixctl.py/' | head -1 > expout]) AT_CHECK([APPCTL -t test-unixctl.py version], [0], [expout]) AT_CHECK([PYAPPCTL_PY -t test-unixctl.py version], [0], [expout]) +AT_CHECK_UNQUOTED([PYAPPCTL_PY -t test-unixctl.py --format json version], [0], [dnl +{"reply":"$(cat expout)\n","reply-format":"plain"}]) AT_CHECK([APPCTL -t test-unixctl.py echo robot ninja], [0], [stdout]) AT_CHECK([cat stdout | sed -e "s/u'/'/g"], [0], [dnl