Repository: oumi-ai/oumi
File: src/oumi/utils/str_utils.py (Python)

Code before:
        >>> str_to_bool("true") # doctest: +SKIP
        True
        >>> str_to_bool("FALSE") # doctest: +SKIP
        False
        >>> str_to_bool("1") # doctest: +SKIP
        True
        >>> str_to_bool("no") # doctest: +SKIP
        False
    """
    result = try_str_to_bool(s)

    if result is None:
        raise ValueError(f"Cannot convert '{s}' to boolean.")
    return result


def compute_utf8_len(s: str) -> int:
    """Computes string length in UTF-8 bytes."""
    # This is inefficient: allocates a temporary copy of string content.
    # FIXME Can we do better?
    return len(s.encode("utf-8"))


def get_editable_install_override() -> bool:
    """Returns whether OUMI_TRY_EDITABLE_INSTALL env var is set to a truthy value."""
    s = os.environ.get("OUMI_TRY_EDITABLE_INSTALL", "")
    mode = s.lower().strip()
    bool_result = try_str_to_bool(mode)
    if bool_result is not None:
        return bool_result
    return False


# Experimental function, only for developer usage.
def set_oumi_install_editable(setup: str) -> str:
    """Tries to replace oumi PyPi installs with editable installation from source.

    For example, the following line:
        `pip install uv && uv pip -q install oumi[gpu,dev] vllm`
    will be replaced with:
        `pip install uv && uv pip -q install -e '.[gpu,dev]' vllm`

    Args:
        setup (str): The bash setup script to modify. May be multi-line.

    Returns:
        The modified setup script.
    """
    setup_lines = setup.split("\n")
    for i, line in enumerate(setup_lines):
        # Skip comments.

Review comment (suggestion):
Let's add one additional check here:
This condition should only return true if the current installation of oumi is also a dev build. Specifically, you should call this method: https://github.com/oumi-ai/oumi/blob/main/src/oumi/utils/version_utils.py#L19

Code after:
        >>> str_to_bool("true") # doctest: +SKIP
        True
        >>> str_to_bool("FALSE") # doctest: +SKIP
        False
        >>> str_to_bool("1") # doctest: +SKIP
        True
        >>> str_to_bool("no") # doctest: +SKIP
        False
    """
    result = try_str_to_bool(s)

    if result is None:
        raise ValueError(f"Cannot convert '{s}' to boolean.")
    return result


def compute_utf8_len(s: str) -> int:
    """Computes string length in UTF-8 bytes."""
    # This is inefficient: allocates a temporary copy of string content.
    # FIXME Can we do better?
    return len(s.encode("utf-8"))


def get_editable_install_override_env_var() -> bool:
    """Returns whether OUMI_FORCE_EDITABLE_INSTALL env var is set to a truthy value."""
    s = os.environ.get("OUMI_FORCE_EDITABLE_INSTALL", "")
    mode = s.lower().strip()
    bool_result = try_str_to_bool(mode)
    if bool_result is not None:
        return bool_result
    return False


# Experimental function, only for developer usage.
def set_oumi_install_editable(setup: str) -> str:
    """Tries to replace oumi PyPi installs with editable installation from source.

    For example, the following line:
        `pip install uv && uv pip -q install oumi[gpu,dev] vllm`
    will be replaced with:
        `pip install uv && uv pip -q install -e '.[gpu,dev]' vllm`

    Args:
        setup (str): The bash setup script to modify. May be multi-line.

    Returns:
        The modified setup script.
    """
    setup_lines = setup.split("\n")
    for i, line in enumerate(setup_lines):
        # Skip comments.