1
0
mirror of https://github.com/gryf/mc_adbfs.git synced 2026-03-27 22:03:34 +01:00

17 Commits
0.11 ... 0.15

Author SHA1 Message Date
8f3a22a729 Removed py2 lefotver, fix variable name. 2022-12-31 11:23:42 +01:00
cbef13ccea Merge pull request #5 from fanick1/master
fix typo: quite->quote
2022-12-29 19:54:58 +01:00
fanick1
9cfa834604 fix typo: quite->quote 2022-12-28 23:31:02 +01:00
b1a6219d21 Removed deprecated encoding argument from json.loads. 2020-12-07 19:08:29 +01:00
d16f0f06b8 Merge branch 'thp-various-fixes' 2020-06-09 19:55:21 +02:00
c2f07f5516 Merge branch 'various-fixes' of https://github.com/thp/mc_adbfs into thp-various-fixes 2020-06-09 19:55:06 +02:00
e9b196eaf8 Change SafeConfigParser to ConfigParser 2020-05-21 10:10:43 +02:00
039c078a35 Fix missing attribute on conf object 2020-04-26 13:48:21 +02:00
2776668913 Fix for removing files with space/parentheses.
Also, some further py2 cleanup.
2020-04-26 09:32:59 +02:00
390f1b1112 Drop Python2 support. 2020-04-26 09:16:59 +02:00
f7a6b145fd Added new option for trying su command. 2020-04-26 09:06:28 +02:00
9f1a51fdbf Added wrapper on adb shell commands 2020-04-26 09:04:28 +02:00
5ece2d579c Fix bad filenames for Python3.
In Python3, if there are filenames encoded with 8-bit encodings, there
might be an issues with converting them into unicode objects. This is a
workaround on this subject. Python2 is not affected.

Other than that, there was tests added to cover this case, appropriate
Makefile which automate creating venvs for both: Python 2 and 3, and
also there is a check against pep8 rules using flake8.
2019-05-14 21:14:24 +02:00
63fdc2c605 Removed unused elements
There was some items unused:

- In readme there was misleading entry, unused `skip_dirs` option.
- There was unused Exception class.
2019-05-14 20:43:28 +02:00
Thomas Perl
b088c45d3f Fix quoting for 'adb shell' 2019-05-09 11:58:29 +02:00
Thomas Perl
c5559f7d41 Python 3's ConfigParser is already the safe one 2019-05-09 11:38:21 +02:00
Thomas Perl
9ffa1a13af Default to Python 3 2019-05-09 11:38:00 +02:00
5 changed files with 215 additions and 60 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
.test
__pycache__
adbfsc

42
Makefile Normal file
View File

@@ -0,0 +1,42 @@
# simple makefile for running tests for the adbfs plugin
all: test_dir py3 flake8
TEST_DIR='.test'
PY3_VENV=$(TEST_DIR)/py3
FL8_VENV=$(TEST_DIR)/flake8
TST_EXISTS=$(shell [ -e $(TEST_DIR) ] && echo 1 || echo 0)
PY3_EXISTS=$(shell [ -e $(PY3_VENV) ] && echo 1 || echo 0)
FL8_EXISTS=$(shell [ -e $(FL8_VENV) ] && echo 1 || echo 0)
py3: test_dir virtualenv3
.test/py3/bin/python test_adbfs.py
flake8: test_dir virtualenv_flake8
.test/flake8/bin/flake8 adbfs test_adbfs.py
ifeq ($(TST_EXISTS), 0)
test_dir:
mkdir -p .test
else
test_dir:
endif
ifeq ($(PY3_EXISTS), 0)
virtualenv3:
virtualenv -p python3 $(PY3_VENV)
$(PY3_VENV)/bin/pip install six
else
virtualenv3:
endif
ifeq ($(FL8_EXISTS), 0)
virtualenv_flake8:
virtualenv -p python3 $(FL8_VENV)
$(FL8_VENV)/bin/pip install flake8
else
virtualenv_flake8:
endif
clean:
rm -fr $(TEST_DIR) __pycache__ adbfsc

View File

@@ -5,10 +5,11 @@ Midnight Commander adbfs external fs plugin
This is Midnight Commander extfs plugin for browsing Android device through This is Midnight Commander extfs plugin for browsing Android device through
``adb`` interface written in Python. ``adb`` interface written in Python.
Rquirements Rquirements
=========== ===========
* Python 2.7 or 3.x (tested on 3.5.4) * Python 3.x (tested on 3.5.4, 3.6 and 3.7)
* ``adb`` installed and in ``$PATH`` or provided via the config file * ``adb`` installed and in ``$PATH`` or provided via the config file
* An Android device or emulator preferably rooted * An Android device or emulator preferably rooted
* ``busybox`` (``toolbox``, ``toybox``) installed and available in the path on * ``busybox`` (``toolbox``, ``toybox``) installed and available in the path on
@@ -24,6 +25,7 @@ Make sure, that issuing from command line:
it should display files from root directory on the device. it should display files from root directory on the device.
Features Features
======== ========
@@ -35,12 +37,14 @@ Features
* Symbolic links in lists are corrected to be relative to the file system * Symbolic links in lists are corrected to be relative to the file system
* Symbolic links also point to the right target, skipping intermediate links * Symbolic links also point to the right target, skipping intermediate links
Installation Installation
============ ============
Copy adbfs into ``~/.local/share/mc/extfs.d/`` directory and make it executable Copy adbfs into ``~/.local/share/mc/extfs.d/`` directory and make it executable
if needed. if needed.
Usage Usage
===== =====
@@ -48,13 +52,14 @@ To use it, just issue:
.. code:: shell-session .. code:: shell-session
cd adbfs:// $ cd adbfs://
under MC - after some time you should see the files and directories on your under MC - after some time you should see the files and directories on your
device. For convenience you can add a bookmark (accessible under CTRL+\) for device. For convenience you can add a bookmark (accessible under CTRL+\\) for
fast access. The time is depended on how many files and directories you have on fast access. The time is depended on how many files and directories you have on
your device and how fast it is :) your device and how fast it is :)
Configuration Configuration
============= =============
@@ -66,13 +71,12 @@ You can configure behaviour of this plugin using ``.ini`` file located under
[adbfs] [adbfs]
debug = false debug = false
skip_dirs = true
dirs_to_skip = ["acct", "charger", "d", "dev", "proc", "sys"] dirs_to_skip = ["acct", "charger", "d", "dev", "proc", "sys"]
suppress_colors = false suppress_colors = false
root = root =
adb_command = adb adb_command = adb
adb_connect = adb_connect =
try_su = false
where: where:
@@ -87,12 +91,48 @@ where:
* ``root`` root directory to read. Everything outside of that directory will be * ``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 omitted. That would be the fastest way to access certain location on the
device. Note, that ``dirs_to_skip`` still apply inside this directory. device. Note, that ``dirs_to_skip`` still apply inside this directory.
* ``adb_command`` absolute or relative path to ``adb`` command. `~/` or * ``adb_command`` absolute or relative path to ``adb`` command. ``~/`` or
environment variables are allowed. environment variables are allowed.
* ``adb_connect`` specifies if connection to specific device needs to be * ``adb_connect`` specifies if connection to specific device needs to be
performed before accessing shell. It is useful for *adb over network* performed before accessing shell. It is useful for *adb over network*
feature. Typical value here is a device IP address with optional port, which feature. Typical value here is a device IP address with optional port, which
defaults to 5555. defaults to 5555.
* ``try_su`` specifies whether or not to try to detect if ``su`` command is
available and usable.
Contribution
============
There is a ``Makefile`` in the top directory, which is basic helper for running
the tests. Please use it, and adapt/add tests for provided fixes/functionality.
The reason why `tox`_ wasn't used is, that there is no ``setup.py`` file, and
it's difficult to install simple script, which isn't a python module (python
interpreter will refuse to import module without ``.py`` extension).
It requires GNU ``make`` program, and also ``virtualenv``. Using it is simple
as running following command:
.. code:: shell-session
$ make
it will run `py3` and `flake8` jobs to check it against the code. For
running tests against Python 3:
.. code:: shell-session
$ make py3
or flake 8:
.. code:: shell-session
$ make flake8
Exit status on any of those means that test fail. Appropriate message/traceback
will also be visible.
Limitations Limitations
=========== ===========
@@ -104,8 +144,11 @@ Limitations
* The implementation is experimental and it's by now working with mine device; * The implementation is experimental and it's by now working with mine device;
while it might not work with yours while it might not work with yours
License License
======= =======
This software is licensed under 3-clause BSD license. See LICENSE file for This software is licensed under 3-clause BSD license. See LICENSE file for
details. details.
.. _tox: https://tox.readthedocs.io

113
adbfs
View File

@@ -1,13 +1,10 @@
#! /usr/bin/env python #! /usr/bin/env python3
""" """
adbfs Virtual filesystem for Midnight Commander adbfs Virtual filesystem for Midnight Commander
* Copyright (c) 2016, Roman Dobosz, * Copyright (c) 2016, Roman Dobosz,
* Published under 3-clause BSD-style license (see LICENSE file) * Published under 3-clause BSD-style license (see LICENSE file)
""" """
try:
import ConfigParser as configparser
except ImportError:
import configparser import configparser
import argparse import argparse
from datetime import datetime from datetime import datetime
@@ -16,8 +13,9 @@ import os
import re import re
import subprocess import subprocess
import sys import sys
import shlex
__version__ = 0.11 __version__ = 0.14
XDG_CONFIG_HOME = os.getenv('XDG_CONFIG_HOME', os.path.expanduser('~/.config')) XDG_CONFIG_HOME = os.getenv('XDG_CONFIG_HOME', os.path.expanduser('~/.config'))
@@ -30,18 +28,18 @@ def check_output(command_list, stderr=None):
""" """
result = subprocess.check_output(command_list, stderr=stderr) result = subprocess.check_output(command_list, stderr=stderr)
if not isinstance(result, str): if not isinstance(result, str):
result = result.decode('utf-8') _result = []
for t in result.split(b'\n'):
if not t:
continue
try:
_result.append(t.decode('utf-8'))
except UnicodeDecodeError:
_result.append(t.decode('iso-8859-1'))
result = '\n'.join(_result) + '\n'
return result return result
class NoBoxFoundException(OSError):
"""
Exception raised in case of not found either toolbox or busybox on remote
filesystem accessed via adb
"""
pass
class Conf(object): class Conf(object):
"""Simple config parser""" """Simple config parser"""
boxes = {'busybox': {'ls': 'busybox ls -anel', boxes = {'busybox': {'ls': 'busybox ls -anel',
@@ -82,6 +80,7 @@ class Conf(object):
self.suppress_colors = False self.suppress_colors = False
self.adb_command = 'adb' self.adb_command = 'adb'
self.adb_connect = '' self.adb_connect = ''
self.try_su = False
self.read() self.read()
self.connect() self.connect()
@@ -189,13 +188,14 @@ class Conf(object):
if not os.path.exists(conf_fname): if not os.path.exists(conf_fname):
return return
cfg = configparser.SafeConfigParser() cfg = configparser.ConfigParser()
cfg_map = {'debug': (cfg.getboolean, 'debug'), cfg_map = {'debug': (cfg.getboolean, 'debug'),
'dirs_to_skip': (cfg.get, 'dirs_to_skip'), 'dirs_to_skip': (cfg.get, 'dirs_to_skip'),
'suppress_colors': (cfg.get, 'suppress_colors'), 'suppress_colors': (cfg.get, 'suppress_colors'),
'root': (cfg.get, 'root'), 'root': (cfg.get, 'root'),
'adb_command': (cfg.get, 'adb_command'), 'adb_command': (cfg.get, 'adb_command'),
'adb_connect': (cfg.get, 'adb_connect')} 'adb_connect': (cfg.get, 'adb_connect'),
'try_su': (cfg.getboolean, 'try_su')}
cfg.read(conf_fname) cfg.read(conf_fname)
for key, (function, attr) in cfg_map.items(): for key, (function, attr) in cfg_map.items():
@@ -205,7 +205,7 @@ class Conf(object):
pass pass
if self.dirs_to_skip and isinstance(self.dirs_to_skip, str): if self.dirs_to_skip and isinstance(self.dirs_to_skip, str):
self.dirs_to_skip = json.loads(self.dirs_to_skip, encoding='ascii') self.dirs_to_skip = json.loads(self.dirs_to_skip)
self.dirs_to_skip = [x.encode('utf-8') for x in self.dirs_to_skip] self.dirs_to_skip = [x.encode('utf-8') for x in self.dirs_to_skip]
else: else:
self.dirs_to_skip = [] self.dirs_to_skip = []
@@ -335,11 +335,23 @@ class Adb(object):
self._links = {} self._links = {}
self._got_root = False self._got_root = False
if self.conf.try_su:
self.__su_check() self.__su_check()
def _shell_cmd(self, with_root, *args):
cmd = [self.conf.adb_command, 'shell']
if with_root and self._got_root:
_args = [shlex.quote(x) for x in args]
cmd += ['su', '-c', shlex.quote(' '.join(_args))]
else:
cmd += args
return cmd
def __su_check(self): def __su_check(self):
"""Check if we are able to get elevated privileges""" """Check if we are able to get elevated privileges"""
cmd = [self.conf.adb_command] + 'shell su -c whoami'.split() cmd = self._shell_cmd(False, 'su -c whoami')
try: try:
with open(os.devnull, 'w') as fnull: with open(os.devnull, 'w') as fnull:
result = check_output(cmd, stderr=fnull) result = check_output(cmd, stderr=fnull)
@@ -389,10 +401,8 @@ class Adb(object):
def _retrieve_single_dir_list(self, dir_): def _retrieve_single_dir_list(self, dir_):
"""Retrieve file list using adb""" """Retrieve file list using adb"""
lscmd = self.conf.box['rls'].format(dir_) lscmd = self.conf.box['rls'].format(shlex.quote(dir_))
if self._got_root: command = self._shell_cmd(True, *shlex.split(lscmd))
lscmd = 'su -c "{}"'.format(lscmd)
command = [self.conf.adb_command, 'shell', lscmd]
try: try:
if self.conf.debug: if self.conf.debug:
@@ -403,7 +413,7 @@ class Adb(object):
sys.stderr.write('Cannot read directory. Is device connected?\n') sys.stderr.write('Cannot read directory. Is device connected?\n')
return 1 return 1
lines = [l.strip() for l in lines.split('\n') if l.strip()] lines = [line.strip() for line in lines.split('\n') if line.strip()]
if len(lines) == 1: if len(lines) == 1:
reg_match = self.file_re.match(lines[0]) reg_match = self.file_re.match(lines[0])
entry = File(**reg_match.groupdict()) entry = File(**reg_match.groupdict())
@@ -449,12 +459,9 @@ class Adb(object):
if not root: if not root:
lscmd = self.conf.box['ls'] lscmd = self.conf.box['ls']
else: else:
lscmd = self.conf.box['rls'].format(root.filepath) lscmd = self.conf.box['rls'].format(shlex.quote(root.filepath))
if self._got_root: command = self._shell_cmd(True, *shlex.split(lscmd))
lscmd = 'su -c "{}"'.format(lscmd)
command = [self.conf.adb_command, 'shell', lscmd]
try: try:
if self.conf.debug: if self.conf.debug:
@@ -463,7 +470,7 @@ class Adb(object):
lines = check_output(command) lines = check_output(command)
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
sys.stderr.write('Cannot read directory. Is device connected?\n') sys.stderr.write('Cannot read directory. Is device connected?\n')
return 1 return 2
current_dir = root.dirname if root else '/' current_dir = root.dirname if root else '/'
for line in lines.split('\n'): for line in lines.split('\n'):
@@ -499,13 +506,13 @@ class Adb(object):
"""Not supported""" """Not supported"""
sys.stderr.write('Not supported - or maybe you are on compatible ' sys.stderr.write('Not supported - or maybe you are on compatible '
'architecture?\n') 'architecture?\n')
return 1 return 3
def list(self): def list(self):
"""Output list contents directory""" """Output list contents directory"""
if self.error: if self.error:
sys.stderr.write(self.error) sys.stderr.write(self.error)
return 1 return 4
if self.conf.root: if self.conf.root:
self._retrieve_single_dir_list(self.conf.root) self._retrieve_single_dir_list(self.conf.root)
@@ -520,7 +527,7 @@ class Adb(object):
"""Copy file form the device using adb.""" """Copy file form the device using adb."""
if self.error: if self.error:
sys.stderr.write(self.error) sys.stderr.write(self.error)
return 1 return 5
cmd = [self.conf.adb_command, 'pull', src, dst] cmd = [self.conf.adb_command, 'pull', src, dst]
if self.conf.debug: if self.conf.debug:
@@ -531,7 +538,7 @@ class Adb(object):
err = subprocess.call(cmd, stdout=fnull, stderr=fnull) err = subprocess.call(cmd, stdout=fnull, stderr=fnull)
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
sys.stderr.write('Error executing adb shell') sys.stderr.write('Error executing adb shell')
return 1 return 6
return err return err
@@ -539,7 +546,7 @@ class Adb(object):
"""Copy file to the device through adb.""" """Copy file to the device through adb."""
if self.error: if self.error:
sys.stderr.write(self.error) sys.stderr.write(self.error)
return 1 return 7
if not dst.startswith('/'): if not dst.startswith('/'):
dst = '/' + dst dst = '/' + dst
@@ -552,69 +559,69 @@ class Adb(object):
err = subprocess.call(cmd, stdout=fnull, stderr=fnull) err = subprocess.call(cmd, stdout=fnull, stderr=fnull)
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
sys.stderr.write('Error executing adb shell') sys.stderr.write('Error executing adb shell')
return 1 return 8
if err != 0: if err != 0:
sys.stderr.write('Cannot push the file, ' sys.stderr.write('Cannot push the file, '
'%s, error %d' % (dst, err)) '%s, error %d' % (dst, err))
return 1 return 9
return 0 return 0
def rm(self, dst): def rm(self, dst):
"""Remove file from device.""" """Remove file from device."""
if self.error: if self.error:
sys.stderr.write(self.error) sys.stderr.write(self.error)
return 1 return 10
cmd = [self.conf.adb_command, 'shell', 'rm', dst] cmd = self._shell_cmd(False, 'rm %s' % shlex.quote(dst))
try: try:
err = check_output(cmd) err = check_output(cmd).strip()
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
sys.stderr.write('Error executing adb shell') sys.stderr.write('Error executing adb shell')
return 1 return 11
if err != '': if err != '':
sys.stderr.write(err) sys.stderr.write(err)
return 1 return 12
return 0 return 0
def rmdir(self, dst): def rmdir(self, dst):
"""Remove directory from device.""" """Remove directory from device."""
if self.error: if self.error:
sys.stderr.write(self.error) sys.stderr.write(self.error)
return 1 return 13
cmd = [self.conf.adb_command, 'shell', 'rm', '-r', dst] cmd = self._shell_cmd(False, 'rm -r %s' % shlex.quote(dst))
try: try:
err = check_output(cmd) err = check_output(cmd).strip()
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
sys.stderr.write('Error executing adb shell') sys.stderr.write('Error executing adb shell')
return 1 return 14
if err != '': if err != '':
sys.stderr.write(err) sys.stderr.write(err)
return 1 return 15
return 0 return 0
def mkdir(self, dst): def mkdir(self, dst):
"""Make directory on the device through adb.""" """Make directory on the device through adb."""
if self.error: if self.error:
sys.stderr.write(self.error) sys.stderr.write(self.error)
return 1 return 16
if not dst.startswith('/'): if not dst.startswith('/'):
dst = '/' + dst dst = '/' + dst
cmd = [self.conf.adb_command, 'shell', 'mkdir', dst] cmd = self._shell_cmd(False, 'mkdir %s' % shlex.quote(dst))
try: try:
err = check_output(cmd) err = check_output(cmd).strip()
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
sys.stderr.write('Error executing adb shell') sys.stderr.write('Error executing adb shell')
return 1 return 17
if err != '': if err != '':
sys.stderr.write(err) sys.stderr.write(err)
return 1 return 18
return 0 return 0
@@ -673,7 +680,11 @@ def main():
args = parser.parse_args() args = parser.parse_args()
try:
return args.func(args) return args.func(args)
except AttributeError:
parser.print_help()
parser.exit()
if __name__ == '__main__': if __name__ == '__main__':

56
test_adbfs.py Normal file
View File

@@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
import os
from importlib.util import spec_from_loader, module_from_spec
from importlib.machinery import SourceFileLoader
import unittest
from unittest import mock
module_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'adbfs')
spec = spec_from_loader("adbfs", SourceFileLoader("adbfs", module_path))
adbfs = module_from_spec(spec)
spec.loader.exec_module(adbfs)
LISTING = '''\
-rw-rw---- 1 0 1015 0 01/01/2010 22:11:01 /storage/emulated/0/Grüß Gott
-rw-rw---- 1 0 1015 0 01/01/2010 22:11:01 /storage/emulated/0/\x80
-rw-rw---- 1 0 1015 0 01/01/2010 22:11:01 /storage/emulated/0/Γεια σας
-rw-rw---- 1 0 1015 0 01/01/2010 22:11:01 /storage/emulated/0/Здравствуйте
-rw-rw---- 1 0 1015 0 01/01/2010 22:11:01 /storage/emulated/0/שָׁלוֹם
-rw-rw---- 1 0 1015 0 01/01/2010 22:11:01 /storage/emulated/0/السَّلامُ عَلَيْكُمْ
-rw-rw---- 1 0 1015 0 01/01/2010 22:11:01 /storage/emulated/0/გამარჯობა
-rw-rw---- 1 0 1015 0 01/01/2010 22:11:01 /storage/emulated/0/こんにちは。
-rw-rw---- 1 0 1015 0 01/01/2010 22:11:01 /storage/emulated/0/안녕하십니까
''' # noqa
class TestCheckOutput(unittest.TestCase):
@mock.patch('subprocess.check_output')
def test_check_output(self, out):
"""
As for Python2 (and its last version: 2.7), subprocess.check_output
always return string like objects, contrary to bytes - no conversion
to string is needed.
Python3 treats string as unicode objects, but subprocess.check_output
returns bytes object, which is equvalend for py2 string… annoying.
"""
out.return_value = bytes(LISTING, 'utf-8')
result = adbfs.check_output(None)
self.assertEqual(result, LISTING)
@mock.patch('subprocess.check_output')
def test_check_output_py3_invalid_char(self, out):
"""
Special case for py3. We have bytes with some weird character - like
some system write something with codepage, instead of utf8.
"""
line = (b'-rw-rw---- 1 0 1015 0 01/01/2010 22:11:01 '
b'/storage/emulated/0/\xe2\n') # Latin 1 char â
out.return_value = bytes(line)
result = adbfs.check_output(None)
self.assertEqual(result, line.decode('iso-8859-1'))
if __name__ == "__main__":
unittest.main()