Skip to content

Commit 7805d63

Browse files
committed
agentwrapper: try labgrid-python3 for agent environment
Allow probing of a labgrid-python3 executable and use that if available. This allows the setup of a distinct python environment with the necessary modules for agent operation. Since this is probed automatically raise a debug message in case labgrid-python3 is not found to aid in debugging. Signed-off-by: Rouven Czerwinski <rouven.czerwinski@linaro.org>
1 parent 04bb2b6 commit 7805d63

2 files changed

Lines changed: 24 additions & 7 deletions

File tree

labgrid/util/agentwrapper.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os.path
55
import socket
66
import subprocess
7+
import shutil
78
import traceback
89
import logging
910

@@ -60,20 +61,31 @@ def __init__(self, host=None):
6061
['rsync', '-e', ' '.join(ssh_opts), '-tq', agent,
6162
f'{host}:{agent_remote}'],
6263
)
64+
agent_python = "labgrid-python3" if not subprocess.call(ssh_opts + [host, '--', 'which', 'labgrid-python3']) else "python3"
65+
if agent_python == "python3":
66+
self.logger.debug(
67+
"labgrid-python3 on %s not found, using python3 which requires system installed python modules for agents",
68+
host
69+
)
6370
self.agent = subprocess.Popen(
64-
ssh_opts + [host, '--', 'python3', agent_remote],
71+
ssh_opts + [host, '--', agent_python, agent_remote],
6572
stdin=subprocess.PIPE,
6673
stdout=subprocess.PIPE,
6774
start_new_session=True,
6875
)
6976
else:
7077
# run locally
7178
self.fdpass, remote_fdpass = socket.socketpair()
79+
agent_python = "labgrid-python3" if shutil.which("labgrid-python3") else "python3"
80+
if agent_python == "python3":
81+
self.logger.debug(
82+
"labgrid-python3 not found locally, using python3 which requires system installed python modules for agents"
83+
)
7284
with remote_fdpass:
7385
env = os.environ.copy()
7486
env["LG_FDPASS"] = str(remote_fdpass.fileno())
7587
self.agent = subprocess.Popen(
76-
['python3', agent],
88+
[agent_python, agent],
7789
stdin=subprocess.PIPE,
7890
stdout=subprocess.PIPE,
7991
env=env,

tests/test_agent.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import socket
33
import subprocess
4+
import shutil
45

56
import pytest
67
from py.path import local
@@ -18,7 +19,7 @@ def subprocess_mock(mocker):
1819
agent = local(labgrid.util.agentwrapper.__file__).dirpath('agent.py')
1920

2021
def run(args, **kwargs):
21-
assert args[0] in ['rsync', 'ssh']
22+
assert args[0] in ['rsync', 'ssh', 'which']
2223
if args[0] == 'rsync':
2324
src = local(args[-2])
2425
assert src == agent
@@ -32,10 +33,14 @@ def run(args, **kwargs):
3233
assert '--' in args
3334
args = args[args.index('--')+1:]
3435
assert len(args) == 2
35-
assert args[0] == 'python3'
36-
assert args[1].startswith('.labgrid_agent')
37-
# we need to use the original here to get the coverage right
38-
return original(['python3', str(agent)], **kwargs)
36+
assert args[0] in ['labgrid-python3', 'python3', 'which']
37+
if args[0] in ['python3', 'labgrid-python3']:
38+
assert args[1].startswith('.labgrid_agent')
39+
# we need to use the original here to get the coverage right
40+
return original(['python3', str(agent)], **kwargs)
41+
else:
42+
lg_py3_env = 'true' if shutil.which('labgrid-python3') else 'false'
43+
return original([lg_py3_env], **kwargs)
3944

4045
mocker.patch('subprocess.Popen', run)
4146

0 commit comments

Comments
 (0)