labgrid icon indicating copy to clipboard operation
labgrid copied to clipboard

agent: add argument to specify python virutal env

Open nvincent-vossloh opened this issue 8 months ago • 2 comments

When using agentwrapper, the python packages used are the one installed on the machine. This commit add an argument to specify the path to a virtual env which will be used by the agent.

Description

Checklist

  • [ ] Documentation for the feature I have not seen documentation about AgentWrapper (git grep -i agentwrapper did not yield any .rst)
  • [ ] Tests for the feature I would need help with that
  • [ ] The arguments and description in doc/configuration.rst have been updated Not applicable as far as I understand
  • [ ] Add a section on how to use the feature to doc/usage.rst Let me know if you think this is necessary, I think it is an advanced feature which might be confusing for first time readers.
  • [ ] Add a section on how to use the feature to doc/development.rst Not applicable
  • [x] PR has been tested I tested with our internal fork of labgrid which adds a driver to make https requests from the exporter to the DUT.
  • [ ] Man pages have been regenerated Not applicable

I developped this feature in order to be able to use the urllib3 module pinned at a version (see https://github.com/labgrid-project/labgrid/discussions/1301#discussioncomment-9447382). The driver using this feature is not (yet) public but a first version is available here: https://github.com/SiemaApplications-attic/labgrid/commit/81918df3ec8f48efbab8a0c67b2a7b9b63afa983

Here is the modification added to the above patch in order to be able to specify the virtual env necessary for pyrequests labgrid driver:

commit 921bd05c2668993dfb64342d78898d215ecd9a86
Author: Nicolas VINCENT <[email protected]>
Date:   Fri May 24 11:37:13 2024 +0200

    pyrequest: Adds option to specify virtual env
    
    adds pyvirtenv option to be able to specify the virtual env used by
    pyrequests
    
    Signed-off-by: Nicolas VINCENT <[email protected]>

diff --git a/labgrid/driver/pyrequestsdriver.py b/labgrid/driver/pyrequestsdriver.py
index 0de22b6..f07e6d6 100644
--- a/labgrid/driver/pyrequestsdriver.py
+++ b/labgrid/driver/pyrequestsdriver.py
@@ -17,6 +18,8 @@ class PyRequestsDriver(Driver):
     bindings = {
         "iface": {"RemoteNetworkInterface", "NetworkInterface", "USBNetworkInterface"},
     }
+    pyvirtenv = attr.ib(default=None,
+                        validator=attr.validators.optional(attr.validators.instance_of(str)))
 
     def __attrs_post_init__(self):
         super().__attrs_post_init__()
@@ -27,7 +30,7 @@ class PyRequestsDriver(Driver):
             host = self.iface.host
         else:
             host = None
-        self.wrapper = AgentWrapper(host)
+        self.wrapper = AgentWrapper(host, self.pyvirtenv)
         self.proxy = self.wrapper.load('pyrequests')
 
     def on_deactivate(self):

Then the env yaml file use looks like this:

- targets:
  main:
    drivers:
      - PyRequestsDRiver:
        pyvirtenv: '/path/to/virt/env/'

I tried to run developper test without success... tox would return an error and I did not know which env file to provide to pytest.

I did not know if I should update all drivers using an instance of AgentWrapper in order to use this new feature. Just let me know.

I feel a little bit embarrassed with this PR as I have not been able to test it and document it according to the development guidelines, my intent is not to burden you but rather to give something back to this project.

Thanks in advance for your guidance.

nvincent-vossloh avatar Jun 10 '24 10:06 nvincent-vossloh