Fix: [ALAS] Allow removing non-existent forwards and reverses

This commit is contained in:
LmeSzinc 2024-04-25 19:05:22 +08:00
parent 7c8d41614f
commit d5f641aeb4

View File

@ -513,30 +513,51 @@ class Connection(ConnectionAttr):
def adb_forward_remove(self, local): def adb_forward_remove(self, local):
""" """
Equivalent to `adb -s <serial> forward --remove <local>` Equivalent to `adb -s <serial> forward --remove <local>`
No error raised when removing a non-existent forward
More about the commands send to ADB server, see: More about the commands send to ADB server, see:
https://cs.android.com/android/platform/superproject/+/master:packages/modules/adb/SERVICES.TXT https://cs.android.com/android/platform/superproject/+/master:packages/modules/adb/SERVICES.TXT
Args: Args:
local (str): Such as 'tcp:2437' local (str): Such as 'tcp:2437'
""" """
with self.adb_client._connect() as c: try:
list_cmd = f"host-serial:{self.serial}:killforward:{local}" with self.adb_client._connect() as c:
c.send_command(list_cmd) list_cmd = f"host-serial:{self.serial}:killforward:{local}"
c.check_okay() c.send_command(list_cmd)
c.check_okay()
except AdbError as e:
# No error raised when removing a non-existed forward
# adbutils.errors.AdbError: listener 'tcp:8888' not found
msg = str(e)
if re.search(r'listener .*? not found', msg):
logger.warning(f'{type(e).__name__}: {msg}')
else:
raise
def adb_reverse_remove(self, local): def adb_reverse_remove(self, local):
""" """
Equivalent to `adb -s <serial> reverse --remove <local>` Equivalent to `adb -s <serial> reverse --remove <local>`
No error raised when removing a non-existent reverse
Args: Args:
local (str): Such as 'tcp:2437' local (str): Such as 'tcp:2437'
""" """
with self.adb_client._connect() as c: try:
c.send_command(f"host:transport:{self.serial}") with self.adb_client._connect() as c:
c.check_okay() c.send_command(f"host:transport:{self.serial}")
list_cmd = f"reverse:killforward:{local}" c.check_okay()
c.send_command(list_cmd) list_cmd = f"reverse:killforward:{local}"
c.check_okay() c.send_command(list_cmd)
c.check_okay()
except AdbError as e:
# No error raised when removing a non-existed forward
# adbutils.errors.AdbError: listener 'tcp:8888' not found
msg = str(e)
if re.search(r'listener .*? not found', msg):
logger.warning(f'{type(e).__name__}: {msg}')
else:
raise
def adb_push(self, local, remote): def adb_push(self, local, remote):
""" """