Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
""" get host and port for a proxy connection from a Resource
Args:
res (Resource): The resource to retrieve the proxy for
default_port (optional): TCP port to use if no other port is
configured
force_port (optional): TCP port to use instead of the one
configured for the resource
Returns:
(host, port) host and port for the proxy connection
Raises:
ExecutionError: if the SSH connection/forwarding fails
"""
assert isinstance(res, Resource)
s = urlsplit('//'+res.host)
host = s.hostname
if force_port:
port = force_port
elif s.port:
port = s.port
else:
port = getattr(res, 'port', None) or default_port
if cls._force_proxy:
port = sshmanager.request_forward(cls._force_proxy, host, port)
host = 'localhost'
extra = getattr(res, 'extra', {})
if extra:
import attr
from ..factory import target_factory
from .common import NetworkResource, Resource
@target_factory.reg_resource
@attr.s(eq=False)
class Flashrom(Resource):
"""Programmer is the programmer parameter described in man(8) of flashrom"""
programmer = attr.ib(validator=attr.validators.instance_of(str))
@target_factory.reg_resource
@attr.s(eq=False)
class NetworkFlashrom(NetworkResource):
programmer = attr.ib(validator=attr.validators.instance_of(str))
def __attrs_post_init__(self):
self.resources = []
def _add_resource(self, resource):
self.resources.append(resource)
self.on_resource_added(resource)
def on_resource_added(self, resource):
pass
def poll(self):
pass
@attr.s(eq=False)
class ManagedResource(Resource):
"""
Represents a resource which can appear and disappear at runtime. Every
ManagedResource has a corresponding ResourceManager which handles these
events.
"""
manager_cls = ResourceManager
timeout = attr.ib(default=2.0, init=False, validator=attr.validators.instance_of(float))
def __attrs_post_init__(self):
super().__attrs_post_init__()
self.avail = False
self.manager = self.manager_cls.get()
self.manager._add_resource(self)
def poll(self):
It has to be created with the to be synced file and the target resource as
argument:
::
from labgrid.util.managedfile import ManagedFile
ManagedFile("/tmp/examplefile", )
Synchronisation is done with the sync_to_resource method.
"""
local_path = attr.ib(
validator=attr.validators.instance_of(str),
converter=lambda x: os.path.abspath(str(x))
)
resource = attr.ib(
validator=attr.validators.instance_of(Resource),
)
detect_nfs = attr.ib(default=True, validator=attr.validators.instance_of(bool))
def __attrs_post_init__(self):
if not os.path.isfile(self.local_path):
raise FileNotFoundError("Local file {} not found".format(self.local_path))
self.logger = logging.getLogger("{}".format(self))
self.hash = None
self.rpath = None
self._on_nfs_cached = None
def sync_to_resource(self):
"""sync the file to the host specified in a resource
Raises:
ExecutionError: if the SSH connection/copy fails
from .common import Resource
@attr.s(eq=False)
class SerialPort(Resource):
"""The basic SerialPort describes port and speed
Args:
port (str): port to connect to
speed (int): speed of the port, defaults to 115200"""
port = attr.ib(default=None)
speed = attr.ib(default=115200, validator=attr.validators.instance_of(int))
@attr.s(eq=False)
class EthernetInterface(Resource):
"""The basic EthernetInterface contains an interfacename
Args:
ifname (str): name of the interface"""
ifname = attr.ib(default=None)
@target_factory.reg_resource
@attr.s
class EthernetPort(Resource):
"""The basic EthernetPort describes a switch and interface
Args:
switch (str): name of the switch
interface (str): name of the interface"""
switch = attr.ib(default=None)
interface = attr.ib(default=None)
speed (int): speed of the port, defaults to 115200"""
port = attr.ib(default=None)
speed = attr.ib(default=115200, validator=attr.validators.instance_of(int))
@attr.s(eq=False)
class EthernetInterface(Resource):
"""The basic EthernetInterface contains an interfacename
Args:
ifname (str): name of the interface"""
ifname = attr.ib(default=None)
@target_factory.reg_resource
@attr.s
class EthernetPort(Resource):
"""The basic EthernetPort describes a switch and interface
Args:
switch (str): name of the switch
interface (str): name of the interface"""
switch = attr.ib(default=None)
interface = attr.ib(default=None)
@target_factory.reg_resource
@attr.s(eq=False)
class SysfsGPIO(Resource):
"""The basic SysfsGPIO contains an index
Args:
index (int): index of target gpio line."""
def get_command(cls, res, host, port, ifname=None):
"""get argument list to start a proxy process connected to the target"""
assert isinstance(res, Resource)
proxy = cls._force_proxy
extra = getattr(res, 'extra', {})
if extra.get('proxy_required') or proxy: # use specific proxy when needed
proxy = extra.get('proxy') or proxy
if not proxy:
return None
conn = sshmanager.get(proxy)
command = conn.get_prefix()
if ifname:
command += [
"--",
"sudo", "--non-interactive",
import attr
from ..factory import target_factory
from .common import Resource
@target_factory.reg_resource
@attr.s(eq=False)
class NetworkService(Resource):
address = attr.ib(validator=attr.validators.instance_of(str))
username = attr.ib(validator=attr.validators.instance_of(str))
password = attr.ib(default='', validator=attr.validators.instance_of(str))
port = attr.ib(default=22, validator=attr.validators.instance_of(int))
import attr
from ..factory import target_factory
from .common import Resource, NetworkResource
from .base import SerialPort
@target_factory.reg_resource
@attr.s(eq=False)
class RawSerialPort(SerialPort, Resource):
"""RawSerialPort describes a serialport which is available on the local computer."""
def __attrs_post_init__(self):
super().__attrs_post_init__()
if self.port is None:
ValueError("RawSerialPort must be configured with a port")
# This does not derive from SerialPort because it is not directly accessible
@target_factory.reg_resource
@attr.s(eq=False)
class NetworkSerialPort(NetworkResource):
"""A NetworkSerialPort is a remotely accessable serialport, usually
accessed via rfc2217 or tcp raw.
Args:
port (str): socket port to connect to
speed (int): speed of the port e.g. 9800
import attr
from ..factory import target_factory
from .common import Resource
@target_factory.reg_resource
@attr.s(eq=False)
class ModbusTCPCoil(Resource):
"""This resource describes Modbus TCP coil.
Args:
host (str): hostname of the Modbus TCP server e.g. "192.168.23.42:502"
coil (int): index of the coil e.g. 3
invert (bool): optional, whether the logic level is be inverted (active-low)"""
host = attr.ib(validator=attr.validators.instance_of(str))
coil = attr.ib(validator=attr.validators.instance_of(int))
invert = attr.ib(default=False, validator=attr.validators.instance_of(bool))