File size: 1,907 Bytes
f5f3483
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Copyright 2024 The etils Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Adhoc utils."""

from collections.abc import Iterator
import contextlib
import os

from etils.epy import py_utils


def normalize_restrict_and_reload(
    restrict: py_utils.StrOrStrList,
    reload: py_utils.StrOrStrList,
    *,
    restrict_reload: bool = True,
) -> tuple[list[str], list[str]]:
  """Normalize restrict and reload."""
  if isinstance(reload, bool):
    raise ValueError(
        f"reload={reload} is deprecated. Instead use reload='my_module'"
    )

  restrict = py_utils.normalize_str_to_list(restrict)
  reload = py_utils.normalize_str_to_list(reload)

  # Restrict also include the reload
  # This allow to call `adhoc(reload='visu3d')` without explicitly set restrict
  if restrict_reload:
    restrict = _remove_duplicate(restrict + reload)

  return restrict, reload


def _remove_duplicate(x: list[str]) -> list[str]:
  return list(dict.fromkeys(x))


@contextlib.contextmanager
def skip_disable_tf2() -> Iterator[None]:
  """Set environment variable."""
  # Allow TF to conditionally detect if they are running inside adhoc (fix
  # b/322775800)
  prev_value = os.environ.get('SKIP_DISABLE_TF2')
  try:
    os.environ['SKIP_DISABLE_TF2'] = '1'
    yield
  finally:
    if prev_value is None:
      del os.environ['SKIP_DISABLE_TF2']
    else:
      os.environ['SKIP_DISABLE_TF2'] = prev_value