Skip to content

Commit

Permalink
Merge pull request #53 from craig8/feature/run_command
Browse files Browse the repository at this point in the history
Added run_command feature to platform wrapper
  • Loading branch information
craig8 authored Oct 29, 2024
2 parents feae1fd + b8341fc commit 9c834fb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/volttrontesting/platformwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,31 @@ def build_agent(self, identity: Identity, agent_class: AgentT = Agent, options:

return agent

def run_command(self, cmd: list, cwd: Path | str = None) -> str:
"""
Execute a shell command within the virtual environment. This will run
in the platformwrapper's context.
if cwd is not set then the cwd will be set to `self.volttron_home`
:raises CalledProcessError: If subprocess return value is not 0.
:param cmd: list passed to subprocess
:param cwd: directory to run the command in.
:return: response of the call.
"""
if cwd is None:
cwd = self.volttron_home
elif isinstance(cwd, Path):
cwd = cwd.as_posix()

try:
output = self._virtual_env.run(args=cmd, capture=True, cwd=cwd, env=self._platform_environment, text=True)
except CalledProcessError as e:
print(f"Error:\n{e.output}")
raise

return output

def install_library(self, library: str | Path, version: str = "latest"):

if isinstance(library, Path):
Expand All @@ -504,7 +529,8 @@ def install_library(self, library: str | Path, version: str = "latest"):
cmd = f"poetry add {library}@latest"

try:
output = self._virtual_env.run(args=cmd, capture=True, cwd=self.volttron_home)
output = self._virtual_env.run(args=cmd, env=self._platform_environment, capture=True,
cwd=self.volttron_home)
except CalledProcessError as e:
print(f"Error:\n{e.output}")
raise
Expand Down
19 changes: 19 additions & 0 deletions tests/test_platformwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@
from pathlib import Path


def test_run_command(get_pyproject_toml):
p = None
try:
options = create_server_options()

p = PlatformWrapper(options=options, project_toml_file=get_pyproject_toml)
cmd = 'ls -la'.split()
output = p.run_command(cmd=cmd).split("\n")

try:
next(filter(lambda a: a.find("config") > 0, output))
except StopIteration:
pytest.fail("Couldn't find config in directory!")

finally:
p.shutdown_platform()
p.cleanup()


def test_can_enable_sys_queue(get_pyproject_toml):
p = None
try:
Expand Down

0 comments on commit 9c834fb

Please sign in to comment.