sanitycheck: run pre/post script using dedicated function

Reduce duplicated code by introducing a dedicated function for running
custom scripts.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
This commit is contained in:
Anas Nashif 2020-01-03 08:44:29 -05:00
commit 1b1a7e242b

View file

@ -576,7 +576,7 @@ class BinaryHandler(Handler):
t.join()
proc.wait()
self.returncode = proc.returncode
(stdout, stderr) = proc.communicate(timeout=30)
_, stderr = proc.communicate(timeout=30)
if stderr:
logger.error(stderr.decode())
@ -685,6 +685,18 @@ class DeviceHandler(Handler):
if i['serial'] == serial:
i['available'] = True
@staticmethod
def run_custom_script(script, timeout):
with subprocess.Popen(script, stderr=subprocess.PIPE, stdout=subprocess.PIPE) as proc:
try:
stdout, _ = proc.communicate(timeout=timeout)
logger.debug(stdout.decode())
except subprocess.TimeoutExpired:
proc.kill()
proc.communicate()
logger.error("{} timed out".format(script))
def handle(self):
out_state = "failed"
@ -768,15 +780,7 @@ class DeviceHandler(Handler):
post_script = hardware.get('post_script')
if pre_script:
with subprocess.Popen(pre_script, stderr=subprocess.PIPE, stdout=subprocess.PIPE) as proc:
try:
(stdout, stderr) = proc.communicate(timeout=30)
logger.debug(stdout.decode())
except subprocess.TimeoutExpired:
proc.kill()
(stdout, stderr) = proc.communicate()
logger.error("{} timed out".format(post_script))
self.run_custom_script(pre_script, 30)
t = threading.Thread(target=self.monitor_serial, daemon=True,
args=(ser, read_pipe, harness))
@ -833,15 +837,7 @@ class DeviceHandler(Handler):
self.set_state(out_state, handler_time)
if post_script:
with subprocess.Popen(post_script, stderr=subprocess.PIPE, stdout=subprocess.PIPE) as proc:
try:
(stdout, stderr) = proc.communicate(timeout=30)
logger.debug(stdout.decode())
except subprocess.TimeoutExpired:
proc.kill()
(stdout, stderr) = proc.communicate()
logger.error("{} timed out".format(post_script))
self.run_custom_script(post_script, 30)
self.make_device_available(serial_device)