|
14 | 14 | # John Admanski <jadmanski@google.com> |
15 | 15 |
|
16 | 16 | import json |
| 17 | +import logging |
17 | 18 | import os |
| 19 | +import platform |
18 | 20 | import shlex |
19 | 21 | import subprocess |
20 | 22 | import tempfile |
21 | | -import platform |
22 | | -import logging |
23 | | - |
24 | 23 | from abc import ABC, abstractmethod |
| 24 | + |
25 | 25 | from avocado.utils import astring, process |
26 | 26 | from avocado.utils.process import can_sudo |
27 | 27 |
|
@@ -138,14 +138,21 @@ class Command(Collectible): |
138 | 138 |
|
139 | 139 | def __init__( |
140 | 140 | self, cmd, timeout=-1, locale="C", sudo_commands=None, sudo_distros=None |
141 | | - ): # pylint: disable=R0913 |
| 141 | + ): # pylint: disable=R0913 |
142 | 142 | super().__init__(cmd) |
143 | 143 | self._name = self.log_path |
144 | 144 | self.cmd = cmd |
145 | 145 | self.timeout = timeout |
146 | 146 | self.locale = locale |
147 | 147 | self.sudo_commands = sudo_commands |
148 | 148 | self.sudo_distros = sudo_distros |
| 149 | + self._sysinfo_cmd = None |
| 150 | + |
| 151 | + @property |
| 152 | + def _sudo_helper(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 |
149 | 156 |
|
150 | 157 | def __repr__(self): |
151 | 158 | r = "Command(%r, %r)" |
@@ -179,9 +186,8 @@ def collect(self): |
179 | 186 |
|
180 | 187 | # Determine whether to run with sudo (do not mutate the command string) |
181 | 188 | 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._sudo_helper: |
| 190 | + sudo_flag = self._sudo_helper.use_sudo() and self._sudo_helper.is_sudo_cmd(self.cmd) |
185 | 191 | log.info("Executing Command%s: %s", " (sudo)" if sudo_flag else "", self.cmd) |
186 | 192 |
|
187 | 193 | try: |
|
0 commit comments