From d44050118c5e482251de895bcc4f6b4108c4a607 Mon Sep 17 00:00:00 2001 From: gryf Date: Sun, 21 May 2017 20:52:56 +0200 Subject: [PATCH] Added adb_connect feature --- README.rst | 9 ++++++++- adbfs | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index ce842e2..461b1f7 100644 --- a/README.rst +++ b/README.rst @@ -70,6 +70,8 @@ You can configure behaviour of this plugin using ``.ini`` file located under suppress_colors = false root = adb_command = adb + adb_connect = + where: @@ -84,7 +86,12 @@ where: * ``root`` root directory to read. Everything outside of that directory will be omitted. That would be the fastest way to access certain location on the device. Note, that ``dirs_to_skip`` still apply inside this directory. -* ``adb_command`` = path to ``adb`` command. +* ``adb_command`` absolute or relative path to ``adb`` command. `~/` or + environment variables are allowed. +* ``adb_connect`` specifies if connection to specific device needs to be + performed before accessing shell. It is usefull for *adb over network* + feature. Typical value here is a device IP address with optional port, which + defaults to 5555. Limitations =========== diff --git a/adbfs b/adbfs index 57e40ba..4df0b66 100755 --- a/adbfs +++ b/adbfs @@ -57,8 +57,10 @@ class Conf(object): self.root = None self.suppress_colors = False self.adb_command = 'adb' + self.adb_connect = '' self.read() + self.connect() self.get_the_box() def get_the_box(self): @@ -94,6 +96,54 @@ class Conf(object): sys.stderr.write('There is no toolbox or busybox available.\n') sys.exit(1) + def get_attached_devices(self): + """Return a list of attached devices""" + cmd = [self.adb_command, 'devices'] + devices = [] + + try: + with open(os.devnull, 'w') as fnull: + result = subprocess.check_output(cmd, stderr=fnull) + except subprocess.CalledProcessError: + result = '' + + for line in result.split('\n'): + if line.startswith('*'): + continue + if line.strip() == 'List of devices attached': + continue + if line.strip() == '': + continue + identifier, _ = line.split() + devices.append(identifier) + + return devices + + def connect(self): + """ + If adb_connect is non empty string, perform connecting to specified + device over network using an address (or hostname). + """ + if not self.adb_connect: + return + + devices = self.get_attached_devices() + + for device in devices: + if self.adb_connect in device: + return # already connected, no need to reconnect + + cmd = [self.adb_command, 'connect', self.adb_connect] + with open(os.devnull, 'w') as fnull: + result = subprocess.check_output(cmd, stderr=fnull) + if result.split()[0] == 'connected': + subprocess.call([self.adb_command, 'wait-for-device']) + return + + sys.stderr.write('Unable to connect to `%s\'. Is adb over network ' + 'enabled on device?\n' % self.adb_connect) + sys.exit(2) + def read(self): """ Read config file and change the options according to values from that @@ -111,7 +161,8 @@ class Conf(object): 'dirs_to_skip': (cfg.get, 'dirs_to_skip'), 'suppress_colors': (cfg.get, 'suppress_colors'), 'root': (cfg.get, 'root'), - 'adb_command': (cfg.get, 'adb_command')} + 'adb_command': (cfg.get, 'adb_command'), + 'adb_connect': (cfg.get, 'adb_connect')} cfg.read(conf_fname) for key, (function, attr) in cfg_map.items():