Skip to content
Open
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
2 changes: 1 addition & 1 deletion labgrid/remote/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def _start(self, start_params):
assert self.local.avail
assert self.child is None
assert start_params["path"].startswith("/dev/")
self.port = get_free_port()
self.port = get_free_port(os.environ.get("LG_PREFERRED_NETWORKSERIAL_PORT", None))

# Ser2net has switched to using YAML format at version 4.0.0.
result = subprocess.run([self.ser2net_bin, "-v"], capture_output=True, text=True)
Expand Down
17 changes: 15 additions & 2 deletions labgrid/util/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,23 @@

re_vt100 = re.compile(r"(\x1b\[|\x9b)[^@-_a-z]*[@-_a-z]|\x1b[@-_a-z]")

def get_free_port():
def get_free_port(prefered_port=None):
"""Helper function to always return an unused port."""
with closing(socket(AF_INET, SOCK_STREAM)) as s:
s.bind(('', 0))
if prefered_port is not None:
port_range = [int(i) for i in prefered_port.split(':')]
port = port_range[0]
max_range = port_range[1] if len(port_range) > 1 else 10
max_no_of_ports = port + max_range
while port <= max_no_of_ports:
try:
s.bind(('', port))
except:
port += 1
else:
break
else:
s.bind(('', 0))
return s.getsockname()[1]


Expand Down
4 changes: 4 additions & 0 deletions man/labgrid-exporter.1
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ The following environment variable can be used to configure labgrid\-exporter.
.sp
This variable can be used to set the default coordinator in the format
\fBHOST[:PORT]\fP (instead of using the \fB\-x\fP option).
.SS LG_PREFERRED_NETWORKSERIAL_PORT
.sp
Set a preferred port to be used with NetworkSerialPort resources.
Specify a port and a optional range counting up using the format \fBPORT[:RANGE]\fP
.SH EXAMPLES
.sp
Start the exporter with the configuration file \fImy\-config.yaml\fP:
Expand Down
7 changes: 7 additions & 0 deletions man/labgrid-exporter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ LG_COORDINATOR
This variable can be used to set the default coordinator in the format
``HOST[:PORT]`` (instead of using the ``-x`` option).

LG_PREFERRED_NETWORKSERIAL_PORT
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Set a preferred port to be used with NetworkSerialPort resources.
Specify a port and a optional range counting up using the format ``PORT[:RANGE]``



EXAMPLES
--------

Expand Down
Loading