From ee5f697065eb1e50ea87bb1a6423b7533c03170a Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Thu, 7 Jul 2022 09:49:34 -0400 Subject: T2719: rework 'show hardware cpu *' commands in the new style --- src/op_mode/cpu.py | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100755 src/op_mode/cpu.py (limited to 'src/op_mode/cpu.py') diff --git a/src/op_mode/cpu.py b/src/op_mode/cpu.py new file mode 100755 index 000000000..cb55ce407 --- /dev/null +++ b/src/op_mode/cpu.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2016-2022 VyOS maintainers and contributors +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 or later as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import json +import sys +import typing + +import vyos.cpu +import vyos.opmode + +from jinja2 import Template +from vyos.util import popen, DEVNULL + +cpu_template = Template(""" +{% for cpu in cpus %} +{% if 'physical id' in cpu %}CPU socket: {{cpu['physical id']}}{% endif %} +{% if 'vendor_id' in cpu %}CPU Vendor: {{cpu['vendor_id']}}{% endif %} +{% if 'model name' in cpu %}Model: {{cpu['model name']}}{% endif %} +{% if 'cpu cores' in cpu %}Cores: {{cpu['cpu cores']}}{% endif %} +{% if 'cpu MHz' in cpu %}Current MHz: {{cpu['cpu MHz']}}{% endif %} +{% endfor %} +""") + +cpu_summary_template = Template(""" +Physical CPU cores: {{count}} +CPU model(s): {{models | join(", ")}} +""") + +def _get_raw_data(): + return vyos.cpu.get_cpus() + +def _format_cpus(cpu_data): + env = {'cpus': cpu_data} + return cpu_template.render(env).strip() + +def _get_summary_data(): + count = vyos.cpu.get_core_count() + cpu_data = vyos.cpu.get_cpus() + models = [c['model name'] for c in cpu_data] + env = {'count': count, "models": models} + + return env + +def _format_cpu_summary(summary_data): + return cpu_summary_template.render(summary_data).strip() + +def show(raw: bool): + cpu_data = _get_raw_data() + + if raw: + return cpu_data + else: + return _format_cpus(cpu_data) + +def show_summary(raw: bool): + cpu_summary_data = _get_summary_data() + + if raw: + return cpu_summary_data + else: + return _format_cpu_summary(cpu_summary_data) + + +if __name__ == '__main__': + try: + res = vyos.opmode.run(sys.modules[__name__]) + if res: + print(res) + except ValueError as e: + print(e) + sys.exit(1) + -- cgit v1.2.3 From f9e835c41643f8d41f675c0364686fbea5055896 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Wed, 20 Jul 2022 12:58:41 -0400 Subject: T2719: fix unused imports --- src/op_mode/cpu.py | 3 --- src/op_mode/version.py | 2 -- 2 files changed, 5 deletions(-) (limited to 'src/op_mode/cpu.py') diff --git a/src/op_mode/cpu.py b/src/op_mode/cpu.py index cb55ce407..f9c425826 100755 --- a/src/op_mode/cpu.py +++ b/src/op_mode/cpu.py @@ -14,15 +14,12 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import json import sys -import typing import vyos.cpu import vyos.opmode from jinja2 import Template -from vyos.util import popen, DEVNULL cpu_template = Template(""" {% for cpu in cpus %} diff --git a/src/op_mode/version.py b/src/op_mode/version.py index 847bb197e..06208c3e5 100755 --- a/src/op_mode/version.py +++ b/src/op_mode/version.py @@ -18,7 +18,6 @@ # Displays image version and system information. # Used by the "run show version" command. -import argparse import sys import typing @@ -27,7 +26,6 @@ import vyos.version import vyos.limericks from jinja2 import Template -from vyos.util import call version_output_tmpl = """ Version: VyOS {{version}} -- cgit v1.2.3