Skip to content

Commit f7a1feb

Browse files
committed
Fix shell issues reported by CodeFactor
1 parent 37b42a5 commit f7a1feb

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

sklbench/utils/env.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,21 @@ def get_numa_cpus_conf() -> Dict[int, str]:
4848
def get_number_of_sockets():
4949
if sys.platform == "win32":
5050
command = "wmic cpu get DeviceID"
51-
result = subprocess.check_output(command, shell=True, text=True)
51+
result = subprocess.check_output(command, shell=False, text=True)
5252
n_sockets = len(list(filter(lambda x: x.startswith("CPU"), result.split("\n"))))
5353
elif sys.platform == "linux":
54-
command = "lscpu | grep 'Socket(s):' | awk '{print $2}'"
55-
result = subprocess.check_output(command, shell=True, text=True)
56-
n_sockets = int(result.strip("\n"))
54+
try:
55+
_, lscpu_text, _ = read_output_from_command("lscpu")
56+
for line in lscpu_text.split("\n"):
57+
if "Socket(s):" in line:
58+
n_sockets = int(line.split(":")[1].strip())
59+
break
60+
else:
61+
logger.warning("Unable to find Socket(s) information in lscpu output")
62+
n_sockets = 1
63+
except (FileNotFoundError, ValueError, IndexError):
64+
logger.warning("Unable to get number of sockets via lscpu")
65+
n_sockets = 1
5766
else:
5867
logger.warning("Unable to get number of sockets due to unknown sys.platform")
5968
n_sockets = 1

0 commit comments

Comments
 (0)