Skip to content

Commit

Permalink
Move async def execute from pack to executors
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre.delaunay committed Nov 28, 2023
1 parent d381abd commit ceefb2e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 29 deletions.
33 changes: 31 additions & 2 deletions milabench/executors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down
30 changes: 4 additions & 26 deletions milabench/pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions milabench/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit ceefb2e

Please sign in to comment.