Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added run_command feature to platform wrapper #53

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading