Skip to content

Commit 010a305

Browse files
author
xianglongfei
committed
Merge branch 'xlf_dev' of https://github.com/xianglongfei-8888/avocado into xlf_dev
2 parents 65dd0ba + d5717d9 commit 010a305

File tree

2 files changed

+10
-45
lines changed

2 files changed

+10
-45
lines changed

avocado/core/sysinfo.py

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def __init__(self, basedir=None, log_packages=None, profiler=None):
8383
else:
8484
log.debug("sudo_distros config is empty or missing")
8585

86-
def _load_sudo_list(raw_value, key):
86+
def _load_sudo_list(raw_value, key):
8787
# pylint: disable=wrong-spelling-in-docstring
8888
"""
8989
If `raw_value` is a path to an INI file, read `[sysinfo] / key`
@@ -110,47 +110,6 @@ def _load_sudo_list(raw_value, key):
110110
dst.strip().lower() for dst in sudo_distros_value.split(",") if dst.strip()
111111
}
112112

113-
# Retrieve the configured paths for sudo commands and distros from the settings dictionary
114-
sudo_commands_conf = self.config.get("sysinfo.sudo_commands", "")
115-
sudo_distros_conf = self.config.get("sysinfo.sudo_distros", "")
116-
117-
if sudo_commands_conf:
118-
log.info("sudo_commands loaded from config: %s", sudo_commands_conf)
119-
else:
120-
log.debug("sudo_commands config is empty or missing")
121-
122-
if sudo_distros_conf:
123-
log.info("sudo_distros loaded from config: %s", sudo_distros_conf)
124-
else:
125-
log.debug("sudo_distros config is empty or missing")
126-
127-
def _load_sudo_list(raw_value, key):
128-
# pylint: disable=wrong-spelling-in-docstring
129-
"""
130-
If `raw_value` is a path to an INI file, read `[sysinfo] / key`
131-
from it; otherwise, treat `raw_value` itself as a CSV list.
132-
"""
133-
if not raw_value:
134-
return ""
135-
if os.path.isfile(raw_value):
136-
config = configparser.ConfigParser()
137-
config.read(raw_value)
138-
return config.get("sysinfo", key, fallback="")
139-
return raw_value
140-
141-
# Retrieve the actual sudo commands and distros values from the config files,
142-
# falling back to empty string if the keys are missing
143-
sudo_commands_value = _load_sudo_list(sudo_commands_conf, "sudo_commands")
144-
sudo_distros_value = _load_sudo_list(sudo_distros_conf, "sudo_distros")
145-
146-
self.sudo_commands = {
147-
cmd.strip().lower() for cmd in sudo_commands_value.split(",") if cmd.strip()
148-
}
149-
150-
self.sudo_distros = {
151-
dst.strip().lower() for dst in sudo_distros_value.split(",") if dst.strip()
152-
}
153-
154113
if basedir is None:
155114
basedir = utils_path.init_dir("sysinfo")
156115
self.basedir = basedir

avocado/utils/sysinfo.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ def __init__(
146146
self.locale = locale
147147
self.sudo_commands = sudo_commands
148148
self.sudo_distros = sudo_distros
149+
self._sysinfo_cmd = None
150+
151+
@property
152+
def _sudoer(self):
153+
if self._sysinfo_cmd is None and self.sudo_commands and self.sudo_distros:
154+
self._sysinfo_cmd = SysinfoCommand(self.sudo_commands, self.sudo_distros)
155+
return self._sysinfo_cmd
149156

150157
def __repr__(self):
151158
r = "Command(%r, %r)"
@@ -179,9 +186,8 @@ def collect(self):
179186

180187
# Determine whether to run with sudo (do not mutate the command string)
181188
sudo_flag = False
182-
if self.sudo_commands and self.sudo_distros:
183-
sysinfo_cmd = SysinfoCommand(self.sudo_commands, self.sudo_distros)
184-
sudo_flag = sysinfo_cmd.use_sudo() and sysinfo_cmd.is_sudo_cmd(self.cmd)
189+
if self._sudoer:
190+
sudo_flag = self._sudoer.use_sudo() and self._sudoer.is_sudo_cmd(self.cmd)
185191
log.info("Executing Command%s: %s", " (sudo)" if sudo_flag else "", self.cmd)
186192

187193
try:

0 commit comments

Comments
 (0)