From 069d2322f1e9e474cc0c0717fc62d1e46a1b3386 Mon Sep 17 00:00:00 2001 From: Douglas Raillard Date: Thu, 6 Apr 2023 21:02:17 +0100 Subject: [PATCH] connection: Add BackgroundCommand.__init__(conn) Add a constructor to BackgroundCommand so that the command knows the connection it's tied to. --- devlib/connection.py | 11 ++++++++--- devlib/host.py | 2 +- devlib/utils/android.py | 13 +++++++++---- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/devlib/connection.py b/devlib/connection.py index 9699d76..b10fde3 100644 --- a/devlib/connection.py +++ b/devlib/connection.py @@ -120,6 +120,10 @@ class BackgroundCommand(ABC): Instances of this class can be used as context managers, with the same semantic as :class:`subprocess.Popen`. """ + + def __init__(self, conn): + self.conn = conn + @abstractmethod def send_signal(self, sig): """ @@ -235,7 +239,8 @@ class PopenBackgroundCommand(BackgroundCommand): :class:`subprocess.Popen`-based background command. """ - def __init__(self, popen): + def __init__(self, conn, popen): + super().__init__(conn=conn) self.popen = popen def send_signal(self, sig): @@ -291,9 +296,9 @@ class ParamikoBackgroundCommand(BackgroundCommand): :mod:`paramiko`-based background command. """ def __init__(self, conn, chan, pid, as_root, cmd, stdin, stdout, stderr, redirect_thread): + super().__init__(conn=conn) self.chan = chan self.as_root = as_root - self.conn = conn self._pid = pid self._stdin = stdin self._stdout = stdout @@ -454,7 +459,7 @@ class AdbBackgroundCommand(BackgroundCommand): """ def __init__(self, conn, adb_popen, pid, as_root): - self.conn = conn + super().__init__(conn=conn) self.as_root = as_root self.adb_popen = adb_popen self._pid = pid diff --git a/devlib/host.py b/devlib/host.py index a6796da..d067d80 100644 --- a/devlib/host.py +++ b/devlib/host.py @@ -141,7 +141,7 @@ class LocalConnection(ConnectionBase): shell=True, preexec_fn=preexec_fn, ) - bg_cmd = PopenBackgroundCommand(popen) + bg_cmd = PopenBackgroundCommand(self, popen) self._current_bg_cmds.add(bg_cmd) return bg_cmd diff --git a/devlib/utils/android.py b/devlib/utils/android.py index b57910d..50bca30 100755 --- a/devlib/utils/android.py +++ b/devlib/utils/android.py @@ -334,7 +334,12 @@ class AdbConnection(ConnectionBase): adb_command(self.device, command, timeout=timeout, adb_server=self.adb_server) else: with self.transfer_mgr.manage(sources, dest, action): - bg_cmd = adb_command_background(self.device, command, adb_server=self.adb_server) + bg_cmd = adb_command_background( + device=self.device, + conn=self, + command=command, + adb_server=self.adb_server + ) self.transfer_mgr.set_transfer_and_wait(bg_cmd) # pylint: disable=unused-argument @@ -692,11 +697,11 @@ def adb_command(device, command, timeout=None, adb_server=None): return output -def adb_command_background(device, command, adb_server=None): +def adb_command_background(device, conn, command, adb_server=None): full_command = get_adb_command(device, command, adb_server) logger.debug(full_command) - proc = get_subprocess(full_command, shell=True) - cmd = PopenBackgroundCommand(proc) + popen = get_subprocess(full_command, shell=True) + cmd = PopenBackgroundCommand(conn=conn, popen=popen) return cmd