Skip to content

Commit a8ef211

Browse files
authored
Merge pull request #6 from regro/in-container-util
feat: add util for using a container and tests
2 parents 99de603 + 3c2f5a0 commit a8ef211

File tree

4 files changed

+59
-7
lines changed

4 files changed

+59
-7
lines changed

conda_forge_feedstock_ops/container_utils.py

+28
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,31 @@ def run_container_operation(
191191
)
192192

193193
return ret["data"]
194+
195+
196+
def should_use_container(use_container: Optional[bool] = None):
197+
"""Determine if we should use a container.
198+
199+
Parameters
200+
----------
201+
use_container
202+
Whether to use a container to run the rerender.
203+
If None, the function will use a container if the environment
204+
variable `CF_FEEDSTOCK_OPS_IN_CONTAINER` is 'false'. This feature can be
205+
used to avoid container in container calls.
206+
207+
Returns
208+
-------
209+
bool
210+
Whether to use a container.
211+
"""
212+
in_container = (
213+
os.environ.get("CF_FEEDSTOCK_OPS_IN_CONTAINER", "false").lower() == "true"
214+
)
215+
if use_container is None:
216+
use_container = not in_container
217+
218+
if use_container and not in_container:
219+
return True
220+
else:
221+
return False

conda_forge_feedstock_ops/os_utils.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ def override_env(name, value):
2626
"""Override an environment variable temporarily."""
2727
old = os.environ.get(name)
2828
try:
29-
os.environ[name] = value
29+
if value is None:
30+
del os.environ[name]
31+
else:
32+
os.environ[name] = value
3033
yield
3134
finally:
3235
if old is None:

conda_forge_feedstock_ops/rerender.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from conda_forge_feedstock_ops.container_utils import (
1212
get_default_log_level_args,
1313
run_container_operation,
14+
should_use_container,
1415
)
1516
from conda_forge_feedstock_ops.os_utils import (
1617
chmod_plus_rwX,
@@ -43,12 +44,7 @@ def rerender(feedstock_dir, timeout=None, use_container=None):
4344
str
4445
The commit message for the rerender. If None, the rerender didn't change anything.
4546
"""
46-
47-
in_container = os.environ.get("CF_FEEDSTOCK_OPS_IN_CONTAINER", "false") == "true"
48-
if use_container is None:
49-
use_container = not in_container
50-
51-
if use_container and not in_container:
47+
if should_use_container(use_container=use_container):
5248
return rerender_containerized(
5349
feedstock_dir,
5450
timeout=timeout,

tests/test_container_utils.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from conda_forge_feedstock_ops.container_utils import should_use_container
2+
from conda_forge_feedstock_ops.os_utils import override_env
3+
4+
5+
def test_should_use_container():
6+
with override_env("CF_FEEDSTOCK_OPS_IN_CONTAINER", "tRue"):
7+
assert not should_use_container()
8+
9+
with override_env("CF_FEEDSTOCK_OPS_IN_CONTAINER", "false"):
10+
assert should_use_container()
11+
12+
with override_env("CF_FEEDSTOCK_OPS_IN_CONTAINER", None):
13+
assert should_use_container()
14+
15+
with override_env("CF_FEEDSTOCK_OPS_IN_CONTAINER", "true"):
16+
assert not should_use_container(use_container=True)
17+
18+
with override_env("CF_FEEDSTOCK_OPS_IN_CONTAINER", "false"):
19+
assert should_use_container(use_container=True)
20+
21+
with override_env("CF_FEEDSTOCK_OPS_IN_CONTAINER", "true"):
22+
assert not should_use_container(use_container=False)
23+
24+
with override_env("CF_FEEDSTOCK_OPS_IN_CONTAINER", "false"):
25+
assert not should_use_container(use_container=False)

0 commit comments

Comments
 (0)