Files
python-utils/python_utils/check_env.py
T
Brian Bjarke Jensen 95a8a29e2f
Code Quality Pipeline / code-quality (push) Failing after 2m6s
Test Python Package / test (push) Failing after 15s
Type Check / type-check (push) Failing after 14s
initial commit
2025-08-27 22:42:09 +02:00

34 lines
976 B
Python

"""Definition of check_env function."""
import logging
import os
def check_env(
var_list: str | list[str],
missing_ok: bool = False,
) -> None:
"""Check if environment variables are set.
Args:
var_list (str | list[str]): A single environment variable name or a list of names to check.
missing_ok (bool): If True, do not raise an error if the variable is not set. Defaults to False.
Raises:
OSError: If any environment variable is not set.
"""
if not var_list:
raise ValueError("List of environment variable names is empty.")
# Handle single string input
if isinstance(var_list, str):
var_list = [var_list]
# Avoid double-checking
var_set = set(var_list)
# Perform checks
for var in var_set:
if not os.getenv(var):
msg = f"Environment variable {var} is not set."
logging.error(msg)
if not missing_ok:
raise OSError(msg)