diff --git a/milabench/executors.py b/milabench/executors.py index 8e1fb4ff7..1b991e23f 100644 --- a/milabench/executors.py +++ b/milabench/executors.py @@ -11,10 +11,11 @@ from voir.instruments.gpu import get_gpu_info from . import pack -from .alt_async import destroy +from .alt_async import destroy, run from .fs import XPath from .merge import merge from .metadata import machine_metadata +from .structs import BenchLogEntry from .utils import select_nodes @@ -33,6 +34,34 @@ async def force_terminate(pack, delay): destroy(proc) +async def execute(pack, *args, cwd=None, env={}, external=False, **kwargs): + """Run a command in the virtual environment. + + Unless specified otherwise, the command is run with + ``self.dirs.code`` as the cwd. + + Arguments: + args: The arguments to the command + cwd: The cwd to use (defaults to ``self.dirs.code``) + """ + args = [str(x) for x in args] + if cwd is None: + cwd = pack.dirs.code + + exec_env = pack.full_env(env) if not external else {**os.environ, **env} + + return await run( + args, + **kwargs, + info={"pack": pack}, + env=exec_env, + constructor=BenchLogEntry, + cwd=cwd, + process_accumulator=pack.processes, + ) + + +# TODO Move this to command class Executor: """Base class for an execution plan @@ -128,7 +157,7 @@ async def execute(self, phase="run", timeout=False, timeout_delay=600, **kwargs) await pack.send(event="config", data=pack.config) await pack.send(event="meta", data=machine_metadata(pack)) - fut = pack.execute(*argv, **{**_kwargs, **kwargs}) + fut = execute(pack, *argv, **{**_kwargs, **kwargs}) coro.append(fut) if timeout: diff --git a/milabench/pack.py b/milabench/pack.py index 25d1bf27e..ec7a8dac3 100644 --- a/milabench/pack.py +++ b/milabench/pack.py @@ -19,7 +19,7 @@ class defines good default behavior. from .fs import XPath from .merge import merge from .structs import BenchLogEntry -from .utils import assemble_options, make_constraints_file, relativize +from .utils import assemble_options, make_constraints_file, relativize, deprecated class PackageCore: @@ -193,17 +193,7 @@ def conda_install(self, *args, **kwargs): args = [str(x) for x in args] return self._nox_session.conda_install(*args, **kwargs, silent=False) - def execute_sync(self, *args, env=dict(), cwd=None): - args = [str(x) for x in args] - if cwd is None: - cwd = self.dirs.code - - exec_env = self.full_env(env) - - import subprocess - - return subprocess.run(args, env=exec_env, cwd=cwd, capture_output=True) - + @deprecated async def execute(self, *args, cwd=None, env={}, external=False, **kwargs): """Run a command in the virtual environment. @@ -214,21 +204,9 @@ async def execute(self, *args, cwd=None, env={}, external=False, **kwargs): args: The arguments to the command cwd: The cwd to use (defaults to ``self.dirs.code``) """ - args = [str(x) for x in args] - if cwd is None: - cwd = self.dirs.code - - exec_env = self.full_env(env) if not external else {**os.environ, **env} + from .executors import execute - return await run( - args, - **kwargs, - info={"pack": self}, - env=exec_env, - constructor=BenchLogEntry, - cwd=cwd, - process_accumulator=self.processes, - ) + return execute(pack, *args, cwd=cwd, env=env, external=external, **kwargs) async def python(self, *args, **kwargs): """Run a Python script. diff --git a/milabench/utils.py b/milabench/utils.py index d7914eb5f..cb058bec0 100644 --- a/milabench/utils.py +++ b/milabench/utils.py @@ -7,8 +7,10 @@ import importlib import pkgutil import traceback +import functools from functools import wraps from typing import Any +import warnings from ovld import ovld @@ -17,6 +19,19 @@ from milabench.validation.validation import Summary +def deprecated(func): + @functools.wraps(func) + def new_func(*args, **kwargs): + warnings.warn( + "Call to deprecated function {}.".format(func.__name__), + category=DeprecationWarning, + stacklevel=2, + ) + return func(*args, **kwargs) + + return new_func + + class Named: """A named object. This class can be used to construct objects with a name that will be used diff --git a/pyproject.toml b/pyproject.toml index f349e2dea..773986140 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -51,7 +51,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry.build] generate-setup-file = false -script = "milabench/vcs.py" +script = "milabench/scripts/vcs.py" [tool.poetry.scripts] milabench = "milabench.cli:main"