Skip to content

Commit

Permalink
Merge 9f3ffeb into 90072d5
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffLIrion committed Jun 27, 2020
2 parents 90072d5 + 9f3ffeb commit f93188f
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 44 deletions.
10 changes: 6 additions & 4 deletions androidtv/__init__.py
Expand Up @@ -13,7 +13,7 @@
__version__ = '0.0.44'


def setup(host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None, device_class='auto', auth_timeout_s=DEFAULT_AUTH_TIMEOUT_S):
def setup(host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None, device_class='auto', auth_timeout_s=DEFAULT_AUTH_TIMEOUT_S, signer=None):
"""Connect to a device and determine whether it's an Android TV or an Amazon Fire TV.
Parameters
Expand All @@ -34,6 +34,8 @@ def setup(host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, st
The type of device: ``'auto'`` (detect whether it is an Android TV or Fire TV device), ``'androidtv'``, or ``'firetv'```
auth_timeout_s : float
Authentication timeout (in seconds)
signer : PythonRSASigner, None
The signer for the ADB keys, as loaded by :meth:`androidtv.adb_manager.adb_manager_sync.ADBPythonSync.load_adbkey`
Returns
-------
Expand All @@ -42,21 +44,21 @@ def setup(host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, st
"""
if device_class == 'androidtv':
atv = AndroidTVSync(host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules)
atv = AndroidTVSync(host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, signer)
atv.adb_connect(auth_timeout_s=auth_timeout_s)
atv.device_properties = atv.get_device_properties()
return atv

if device_class == 'firetv':
ftv = FireTVSync(host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules)
ftv = FireTVSync(host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, signer)
ftv.adb_connect(auth_timeout_s=auth_timeout_s)
ftv.device_properties = ftv.get_device_properties()
return ftv

if device_class != 'auto':
raise ValueError("`device_class` must be 'androidtv', 'firetv', or 'auto'.")

aftv = BaseTVSync(host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules)
aftv = BaseTVSync(host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, signer)

# establish the ADB connection
aftv.adb_connect(auth_timeout_s=auth_timeout_s)
Expand Down
51 changes: 37 additions & 14 deletions androidtv/adb_manager/adb_manager_async.py
Expand Up @@ -102,13 +102,16 @@ class ADBPythonAsync(object):
The device port to which we are connecting (default is 5555)
adbkey : str
The path to the ``adbkey`` file for ADB authentication
signer : PythonRSASigner, None
The signer for the ADB keys, as loaded by :meth:`ADBPythonAsync.load_adbkey`
"""
def __init__(self, host, port, adbkey=''):
def __init__(self, host, port, adbkey='', signer=None):
self.host = host
self.port = int(port)
self.adbkey = adbkey
self._adb = AdbDeviceTcpAsync(host=self.host, port=self.port, default_transport_timeout_s=DEFAULT_ADB_TIMEOUT_S, banner=b'androidtv')
self._signer = signer

# keep track of whether the ADB connection is intact
self._available = False
Expand Down Expand Up @@ -156,20 +159,10 @@ async def connect(self, always_log_errors=True, auth_timeout_s=DEFAULT_AUTH_TIME
try:
# Connect with authentication
if self.adbkey:
# private key
with open(self.adbkey) as f:
priv = f.read()
if not self._signer:
self._signer = await self.load_adbkey(self.adbkey)

# public key
try:
with open(self.adbkey + '.pub') as f:
pub = f.read()
except FileNotFoundError:
pub = ''

signer = PythonRSASigner(pub, priv)

await self._adb.connect(rsa_keys=[signer], auth_timeout_s=auth_timeout_s)
await self._adb.connect(rsa_keys=[self._signer], auth_timeout_s=auth_timeout_s)

# Connect without authentication
else:
Expand Down Expand Up @@ -206,6 +199,36 @@ async def connect(self, always_log_errors=True, auth_timeout_s=DEFAULT_AUTH_TIME
self._available = False
return False

@staticmethod
async def load_adbkey(adbkey):
"""Load the ADB keys.
TODO: Make this real async.
Parameters
----------
adbkey : str
The path to the ``adbkey`` file for ADB authentication
Returns
-------
PythonRSASigner
The ``PythonRSASigner`` with the key files loaded
"""
# private key
with open(adbkey) as f:
priv = f.read()

# public key
try:
with open(adbkey + '.pub') as f:
pub = f.read()
except FileNotFoundError:
pub = ''

return PythonRSASigner(pub, priv)

async def pull(self, local_path, device_path):
"""Pull a file from the device using the Python ADB implementation.
Expand Down
49 changes: 35 additions & 14 deletions androidtv/adb_manager/adb_manager_sync.py
Expand Up @@ -69,13 +69,16 @@ class ADBPythonSync(object):
The device port to which we are connecting (default is 5555)
adbkey : str
The path to the ``adbkey`` file for ADB authentication
signer : PythonRSASigner, None
The signer for the ADB keys, as loaded by :meth:`ADBPythonSync.load_adbkey`
"""
def __init__(self, host, port, adbkey=''):
def __init__(self, host, port, adbkey='', signer=None):
self.host = host
self.port = int(port)
self.adbkey = adbkey
self._adb = AdbDeviceTcp(host=self.host, port=self.port, default_transport_timeout_s=DEFAULT_ADB_TIMEOUT_S)
self._signer = signer

# keep track of whether the ADB connection is intact
self._available = False
Expand Down Expand Up @@ -123,20 +126,10 @@ def connect(self, always_log_errors=True, auth_timeout_s=DEFAULT_AUTH_TIMEOUT_S)
try:
# Connect with authentication
if self.adbkey:
# private key
with open(self.adbkey) as f:
priv = f.read()
if not self._signer:
self._signer = self.load_adbkey(self.adbkey)

# public key
try:
with open(self.adbkey + '.pub') as f:
pub = f.read()
except FileNotFoundError:
pub = ''

signer = PythonRSASigner(pub, priv)

self._adb.connect(rsa_keys=[signer], auth_timeout_s=auth_timeout_s)
self._adb.connect(rsa_keys=[self._signer], auth_timeout_s=auth_timeout_s)

# Connect without authentication
else:
Expand Down Expand Up @@ -173,6 +166,34 @@ def connect(self, always_log_errors=True, auth_timeout_s=DEFAULT_AUTH_TIMEOUT_S)
self._available = False
return False

@staticmethod
def load_adbkey(adbkey):
"""Load the ADB keys.
Parameters
----------
adbkey : str
The path to the ``adbkey`` file for ADB authentication
Returns
-------
PythonRSASigner
The ``PythonRSASigner`` with the key files loaded
"""
# private key
with open(adbkey) as f:
priv = f.read()

# public key
try:
with open(adbkey + '.pub') as f:
pub = f.read()
except FileNotFoundError:
pub = ''

return PythonRSASigner(pub, priv)

def pull(self, local_path, device_path):
"""Pull a file from the device using the Python ADB implementation.
Expand Down
6 changes: 4 additions & 2 deletions androidtv/androidtv/androidtv_async.py
Expand Up @@ -30,11 +30,13 @@ class AndroidTVAsync(BaseTVAsync, BaseAndroidTV):
The port for the ADB server
state_detection_rules : dict, None
A dictionary of rules for determining the state (see :class:`~androidtv.basetv.basetv.BaseTV`)
signer : PythonRSASigner, None
The signer for the ADB keys, as loaded by :meth:`androidtv.adb_manager.adb_manager_async.ADBPythonAsync.load_adbkey`
"""

def __init__(self, host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None): # pylint: disable=super-init-not-called
BaseTVAsync.__init__(self, host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules)
def __init__(self, host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None, signer=None): # pylint: disable=super-init-not-called
BaseTVAsync.__init__(self, host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, signer)

# ======================================================================= #
# #
Expand Down
6 changes: 4 additions & 2 deletions androidtv/androidtv/androidtv_sync.py
Expand Up @@ -30,11 +30,13 @@ class AndroidTVSync(BaseTVSync, BaseAndroidTV):
The port for the ADB server
state_detection_rules : dict, None
A dictionary of rules for determining the state (see :class:`~androidtv.basetv.basetv.BaseTV`)
signer : PythonRSASigner, None
The signer for the ADB keys, as loaded by :meth:`androidtv.adb_manager.adb_manager_sync.ADBPythonSync.load_adbkey`
"""

def __init__(self, host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None): # pylint: disable=super-init-not-called
BaseTVSync.__init__(self, host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules)
def __init__(self, host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None, signer=None): # pylint: disable=super-init-not-called
BaseTVSync.__init__(self, host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, signer)

# ======================================================================= #
# #
Expand Down
6 changes: 4 additions & 2 deletions androidtv/basetv/basetv_async.py
Expand Up @@ -62,14 +62,16 @@ class BaseTVAsync(BaseTV):
The port for the ADB server
state_detection_rules : dict, None
A dictionary of rules for determining the state (see above)
signer : PythonRSASigner, None
The signer for the ADB keys, as loaded by :meth:`androidtv.adb_manager.adb_manager_async.ADBPythonAsync.load_adbkey`
"""

def __init__(self, host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None):
def __init__(self, host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None, signer=None):
# the handler for ADB commands
if not adb_server_ip:
# python-adb
adb = ADBPythonAsync(host, port, adbkey)
adb = ADBPythonAsync(host, port, adbkey, signer)
else:
# pure-python-adb
adb = ADBServerAsync(host, port, adb_server_ip, adb_server_port)
Expand Down
6 changes: 4 additions & 2 deletions androidtv/basetv/basetv_sync.py
Expand Up @@ -62,14 +62,16 @@ class BaseTVSync(BaseTV):
The port for the ADB server
state_detection_rules : dict, None
A dictionary of rules for determining the state (see above)
signer : PythonRSASigner, None
The signer for the ADB keys, as loaded by :meth:`androidtv.adb_manager.adb_manager_sync.ADBPythonSync.load_adbkey`
"""

def __init__(self, host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None):
def __init__(self, host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None, signer=None):
# the handler for ADB commands
if not adb_server_ip:
# python-adb
adb = ADBPythonSync(host, port, adbkey)
adb = ADBPythonSync(host, port, adbkey, signer)
else:
# pure-python-adb
adb = ADBServerSync(host, port, adb_server_ip, adb_server_port)
Expand Down
6 changes: 4 additions & 2 deletions androidtv/firetv/firetv_async.py
Expand Up @@ -30,11 +30,13 @@ class FireTVAsync(BaseTVAsync, BaseFireTV):
The port for the ADB server
state_detection_rules : dict, None
A dictionary of rules for determining the state (see :class:`~androidtv.basetv.basetv.BaseTV`)
signer : PythonRSASigner, None
The signer for the ADB keys, as loaded by :meth:`androidtv.adb_manager.adb_manager_sync.ADBPythonAsync.load_adbkey`
"""

def __init__(self, host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None): # pylint: disable=super-init-not-called
BaseTVAsync.__init__(self, host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules)
def __init__(self, host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None, signer=None): # pylint: disable=super-init-not-called
BaseTVAsync.__init__(self, host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, signer)

# ======================================================================= #
# #
Expand Down
6 changes: 4 additions & 2 deletions androidtv/firetv/firetv_sync.py
Expand Up @@ -30,11 +30,13 @@ class FireTVSync(BaseTVSync, BaseFireTV):
The port for the ADB server
state_detection_rules : dict, None
A dictionary of rules for determining the state (see :class:`~androidtv.basetv.basetv.BaseTV`)
signer : PythonRSASigner, None
The signer for the ADB keys, as loaded by :meth:`androidtv.adb_manager.adb_manager_sync.ADBPythonSync.load_adbkey`
"""

def __init__(self, host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None): # pylint: disable=super-init-not-called
BaseTVSync.__init__(self, host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules)
def __init__(self, host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None, signer=None): # pylint: disable=super-init-not-called
BaseTVSync.__init__(self, host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, signer)

# ======================================================================= #
# #
Expand Down

0 comments on commit f93188f

Please sign in to comment.