Skip to content

Commit abb1377

Browse files
authored
Merge pull request #1137 from Bastian-Krause/bst/subcommand-return-codes
remote/client: return exit code for ssh/scp/rsync/telnet/video/audio/console
2 parents 08b3f70 + cdebb1c commit abb1377

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

labgrid/driver/httpvideodriver.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ def stream(self, quality_hint=None):
4545
"sync=false",
4646
]
4747

48-
subprocess.run(pipeline)
48+
sub = subprocess.run(pipeline)
49+
return sub.returncode

labgrid/driver/usbaudiodriver.py

+2
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,5 @@ def play(self):
204204

205205
rx.communicate()
206206
tx.communicate()
207+
208+
return tx.returncode or rx.returncode

labgrid/remote/client.py

+41-12
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ class ServerError(Error):
4747
pass
4848

4949

50+
class InteractiveCommandError(Error):
51+
pass
52+
53+
5054
class ClientSession(ApplicationSession):
5155
"""The ClientSession encapsulates all the actions a Client can Invoke on
5256
the coordinator."""
@@ -842,14 +846,17 @@ async def _console(self, place, target, timeout, *, logfile=None, loop=False, li
842846
raise
843847
if p.returncode:
844848
print("connection lost", file=sys.stderr)
845-
return False
846-
return True
849+
return p.returncode
847850

848851
async def console(self, place, target):
849852
while True:
850853
res = await self._console(place, target, 10.0, logfile=self.args.logfile,
851854
loop=self.args.loop, listen_only=self.args.listenonly)
852-
if res or not self.args.loop:
855+
if res:
856+
exc = InteractiveCommandError("microcom error")
857+
exc.exitcode = res
858+
raise exc
859+
if not self.args.loop:
853860
break
854861
await asyncio.sleep(1.0)
855862
console.needs_target = True
@@ -1033,20 +1040,28 @@ def ssh(self):
10331040
drv = self._get_ssh()
10341041

10351042
res = drv.interact(self.args.leftover)
1036-
if res == 255:
1037-
print("connection lost (SSH error)", file=sys.stderr)
1038-
elif res:
1039-
print(f"connection lost (remote exit code {res})", file=sys.stderr)
1043+
if res:
1044+
exc = InteractiveCommandError("ssh error")
1045+
exc.exitcode = res
1046+
raise exc
10401047

10411048
def scp(self):
10421049
drv = self._get_ssh()
10431050

1044-
drv.scp(src=self.args.src, dst=self.args.dst)
1051+
res = drv.scp(src=self.args.src, dst=self.args.dst)
1052+
if res:
1053+
exc = InteractiveCommandError("scp error")
1054+
exc.exitcode = res
1055+
raise exc
10451056

10461057
def rsync(self):
10471058
drv = self._get_ssh()
10481059

1049-
drv.rsync(src=self.args.src, dst=self.args.dst, extra=self.args.leftover)
1060+
res = drv.rsync(src=self.args.src, dst=self.args.dst, extra=self.args.leftover)
1061+
if res:
1062+
exc = InteractiveCommandError("rsync error")
1063+
exc.exitcode = res
1064+
raise exc
10501065

10511066
def sshfs(self):
10521067
drv = self._get_ssh()
@@ -1084,7 +1099,9 @@ def telnet(self):
10841099
args = ['telnet', str(ip)]
10851100
res = subprocess.call(args)
10861101
if res:
1087-
print("connection lost", file=sys.stderr)
1102+
exc = InteractiveCommandError("telnet error")
1103+
exc.exitcode = res
1104+
raise exc
10881105

10891106
def video(self):
10901107
place = self.get_acquired_place()
@@ -1115,14 +1132,22 @@ def video(self):
11151132
mark = '*' if default == name else ' '
11161133
print(f"{mark} {name:<10s} {caps:s}")
11171134
else:
1118-
drv.stream(quality, controls=controls)
1135+
res = drv.stream(quality, controls=controls)
1136+
if res:
1137+
exc = InteractiveCommandError("gst-launch-1.0 error")
1138+
exc.exitcode = res
1139+
raise exc
11191140

11201141
def audio(self):
11211142
place = self.get_acquired_place()
11221143
target = self._get_target(place)
11231144
name = self.args.name
11241145
drv = self._get_driver_or_new(target, "USBAudioInputDriver", name=name)
1125-
drv.play()
1146+
res = drv.play()
1147+
if res:
1148+
exc = InteractiveCommandError("gst-launch-1.0 error")
1149+
exc.exitcode = res
1150+
raise exc
11261151

11271152
def _get_tmc(self):
11281153
place = self.get_acquired_place()
@@ -1864,6 +1889,10 @@ def main():
18641889
except ConnectionError as e:
18651890
print(f"Could not connect to coordinator: {e}", file=sys.stderr)
18661891
exitcode = 1
1892+
except InteractiveCommandError as e:
1893+
if args.debug:
1894+
traceback.print_exc(file=sys.stderr)
1895+
exitcode = e.exitcode
18671896
except Error as e:
18681897
if args.debug:
18691898
traceback.print_exc(file=sys.stderr)

0 commit comments

Comments
 (0)