Skip to content

Commit b095ccd

Browse files
jwieleRHgemini-code-assist[bot]sourcery-ai[bot]
committed
Apply suggestions from code review
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com> Signed-off-by: John Wiele <[email protected]>
1 parent 767c009 commit b095ccd

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

ramalama/common.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def engine_version(engine: SUPPORTED_ENGINES) -> str:
228228
return run_cmd(cmd_args).stdout.decode("utf-8").strip()
229229

230230

231-
def load_cdi_config(spec_dirs: List[str]) -> dict:
231+
def load_cdi_config(spec_dirs: List[str]) -> dict | None:
232232
"""Load the first YAML or JSON CDI configuration file found in the given directories."""
233233
for spec_dir in spec_dirs:
234234
for root, _, files in os.walk(spec_dir):
@@ -240,7 +240,7 @@ def load_cdi_config(spec_dirs: List[str]) -> dict:
240240
with open(file_path, "r") as stream:
241241
config = yaml.safe_load(stream)
242242
return config
243-
except Exception:
243+
except (yaml.YAMLError, OSError):
244244
continue
245245
elif ext == ".json":
246246
try:
@@ -254,12 +254,12 @@ def load_cdi_config(spec_dirs: List[str]) -> dict:
254254
return None
255255

256256

257-
def find_in_cdi(devices: List[str]) -> (List[str], List[str]):
257+
def find_in_cdi(devices: List[str]) -> tuple[List[str], List[str]]:
258258
# Attempt to find CDI configuration for each device in devices and
259259
# return lists of configured and unconfigured devices.
260260
cdi = load_cdi_config(['/etc/cdi', '/var/run/cdi'])
261-
cdi_devices = cdi["devices"] if cdi else []
262-
cdi_device_names = [cdi_device["name"] for cdi_device in cdi_devices]
261+
cdi_devices = cdi.get("devices", []) if cdi else []
262+
cdi_device_names = [name for cdi_device in cdi_devices if (name := cdi_device.get("name"))]
263263

264264
logger.debug(f"cdi_device_names: {','.join(cdi_device_names)}")
265265

@@ -301,7 +301,7 @@ def check_metal(args: ContainerArgType) -> bool:
301301

302302

303303
@lru_cache(maxsize=1)
304-
def check_nvidia() -> Literal["cuda"] | None:
304+
def check_nvidia() -> Literal["cuda", "all"] | None:
305305
try:
306306
command = ['nvidia-smi', '--query-gpu=index,uuid', '--format=csv,noheader']
307307
result = run_cmd(command, encoding="utf-8")
@@ -311,9 +311,10 @@ def check_nvidia() -> Literal["cuda"] | None:
311311
return "all"
312312

313313
smi_lines = result.stdout.splitlines()
314-
indices, uuids = zip(*[[item.strip() for item in line.split(',')] for line in smi_lines if line])
314+
parsed_lines = [[item.strip() for item in line.split(',')] for line in smi_lines if line]
315+
indices, uuids = zip(*parsed_lines) if parsed_lines else (tuple(), tuple())
315316
# Get the list of devices specified by CUDA_VISIBLE_DEVICES, if any
316-
cuda_visible_devices = os.environ["CUDA_VISIBLE_DEVICES"] if "CUDA_VISIBLE_DEVICES" in os.environ else "all"
317+
cuda_visible_devices = os.environ.get("CUDA_VISIBLE_DEVICES", "all")
317318
visible_devices = list(cuda_visible_devices.split(',') if cuda_visible_devices else uuids)
318319

319320
logger.debug(f"visible devices {','.join(visible_devices)}")

0 commit comments

Comments
 (0)