Skip to content

Commit

Permalink
Move env util (#3499)
Browse files Browse the repository at this point in the history
Summary: Pull Request resolved: #3499

Differential Revision: D47803654

Pulled By: mthrok

fbshipit-source-id: 2b916fa66d84c91c01b4dfe6dd5ee3501159f451
  • Loading branch information
mthrok authored and facebook-github-bot committed Jul 26, 2023
1 parent f082e6c commit da21202
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
26 changes: 4 additions & 22 deletions test/torchaudio_unittest/common_utils/case_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import torch
import torchaudio
from torch.testing._internal.common_utils import TestCase as PytorchTestCase
from torchaudio._internal.module_utils import is_module_available
from torchaudio._internal.module_utils import eval_env, is_module_available
from torchaudio.utils.ffmpeg_utils import get_video_decoders, get_video_encoders

from .backend_utils import set_audio_backend
Expand Down Expand Up @@ -143,24 +143,6 @@ def is_cuda_ctc_decoder_available():
return _IS_CUDA_CTC_DECODER_AVAILABLE


def _eval_env(var, default):
if var not in os.environ:
return default

val = os.environ.get(var, "0")
trues = ["1", "true", "TRUE", "on", "ON", "yes", "YES"]
falses = ["0", "false", "FALSE", "off", "OFF", "no", "NO"]
if val in trues:
return True
if val not in falses:
# fmt: off
raise RuntimeError(
f"Unexpected environment variable value `{var}={val}`. "
f"Expected one of {trues + falses}")
# fmt: on
return False


def _fail(reason):
def deco(test_item):
if isinstance(test_item, type):
Expand All @@ -185,7 +167,7 @@ def _pass(test_item):
return test_item


_IN_CI = _eval_env("CI", default=False)
_IN_CI = eval_env("CI", default=False)


def _skipIf(condition, reason, key):
Expand All @@ -195,7 +177,7 @@ def _skipIf(condition, reason, key):
# In CI, default to fail, so as to prevent accidental skip.
# In other env, default to skip
var = f"TORCHAUDIO_TEST_ALLOW_SKIP_IF_{key}"
skip_allowed = _eval_env(var, default=not _IN_CI)
skip_allowed = eval_env(var, default=not _IN_CI)
if skip_allowed:
return unittest.skip(reason)
return _fail(f"{reason} But the test cannot be skipped. (CI={_IN_CI}, {var}={skip_allowed}.)")
Expand Down Expand Up @@ -268,7 +250,7 @@ def skipIfNoSoxEncoder(ext):
key="NO_CUCTC_DECODER",
)
skipIfRocm = _skipIf(
_eval_env("TORCHAUDIO_TEST_WITH_ROCM", default=False),
eval_env("TORCHAUDIO_TEST_WITH_ROCM", default=False),
reason="The test doesn't currently work on the ROCm stack.",
key="ON_ROCM",
)
Expand Down
20 changes: 20 additions & 0 deletions torchaudio/_internal/module_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
import importlib.util
import os
import warnings
from functools import wraps
from typing import Optional


def eval_env(var, default):
"""Check if environment varable has True-y value"""
if var not in os.environ:
return default

val = os.environ.get(var, "0")
trues = ["1", "true", "TRUE", "on", "ON", "yes", "YES"]
falses = ["0", "false", "FALSE", "off", "OFF", "no", "NO"]
if val in trues:
return True
if val not in falses:
# fmt: off
raise RuntimeError(
f"Unexpected environment variable value `{var}={val}`. "
f"Expected one of {trues + falses}")
# fmt: on
return False


def is_module_available(*modules: str) -> bool:
r"""Returns if a top-level module with :attr:`name` exists *without**
importing it. This is generally safer than try-catch block around a
Expand Down

0 comments on commit da21202

Please sign in to comment.