From nobody Sat Nov 15 13:37:59 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1595018222; cv=none; d=zohomail.com; s=zohoarc; b=QHCMeAMRrnfMQCwCtxy5Fal+uJit3hwWW0OBh1hGmf7xcB2nP7//O602i8qHaoZFjF54ygETblO0F/G7zkXGpvinkZDipvOHGxyVlKGJwRrzoefp8x7KE81sc0AL1HP+isBvqbtAjzbv66x0k+E2zul4nPs9rekMjDGqftmupA4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1595018222; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=YZOBwBBn9Q8zZljvmjtMM12SRKnwyEyG67balOtqryk=; b=ix9ShwCaT9WJbM2je9dNAZGYodUD32VrSVJQDlAd5Bae/o5vZY2urERq3Hhh8HMwtFWqvlVZ3IdnyzcGqBDg11PViVF6g3h9KDGVlZs4O1md7Ddy0CPTNdDKoF8xQdeGWcqS7p1aqgMv7TQsiKcH+Cdjz3x7HQF4rICiNOY5j2Y= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 159501822280130.681741989346847; Fri, 17 Jul 2020 13:37:02 -0700 (PDT) Received: from localhost ([::1]:36738 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jwX6L-0007il-Hj for importer@patchew.org; Fri, 17 Jul 2020 16:37:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41286) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jwX5J-0006Dz-Mx for qemu-devel@nongnu.org; Fri, 17 Jul 2020 16:35:57 -0400 Received: from mail-pg1-x535.google.com ([2607:f8b0:4864:20::535]:44943) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jwX5H-0000U7-Nh for qemu-devel@nongnu.org; Fri, 17 Jul 2020 16:35:57 -0400 Received: by mail-pg1-x535.google.com with SMTP id j19so7190751pgm.11 for ; Fri, 17 Jul 2020 13:35:55 -0700 (PDT) Received: from Rfoley-MA01.hsd1.ma.comcast.net ([2601:199:4480:60c0:b0d0:3aae:620c:a8c5]) by smtp.gmail.com with ESMTPSA id x7sm8212848pfq.197.2020.07.17.13.35.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Jul 2020 13:35:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=YZOBwBBn9Q8zZljvmjtMM12SRKnwyEyG67balOtqryk=; b=oRZaLHdGz/gywpAQP7qTgsGbf1z1BVX493v3sfLJM7OrkHG/5Zty0pdSz0jfUtYqAm Bvl/YZeBTnlFQtJLu5gViOVbKDQGApxY4sy6PJyFO3d+0zsGgW3ebHJwjgSrwRw0pWID by20Kh39yq1QmjwwzBgpQFKxu/ujGqUIfIS4CXERqOduF0M+89O6LUDiSkREQPn7ajgd OScXC0nBdlI9+tqZEtvIFUDjMJxGy55MrUn/hPN884fE6cvS99+4nUc5D3NRv/IrOhxe zVh4FO9fTWtiTGv3GchygZFSCrMvP6aw9UU6VNrFez2bQTALeewQ34Y7Tc4nIC2VSqQK nTcw== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=YZOBwBBn9Q8zZljvmjtMM12SRKnwyEyG67balOtqryk=; b=aoojO37XrqocY0I4bqSG5ve4I3rvS1txnJSrUFaQy6mpnVolxXLfXHps+cfPFYpPPG 8vtLc8EJf5MLfcS9z43zveEFrz0gx5rXfjg0hg3pF0UqixZGPSbd8ddRNO+7cBc9p2kM ApD7YG8XckpDrASNkud7xI2pWWkPBTnKDKKXvJlDaVEr3aGPghq9Yh2EQo6rlyjUMi05 59a6ulSkuZhXOjnVCf44YSL6sbiLbyGTpGk8E+Gn83klh+nyUu6aAvBAyXknt2sWgVJK GsWPAxtEgj2qYviHX2577nIXg5gvUIxWxe8fDrJo0o8vCDL5EnRpvbr9iJjGDZN1vM/s wwzw== X-Gm-Message-State: AOAM530UQl0gkkrOdI2inGgavNtjbTS/Du06ofaDdgLPS0/upTrqhkrF sGoX6+/oFspGKuw59396ZGE9OKNEjno= X-Google-Smtp-Source: ABdhPJxtCtyP3TAlIRTARH3WeYjqQ2+Q5eFYRNsGdCuYUMF0kdnvDrXb0+eQqhI5tzTE1JJ9QeJNSQ== X-Received: by 2002:a63:cd4d:: with SMTP id a13mr10216595pgj.49.1595018153631; Fri, 17 Jul 2020 13:35:53 -0700 (PDT) From: Robert Foley To: qemu-devel@nongnu.org Subject: [PATCH v1 1/3] python/qemu: Cleanup changes to ConsoleSocket Date: Fri, 17 Jul 2020 16:30:39 -0400 Message-Id: <20200717203041.9867-2-robert.foley@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200717203041.9867-1-robert.foley@linaro.org> References: <20200717203041.9867-1-robert.foley@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::535; envelope-from=robert.foley@linaro.org; helo=mail-pg1-x535.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: robert.foley@linaro.org, jsnow@redhat.com, Cleber Rosa , peter.puhov@linaro.org, alex.bennee@linaro.org, Eduardo Habkost Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) The changes to console_socket.py and machine.py are to cleanup for pylint and flake8. Reviewed-by: Alex Benn=C3=A9e Signed-off-by: Robert Foley --- python/qemu/console_socket.py | 57 ++++++++++++++++++----------------- python/qemu/machine.py | 7 +++-- python/qemu/pylintrc | 2 +- 3 files changed, 34 insertions(+), 32 deletions(-) diff --git a/python/qemu/console_socket.py b/python/qemu/console_socket.py index 830cb7c628..09986bc215 100644 --- a/python/qemu/console_socket.py +++ b/python/qemu/console_socket.py @@ -1,12 +1,9 @@ -#!/usr/bin/env python3 -# -# This python module implements a ConsoleSocket object which is -# designed always drain the socket itself, and place -# the bytes into a in memory buffer for later processing. -# -# Optionally a file path can be passed in and we will also -# dump the characters to this file for debug. -# +""" +QEMU Console Socket Module: + +This python module implements a ConsoleSocket object, +which can drain a socket and optionally dump the bytes to file. +""" # Copyright 2020 Linaro # # Authors: @@ -15,20 +12,27 @@ # This code is licensed under the GPL version 2 or later. See # the COPYING file in the top-level directory. # + import asyncore import socket import threading -import io -import os -import sys from collections import deque import time -import traceback + =20 class ConsoleSocket(asyncore.dispatcher): + """ + ConsoleSocket represents a socket attached to a char device. =20 + Drains the socket and places the bytes into an in memory buffer + for later processing. + + Optionally a file path can be passed in and we will also + dump the characters to this file for debugging purposes. + """ def __init__(self, address, file=3DNone): self._recv_timeout_sec =3D 300 + self._sleep_time =3D 0.5 self._buffer =3D deque() self._asyncore_thread =3D None self._sock =3D socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) @@ -70,31 +74,28 @@ class ConsoleSocket(asyncore.dispatcher): =20 def handle_read(self): """process arriving characters into in memory _buffer""" - try: - data =3D asyncore.dispatcher.recv(self, 1) - # latin1 is needed since there are some chars - # we are receiving that cannot be encoded to utf-8 - # such as 0xe2, 0x80, 0xA6. - string =3D data.decode("latin1") - except: - print("Exception seen.") - traceback.print_exc() - return + data =3D asyncore.dispatcher.recv(self, 1) + # latin1 is needed since there are some chars + # we are receiving that cannot be encoded to utf-8 + # such as 0xe2, 0x80, 0xA6. + string =3D data.decode("latin1") if self._logfile: self._logfile.write("{}".format(string)) self._logfile.flush() for c in string: self._buffer.extend(c) =20 - def recv(self, n=3D1, sleep_delay_s=3D0.1): - """Return chars from in memory buffer""" + def recv(self, buffer_size=3D1): + """Return chars from in memory buffer. + Maintains the same API as socket.socket.recv. + """ start_time =3D time.time() - while len(self._buffer) < n: - time.sleep(sleep_delay_s) + while len(self._buffer) < buffer_size: + time.sleep(self._sleep_time) elapsed_sec =3D time.time() - start_time if elapsed_sec > self._recv_timeout_sec: raise socket.timeout - chars =3D ''.join([self._buffer.popleft() for i in range(n)]) + chars =3D ''.join([self._buffer.popleft() for i in range(buffer_si= ze)]) # We choose to use latin1 to remain consistent with # handle_read() and give back the same data as the user would # receive if they were reading directly from the diff --git a/python/qemu/machine.py b/python/qemu/machine.py index 80c4d4a8b6..9956360a79 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -27,7 +27,7 @@ import socket import tempfile from typing import Optional, Type from types import TracebackType -from qemu.console_socket import ConsoleSocket +from . import console_socket =20 from . import qmp =20 @@ -674,8 +674,9 @@ class QEMUMachine: """ if self._console_socket is None: if self._drain_console: - self._console_socket =3D ConsoleSocket(self._console_addre= ss, - file=3Dself._console_l= og_path) + self._console_socket =3D console_socket.ConsoleSocket( + self._console_address, + file=3Dself._console_log_path) else: self._console_socket =3D socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) diff --git a/python/qemu/pylintrc b/python/qemu/pylintrc index 5d6ae7367d..3f69205000 100644 --- a/python/qemu/pylintrc +++ b/python/qemu/pylintrc @@ -33,7 +33,7 @@ good-names=3Di, Run, _, fd, - + c, [VARIABLES] =20 [STRING] --=20 2.17.1