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
1 change: 1 addition & 0 deletions doc/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3405,6 +3405,7 @@ Arguments:
- script (str): optional, key in :ref:`images <labgrid-device-config-images>`
containing the script to execute for writing of the flashable device
- args (list of str): optional, list of arguments for flash script execution
- timeout (float): optional, timeout for script execution in seconds

The FlashScriptDriver allows running arbitrary programs to flash a device.
Some SoC or devices may require custom, one-off, or proprietary programs to
Expand Down
4 changes: 2 additions & 2 deletions labgrid/driver/flashscriptdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def on_deactivate(self):

@Driver.check_active
@step(args=["script"])
def flash(self, script=None, args=None):
def flash(self, script=None, args=None, timeout=None):
"""
Transfers and remotely executes the script

Expand All @@ -55,5 +55,5 @@ def flash(self, script=None, args=None):

self.logger.debug("Running command '%s'", " ".join(cmd))
processwrapper.check_output(
self.device.command_prefix + cmd, print_on_silent_log=True
self.device.command_prefix + cmd, print_on_silent_log=True, timeout=timeout
)
20 changes: 16 additions & 4 deletions labgrid/util/helper.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import errno
import fcntl
import os
import logging
import os
import pty
import re
import select
import subprocess
import errno
from socket import socket, AF_INET, SOCK_STREAM
from contextlib import closing
from socket import AF_INET, SOCK_STREAM, socket

import attr

from ..step import step
from .timeout import Timeout

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

Expand Down Expand Up @@ -40,7 +41,7 @@ class ProcessWrapper:
loglevel = logging.INFO

@step(args=['command'], result=True, tag='process')
def check_output(self, command, *, print_on_silent_log=False, input=None, stdin=None): # pylint: disable=redefined-builtin
def check_output(self, command, *, print_on_silent_log=False, input=None, stdin=None, timeout=None): # pylint: disable=redefined-builtin
"""Run a command and supply the output to callback functions"""
logger = logging.getLogger("Process")
res = []
Expand Down Expand Up @@ -82,6 +83,9 @@ def check_output(self, command, *, print_on_silent_log=False, input=None, stdin=
if stdin_w is not None:
write_fds.append(stdin_w)

if timeout is not None:
timeout = Timeout(timeout)

while True:
ready_r, ready_w, _ = select.select(read_fds, write_fds, [], 0.1)

Expand Down Expand Up @@ -127,6 +131,9 @@ def check_output(self, command, *, print_on_silent_log=False, input=None, stdin=
if process.returncode is not None:
break

if timeout is not None and timeout.expired:
break

if stdin_w is not None:
os.close(stdin_w)

Expand All @@ -143,6 +150,11 @@ def check_output(self, command, *, print_on_silent_log=False, input=None, stdin=
if print_on_silent_log and logger.getEffectiveLevel() > ProcessWrapper.loglevel:
self.disable_print()

if timeout is not None and timeout.expired:
raise TimeoutError(
f"Timeout of {timeout.timeout} seconds exceeded while waiting for process: [{process.pid}] command: {command}"
)

if process.returncode != 0:
raise subprocess.CalledProcessError(process.returncode,
command,
Expand Down