From 65c897e7dbde91d9915436ab41c4ad3347401e48 Mon Sep 17 00:00:00 2001 From: Fei Su Date: Wed, 10 Jan 2024 09:13:57 +0000 Subject: [PATCH 1/2] format with black Signed-off-by: Fei Su --- scripts/host-display | 47 ++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/scripts/host-display b/scripts/host-display index 386e8d27c61..cb7eb44f527 100755 --- a/scripts/host-display +++ b/scripts/host-display @@ -8,26 +8,23 @@ n.b. the value returned by the "status" command will be correct as of the next boot (assuming that the boot config is not modified further), and is not necessarily correct at the time this script is called. """ -from __future__ import print_function import os -import os.path import re -import subprocess import sys import syslog from xcp.bootloader import Bootloader -COMMAND_NAME = sys.argv[0] -SHORT_COMMAND_NAME = os.path.basename(COMMAND_NAME) +SHORT_COMMAND_NAME = Path(__file__).name + +XE_SERIAL = "xe-serial" +XEN_CMDLINE = "/opt/xensource/libexec/xen-cmdline" -XE_SERIAL = 'xe-serial' -XEN_CMDLINE = '/opt/xensource/libexec/xen-cmdline' def send_to_syslog(msg): - """ Send a message to syslog. """ + """Send a message to syslog.""" pid = os.getpid() - syslog.syslog("%s[%d] - %s" %(SHORT_COMMAND_NAME, pid, msg)) + syslog.syslog("%s[%d] - %s" % (SHORT_COMMAND_NAME, pid, msg)) def doexec(args): """Execute a subprocess, then return its return code, stdout and stderr""" @@ -46,8 +43,8 @@ def doexec(args): def disable(bootloader): """ - Configure the host not to output its console to the physical display on - next boot. + Configure the host not to output its console to the physical display on + next boot. """ if bootloader.default == XE_SERIAL: doexec([XEN_CMDLINE, "--set-xen", "console=%s" % (_get_com_port())]) @@ -55,10 +52,11 @@ def disable(bootloader): doexec([XEN_CMDLINE, "--delete-dom0", "console=tty0"]) doexec([XEN_CMDLINE, "--delete-xen", "console"]) + def enable(bootloader): """ - Configure the host to output its console to the physical display on - next boot. + Configure the host to output its console to the physical display on + next boot. """ if bootloader.default == XE_SERIAL: doexec([XEN_CMDLINE, "--set-xen", "console=%s,vga" % (_get_com_port())]) @@ -66,10 +64,11 @@ def enable(bootloader): doexec([XEN_CMDLINE, "--set-xen", "console=vga"]) doexec([XEN_CMDLINE, "--set-dom0", "console=hvc0 console=tty0"]) + def status(_): """ - Determine from the bootloader config whether the host is currently - configured to output its console to the physical display. + Determine from the bootloader config whether the host is currently + configured to output its console to the physical display. """ (_, stdout, _) = doexec([XEN_CMDLINE, "--get-xen", "console"]) if "vga" in stdout.readline().strip(): @@ -85,7 +84,7 @@ COMMANDS = { def _get_com_port(): """ - Determine from the current bootloader config which COM port is in use + Determine from the current bootloader config which COM port is in use """ (_, stdout, _) = doexec([XEN_CMDLINE, "--get-xen", "console"]) m = re.match("console=.*(com\d).*", stdout.readline().strip()) @@ -94,16 +93,17 @@ def _get_com_port(): else: return "com1" + def usage(): - """ Display a usage string. """ - command_names = list(COMMANDS.keys()) - command_names.sort() + """Display a usage string.""" + command_names = sorted(COMMANDS.keys()) print("Usage:") - print(" %s {%s}" % (sys.argv[0], '|'.join(command_names))) + print(" %s {%s}" % (sys.argv[0], "|".join(command_names))) + def main(): - """ Program entry point. """ - if len(sys.argv) == 2 and sys.argv[1] in COMMANDS.keys(): + """Program entry point.""" + if len(sys.argv) == 2 and sys.argv[1] in COMMANDS: bootloader = Bootloader.loadExisting() command = COMMANDS[sys.argv[1]] command(bootloader) @@ -111,5 +111,6 @@ def main(): usage() sys.exit(1) -if __name__ == '__main__': + +if __name__ == "__main__": main() From 8e09d90a276bff2e436ccbac6194979dd22762bf Mon Sep 17 00:00:00 2001 From: Fei Su Date: Wed, 10 Jan 2024 09:14:07 +0000 Subject: [PATCH 2/2] refactory doexec with xcp.cmd Signed-off-by: Fei Su --- scripts/host-display | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/scripts/host-display b/scripts/host-display index cb7eb44f527..0a70775e75a 100755 --- a/scripts/host-display +++ b/scripts/host-display @@ -13,7 +13,10 @@ import os import re import sys import syslog +from pathlib import Path + from xcp.bootloader import Bootloader +from xcp.cmd import runCmd SHORT_COMMAND_NAME = Path(__file__).name @@ -26,20 +29,10 @@ def send_to_syslog(msg): pid = os.getpid() syslog.syslog("%s[%d] - %s" % (SHORT_COMMAND_NAME, pid, msg)) -def doexec(args): - """Execute a subprocess, then return its return code, stdout and stderr""" + +def doexec(args, with_stdout=False): send_to_syslog(args) - proc = subprocess.Popen( - args, - stdin=None, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - close_fds=True, - universal_newlines=True) - rc = proc.wait() - stdout = proc.stdout - stderr = proc.stderr - return (rc, stdout, stderr) + return runCmd(args, with_stdout) def disable(bootloader): """ @@ -70,8 +63,8 @@ def status(_): Determine from the bootloader config whether the host is currently configured to output its console to the physical display. """ - (_, stdout, _) = doexec([XEN_CMDLINE, "--get-xen", "console"]) - if "vga" in stdout.readline().strip(): + (_, stdout) = doexec([XEN_CMDLINE, "--get-xen", "console"], with_stdout=True) + if "vga" in stdout: sys.stdout.write("enabled") else: sys.stdout.write("disabled") @@ -86,8 +79,8 @@ def _get_com_port(): """ Determine from the current bootloader config which COM port is in use """ - (_, stdout, _) = doexec([XEN_CMDLINE, "--get-xen", "console"]) - m = re.match("console=.*(com\d).*", stdout.readline().strip()) + (_, stdout) = doexec([XEN_CMDLINE, "--get-xen", "console"], with_stdout=True) + m = re.match(r"console=.*(com\d).*", stdout.strip()) if m: return m.group(1) else: