34 lines
1005 B
Python
34 lines
1005 B
Python
"""Definition of check_env function."""
|
|
|
|
import logging
|
|
import os
|
|
|
|
|
|
def check_env(
|
|
var_list: str | set[str] | list[str] | tuple[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)
|