Skip to content

Commit 99de603

Browse files
authored
Merge pull request #4 from regro/api
feat: generalize args so can use as api
2 parents fe9a374 + ebe23d2 commit 99de603

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

conda_forge_feedstock_ops/container_utils.py

+17-18
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ def get_default_container_name():
3333
class ContainerRuntimeError(RuntimeError):
3434
"""An error raised when running a container fails."""
3535

36-
def __init__(self, *, error, name, cmd, returncode, traceback=None):
37-
self.name = name
36+
def __init__(self, *, error, args, cmd, returncode, traceback=None):
37+
self.args = args
3838
self.cmd = cmd
3939
self.returncode = returncode
4040
self.traceback = traceback
@@ -87,8 +87,16 @@ def get_default_container_run_args(
8787
)
8888

8989

90+
def get_default_log_level_args(logger):
91+
log_level_str = str(logging.getLevelName(logger.getEffectiveLevel())).lower()
92+
logger.debug("computed effective logging level: %s", log_level_str)
93+
return [
94+
"--log-level",
95+
log_level_str,
96+
]
97+
98+
9099
def run_container_operation(
91-
name: str,
92100
args: Iterable[str],
93101
json_loads: Callable = json.loads,
94102
tmpfs_size_mb: int = DEFAULT_CONTAINER_TMPFS_SIZE_MB,
@@ -100,8 +108,6 @@ def run_container_operation(
100108
101109
Parameters
102110
----------
103-
name
104-
The name of the operation.
105111
args
106112
The arguments to pass to the container.
107113
json_loads
@@ -131,18 +137,11 @@ def run_container_operation(
131137
else:
132138
mnt_args = []
133139

134-
log_level_str = str(logging.getLevelName(logger.getEffectiveLevel())).lower()
135-
logger.debug("computed effective logging level: %s", log_level_str)
136-
137140
cmd = [
138141
*get_default_container_run_args(tmpfs_size_mb=tmpfs_size_mb),
139142
*mnt_args,
140143
get_default_container_name(),
141-
"conda-forge-feedstock-ops-container",
142-
name,
143144
*args,
144-
"--log-level",
145-
log_level_str,
146145
]
147146
res = subprocess.run(
148147
cmd,
@@ -153,10 +152,10 @@ def run_container_operation(
153152
# we handle this ourselves to customize the error message
154153
if res.returncode != 0:
155154
raise ContainerRuntimeError(
156-
error=f"Error running {name} in container - return code {res.returncode}:"
155+
error=f"Error running '{' '.join(args)}' in container - return code {res.returncode}:"
157156
f"\ncmd: {pprint.pformat(cmd)}"
158157
f"\noutput: {pprint.pformat(res.stdout)}",
159-
name=name,
158+
args=args,
160159
cmd=pprint.pformat(cmd),
161160
returncode=res.returncode,
162161
)
@@ -165,10 +164,10 @@ def run_container_operation(
165164
ret = json_loads(res.stdout)
166165
except json.JSONDecodeError:
167166
raise ContainerRuntimeError(
168-
error=f"Error running {name} in container - JSON could not parse stdout:"
167+
error=f"Error running '{' '.join(args)}' in container - JSON could not parse stdout:"
169168
f"\ncmd: {pprint.pformat(cmd)}"
170169
f"\noutput: {pprint.pformat(res.stdout)}",
171-
name=name,
170+
args=args,
172171
cmd=pprint.pformat(cmd),
173172
returncode=res.returncode,
174173
)
@@ -182,8 +181,8 @@ def run_container_operation(
182181
.decode("unicode_escape")
183182
)
184183
raise ContainerRuntimeError(
185-
error=f"Error running {name} in container - error {ret['error'].split('(')[0]} raised:\n{ret_str}",
186-
name=name,
184+
error=f"Error running '{' '.join(args)}' in container - error {ret['error'].split('(')[0]} raised:\n{ret_str}",
185+
args=args,
187186
cmd=pprint.pformat(cmd),
188187
returncode=res.returncode,
189188
traceback=ret["traceback"]

conda_forge_feedstock_ops/rerender.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
import time
99
from threading import Event, Thread
1010

11-
from conda_forge_feedstock_ops.container_utils import run_container_operation
11+
from conda_forge_feedstock_ops.container_utils import (
12+
get_default_log_level_args,
13+
run_container_operation,
14+
)
1215
from conda_forge_feedstock_ops.os_utils import (
1316
chmod_plus_rwX,
1417
get_user_execute_permissions,
@@ -74,7 +77,10 @@ def rerender_containerized(feedstock_dir, timeout=None):
7477
str
7578
The commit message for the rerender. If None, the rerender didn't change anything.
7679
"""
77-
args = []
80+
args = [
81+
"conda-forge-feedstock-ops-container",
82+
"rerender",
83+
] + get_default_log_level_args(logger)
7884

7985
if timeout is not None:
8086
args += ["--timeout", str(timeout)]
@@ -106,7 +112,6 @@ def rerender_containerized(feedstock_dir, timeout=None):
106112
)
107113

108114
data = run_container_operation(
109-
"rerender",
110115
args,
111116
mount_readonly=False,
112117
mount_dir=tmpdir,

0 commit comments

Comments
 (0)