Skip to content

Commit

Permalink
Merge 159d8e6 into e83bcba
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffLIrion committed Nov 14, 2019
2 parents e83bcba + 159d8e6 commit be63fc4
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions androidtv/adb_manager.py
Expand Up @@ -86,6 +86,7 @@ def connect(self, always_log_errors=True, auth_timeout_s=DEFAULT_AUTH_TIMEOUT_S)
try:
# Catch errors
try:
# Connect with authentication
if self.adbkey:
# private key
with open(self.adbkey) as f:
Expand All @@ -100,14 +101,16 @@ def connect(self, always_log_errors=True, auth_timeout_s=DEFAULT_AUTH_TIMEOUT_S)

signer = PythonRSASigner(pub, priv)

# Connect to the device
self._adb.connect(rsa_keys=[signer], auth_timeout_s=auth_timeout_s)

# Connect without authentication
else:
self._adb.connect(auth_timeout_s=auth_timeout_s)

# ADB connection successfully established
self._available = True
_LOGGER.debug("ADB connection to %s successfully established", self.host)
self._available = True
return True

except OSError as exc:
if self._available or always_log_errors:
Expand All @@ -116,24 +119,25 @@ def connect(self, always_log_errors=True, auth_timeout_s=DEFAULT_AUTH_TIMEOUT_S)
_LOGGER.warning("Couldn't connect to host %s. %s: %s", self.host, exc.__class__.__name__, exc.strerror)

# ADB connection attempt failed
self._adb.close()
self.close()
self._available = False
return False

except Exception as exc: # pylint: disable=broad-except
if self._available or always_log_errors:
_LOGGER.warning("Couldn't connect to host %s. %s: %s", self.host, exc.__class__.__name__, exc)

# ADB connection attempt failed
self._adb.close()
self.close()
self._available = False

finally:
return self._available
return False

finally:
self._adb_lock.release()

# Lock could not be acquired
_LOGGER.warning("Couldn't connect to host %s because adb-shell lock not acquired.", self.host)
self.close()
self._available = False
return False

Expand Down Expand Up @@ -163,7 +167,7 @@ def shell(self, cmd):
self._adb_lock.release()

# Lock could not be acquired
_LOGGER.debug("ADB command not sent to %s because adb-shell lock not acquired: %s", self.host, cmd)
_LOGGER.warning("ADB command not sent to %s because adb-shell lock not acquired: %s", self.host, cmd)
return None


Expand Down Expand Up @@ -270,27 +274,31 @@ def connect(self, always_log_errors=True):
if self._adb_device:
_LOGGER.debug("ADB connection to %s via ADB server %s:%s successfully established", self.host, self.adb_server_ip, self.adb_server_port)
self._available = True
return True

# ADB connection attempt failed (without an exception)
else:
if self._available or always_log_errors:
_LOGGER.warning("Couldn't connect to host %s via ADB server %s:%s", self.host, self.adb_server_ip, self.adb_server_port)
self._available = False
if self._available or always_log_errors:
_LOGGER.warning("Couldn't connect to host %s via ADB server %s:%s", self.host, self.adb_server_ip, self.adb_server_port)

self.close()
self._available = False
return False

# ADB connection attempt failed
except Exception as exc: # noqa pylint: disable=broad-except
if self._available or always_log_errors:
_LOGGER.warning("Couldn't connect to host %s via ADB server %s:%s, error: %s", self.host, self.adb_server_ip, self.adb_server_port, exc)

# ADB connection attempt failed
self.close()
self._available = False

finally:
return self._available
return False

finally:
self._adb_lock.release()

# Lock could not be acquired
_LOGGER.warning("Couldn't connect to host %s via ADB server %s:%s because pure-python-adb lock not acquired.", self.host, self.adb_server_ip, self.adb_server_port)
self.close()
self._available = False
return False

Expand Down Expand Up @@ -320,5 +328,5 @@ def shell(self, cmd):
self._adb_lock.release()

# Lock could not be acquired
_LOGGER.debug("ADB command not sent to %s via ADB server %s:%s because pure-python-adb lock not acquired: %s", self.host, self.adb_server_ip, self.adb_server_port, cmd)
_LOGGER.warning("ADB command not sent to %s via ADB server %s:%s because pure-python-adb lock not acquired: %s", self.host, self.adb_server_ip, self.adb_server_port, cmd)
return None

0 comments on commit be63fc4

Please sign in to comment.