bios.py 3.1 KB
Newer Older
D
dogsheng 已提交
1 2 3
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Copyright (c) 2019 Huawei Technologies Co., Ltd.
4 5 6
# A-Tune is licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
7
#     http://license.coscl.org.cn/MulanPSL2
D
dogsheng 已提交
8 9 10
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
11
# See the Mulan PSL v2 for more details.
D
dogsheng 已提交
12 13 14 15 16
# Create: 2019-10-29

"""
The sub class of the Configurator, used to change the bios config.
"""
Z
Zhipeng Xie 已提交
17
import inspect
D
dogsheng 已提交
18 19 20
import logging
import subprocess

Z
Zhipeng Xie 已提交
21 22
from analysis.plugin.public import NeedConfigWarning
from ..common import Configurator
D
dogsheng 已提交
23

Z
Zhipeng Xie 已提交
24
LOGGER = logging.getLogger(__name__)
D
dogsheng 已提交
25 26 27 28 29 30 31 32 33 34 35 36


class Bios(Configurator):
    """To change the bios config"""
    _module = "BIOS"
    _submod = "BIOS"

    def __init__(self, user=None):
        Configurator.__init__(self, user)

    def _set(self, key, value):
        raise NeedConfigWarning(
37
            "please change the BIOS configuration {key} to {val}".format(
D
dogsheng 已提交
38 39
                key=key, val=value))

40
    def _get(self, key, _):
D
dogsheng 已提交
41
        if key.lower() == "version":
Z
Zhipeng Xie 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
            output_dmi = subprocess.Popen(["dmidecode", "-t", "bios"], stdout=subprocess.PIPE,
                                          shell=False)
            output_grep = subprocess.Popen(["grep", "-Po", "(?<=Version:)(.*)"],
                                           stdin=output_dmi.stdout,
                                           stdout=subprocess.PIPE,
                                           shell=False)
            output_sed = subprocess.Popen(["sed", "s/^ *//g"],
                                          stdin=output_grep.stdout,
                                          stdout=subprocess.PIPE,
                                          shell=False)
            output = subprocess.Popen(["sed", "s/ *$//g"],
                                      stdin=output_sed.stdout,
                                      stdout=subprocess.PIPE,
                                      shell=False)

            out = output.communicate()
            return bytes.decode(out[0]).strip()
        if key.lower() == "hpre_support":
            output_lspci = subprocess.Popen(["lspci"],
                                            stdout=subprocess.PIPE,
                                            shell=False)
            output = subprocess.Popen(["grep", "HPRE"],
                                      stdin=output_lspci.stdout,
                                      stdout=subprocess.PIPE,
                                      shell=False)
            out = output.communicate()
            if bytes.decode(out[0]) != "":
D
dogsheng 已提交
69
                return "yes"
Z
Zhipeng Xie 已提交
70 71 72 73 74 75 76
            return "no"
        err = NotImplementedError(
            "{} can not get {}".format(
                self._module, key))
        LOGGER.error("%s.%s: %s", self.__class__.__name__,
                     inspect.stack()[0][3], str(err))
        raise err
D
dogsheng 已提交
77

Z
Zhipeng Xie 已提交
78
    def _backup(self, _, __):
D
dogsheng 已提交
79 80
        return ""

Z
Zhipeng Xie 已提交
81
    def _resume(self, _, __):
D
dogsheng 已提交
82
        return None