mirror of
https://github.com/gryf/mc_adbfs.git
synced 2026-03-27 22:03:34 +01:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1c6a6cfdf8 | |||
| 7ce2dd2568 | |||
| 11f980beb1 | |||
| bc742ccdf6 | |||
| 7a6a8a499b | |||
| b2163e0fba | |||
| 57509eaac0 |
12
README.rst
12
README.rst
@@ -9,7 +9,7 @@ Rquirements
|
||||
===========
|
||||
|
||||
* Python 2.7
|
||||
* ``adb`` installed and in ``$PATH``
|
||||
* ``adb`` installed and in ``$PATH`` or provided via the config file
|
||||
* An Android device or emulator preferably rooted
|
||||
* Busybox installed and available in the path on the device
|
||||
|
||||
@@ -18,6 +18,8 @@ Make sure, that issuing from command line:
|
||||
.. code:: shell-session
|
||||
|
||||
$ adb shell busybox ls
|
||||
$ # or in case of no PATH adb placement
|
||||
$ /path/to/adb shell busybox ls
|
||||
|
||||
it should display files from root directory on the device.
|
||||
|
||||
@@ -55,7 +57,7 @@ your device and how fast it is :)
|
||||
Configuration
|
||||
=============
|
||||
|
||||
You can use configure behaviour of this plugin using ``.ini`` file located under
|
||||
You can configure behaviour of this plugin using ``.ini`` file located under
|
||||
``$XDG_CONFIG_HOME/mc/adbfs.ini`` (which usually is located under
|
||||
``~/.config/mc/adbfs.ini``), and have default values, like:
|
||||
|
||||
@@ -65,7 +67,9 @@ You can use configure behaviour of this plugin using ``.ini`` file located under
|
||||
debug = false
|
||||
skip_dirs = true
|
||||
dirs_to_skip = ["acct", "charger", "d", "dev", "proc", "sys"]
|
||||
suppress_colors = false
|
||||
root =
|
||||
adb_command = adb
|
||||
|
||||
where:
|
||||
|
||||
@@ -74,9 +78,13 @@ where:
|
||||
* ``dirs_to_skip`` list of paths to directories which will be skipped during
|
||||
reading. If leaved empty, or setted to empty list (``[]``) will read
|
||||
everything (slow!)
|
||||
* ``suppress_colors`` this option will make ``busybox`` not to display colors,
|
||||
helpful, if ``busybox ls`` is configured to display colors by default. Does
|
||||
not affect ``toolbox``.
|
||||
* ``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.
|
||||
|
||||
Limitations
|
||||
===========
|
||||
|
||||
203
adbfs
203
adbfs
@@ -16,9 +16,9 @@ import re
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
__version__ = 0.8
|
||||
__version__ = 0.10
|
||||
|
||||
XDG_CONFIG_HOME = os.getenv("XDG_CONFIG_HOME", os.path.expanduser("~/.config"))
|
||||
XDG_CONFIG_HOME = os.getenv('XDG_CONFIG_HOME', os.path.expanduser('~/.config'))
|
||||
|
||||
|
||||
class NoBoxFoundException(OSError):
|
||||
@@ -28,6 +28,7 @@ class NoBoxFoundException(OSError):
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class Conf(object):
|
||||
"""Simple config parser"""
|
||||
boxes = {'busybox': {'ls': 'busybox ls -anel',
|
||||
@@ -48,37 +49,40 @@ class Conf(object):
|
||||
r'(?P<size>\d+)?\s'
|
||||
r'(?P<date>\d{4}-\d{2}-\d{2}\s'
|
||||
r'\d{2}:\d{2})\s'
|
||||
r'(?P<name>.*)'}
|
||||
}
|
||||
r'(?P<name>.*)'}}
|
||||
|
||||
def __init__(self):
|
||||
self.box = None
|
||||
self.debug = False
|
||||
self.dirs_to_skip = ["acct", "charger", "d", "dev", "proc", "sys"]
|
||||
self.dirs_to_skip = ['acct', 'charger', 'd', 'dev', 'proc', 'sys']
|
||||
self.root = None
|
||||
self.suppress_colors = False
|
||||
self.adb_command = 'adb'
|
||||
|
||||
self.get_the_box()
|
||||
self.read()
|
||||
self.get_the_box()
|
||||
|
||||
def get_the_box(self):
|
||||
"""Detect if we dealing with busybox or toolbox"""
|
||||
cmd = [self.adb_command] + 'shell which'.split()
|
||||
try:
|
||||
with open(os.devnull, "w") as fnull:
|
||||
result = subprocess.check_output('adb shell which '
|
||||
'busybox'.split(),
|
||||
with open(os.devnull, 'w') as fnull:
|
||||
result = subprocess.check_output(cmd + ['busybox'],
|
||||
stderr=fnull)
|
||||
|
||||
if 'busybox' in result:
|
||||
self.box = Conf.boxes['busybox']
|
||||
if self.suppress_colors:
|
||||
self.box.update({'ls': 'busybox ls --color=none -anel',
|
||||
'rls': 'busybox ls --color=none '
|
||||
'-Ranel {}'})
|
||||
Adb.file_re = re.compile(self.box['file_re'])
|
||||
return
|
||||
except subprocess.CalledProcessError:
|
||||
pass
|
||||
|
||||
try:
|
||||
with open(os.devnull, "w") as fnull:
|
||||
result = subprocess.check_output('adb shell which '
|
||||
'toolbox'.split(),
|
||||
with open(os.devnull, 'w') as fnull:
|
||||
result = subprocess.check_output(cmd + ['toolbox'],
|
||||
stderr=fnull)
|
||||
|
||||
if 'toolbox' in result:
|
||||
@@ -89,7 +93,7 @@ class Conf(object):
|
||||
pass
|
||||
|
||||
raise NoBoxFoundException(errno.ENOENT,
|
||||
"There is no toolbox or busybox available")
|
||||
'There is no toolbox or busybox available')
|
||||
|
||||
def read(self):
|
||||
"""
|
||||
@@ -106,7 +110,9 @@ class Conf(object):
|
||||
cfg = ConfigParser.SafeConfigParser()
|
||||
cfg_map = {'debug': (cfg.getboolean, 'debug'),
|
||||
'dirs_to_skip': (cfg.get, 'dirs_to_skip'),
|
||||
'root': (cfg.get, 'root')}
|
||||
'suppress_colors': (cfg.get, 'suppress_colors'),
|
||||
'root': (cfg.get, 'root'),
|
||||
'adb_command': (cfg.get, 'adb')}
|
||||
cfg.read(conf_fname)
|
||||
|
||||
for key, (function, attr) in cfg_map.items():
|
||||
@@ -116,11 +122,15 @@ class Conf(object):
|
||||
pass
|
||||
|
||||
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, encoding='ascii')
|
||||
self.dirs_to_skip = [x.encode('utf-8') for x in self.dirs_to_skip]
|
||||
else:
|
||||
self.dirs_to_skip = []
|
||||
|
||||
if self.adb_command:
|
||||
self.adb_command = os.path.expandvars(self.adb_command)
|
||||
self.adb_command = os.path.expanduser(self.adb_command)
|
||||
|
||||
|
||||
class File(object):
|
||||
"""Item in filesystem representation"""
|
||||
@@ -136,7 +146,7 @@ class File(object):
|
||||
self.name = name
|
||||
self.date = date # as string
|
||||
|
||||
self.dirname = ""
|
||||
self.dirname = ''
|
||||
self.type = None
|
||||
self.string = None
|
||||
self.link_target = None
|
||||
@@ -145,7 +155,7 @@ class File(object):
|
||||
def _correct_link(self):
|
||||
"""Canonize filename and fill the link attr"""
|
||||
try:
|
||||
name, target = self.name.split(" -> ")
|
||||
name, target = self.name.split(' -> ')
|
||||
except ValueError:
|
||||
return
|
||||
|
||||
@@ -154,7 +164,7 @@ class File(object):
|
||||
if not self.size:
|
||||
self.size = 0
|
||||
|
||||
if target.startswith("/"):
|
||||
if target.startswith('/'):
|
||||
self.link_target = target
|
||||
else:
|
||||
self.link_target = os.path.abspath(os.path.join(self.dirname,
|
||||
@@ -162,34 +172,34 @@ class File(object):
|
||||
|
||||
def update(self, dirname):
|
||||
"""update object fields"""
|
||||
month_num = {"Jan": 1,
|
||||
"Feb": 2,
|
||||
"Mar": 3,
|
||||
"Apr": 4,
|
||||
"May": 5,
|
||||
"Jun": 6,
|
||||
"Jul": 7,
|
||||
"Aug": 8,
|
||||
"Sep": 9,
|
||||
"Oct": 10,
|
||||
"Nov": 11,
|
||||
"Dec": 12}
|
||||
month_num = {'Jan': 1,
|
||||
'Feb': 2,
|
||||
'Mar': 3,
|
||||
'Apr': 4,
|
||||
'May': 5,
|
||||
'Jun': 6,
|
||||
'Jul': 7,
|
||||
'Aug': 8,
|
||||
'Sep': 9,
|
||||
'Oct': 10,
|
||||
'Nov': 11,
|
||||
'Dec': 12}
|
||||
self.dirname = dirname
|
||||
if self.date_time:
|
||||
date = self.date_time.split()
|
||||
date = "%s-%02d-%s %s" % (date[1],
|
||||
date = '%s-%02d-%s %s' % (date[1],
|
||||
month_num[date[0]],
|
||||
date[3],
|
||||
date[2])
|
||||
date = datetime.strptime(date, "%d-%m-%Y %H:%M:%S")
|
||||
date = datetime.strptime(date, '%d-%m-%Y %H:%M:%S')
|
||||
elif self.date:
|
||||
date = datetime.strptime(self.date, "%Y-%m-%d %H:%M")
|
||||
date = datetime.strptime(self.date, '%Y-%m-%d %H:%M')
|
||||
|
||||
self.date_time = date.strftime("%m/%d/%Y %H:%M:01")
|
||||
self.date_time = date.strftime('%m/%d/%Y %H:%M:01')
|
||||
|
||||
self.type = self.perms[0] if self.perms else None
|
||||
|
||||
if self.type == "l" and " -> " in self.name:
|
||||
if self.type == 'l' and ' -> ' in self.name:
|
||||
self._correct_link()
|
||||
|
||||
self.filepath = os.path.join(self.dirname, self.name)
|
||||
@@ -202,22 +212,22 @@ class File(object):
|
||||
"""represent the file/entire node"""
|
||||
fullname = os.path.join(self.dirname, self.name)
|
||||
if self.link_target:
|
||||
fullname += " -> " + self.link_target
|
||||
return "<File {type} {name} {id}>".format(type=self.type,
|
||||
fullname += ' -> ' + self.link_target
|
||||
return '<File {type} {name} {id}>'.format(type=self.type,
|
||||
name=fullname,
|
||||
id=hex(id(self)))
|
||||
|
||||
def __str__(self):
|
||||
"""display the file/entire node"""
|
||||
template = ("{perms} {links:>4} {uid:<8} {gid:<8} {size:>8} "
|
||||
"{date_time} {fullname}\n")
|
||||
template = ('{perms} {links:>4} {uid:<8} {gid:<8} {size:>8} '
|
||||
'{date_time} {fullname}\n')
|
||||
|
||||
if not self.name:
|
||||
return ""
|
||||
return ''
|
||||
|
||||
fullname = os.path.join(self.dirname, self.name)
|
||||
if self.link_target:
|
||||
fullname += " -> " + self.link_target
|
||||
fullname += ' -> ' + self.link_target
|
||||
|
||||
return template.format(perms=self.perms,
|
||||
links=self.links,
|
||||
@@ -231,7 +241,7 @@ class File(object):
|
||||
class Adb(object):
|
||||
"""Class for interact with android rooted device through adb"""
|
||||
file_re = None
|
||||
current_re = re.compile(r"^(\./)?(?P<dir>.+):$")
|
||||
current_re = re.compile(r'^(\./)?(?P<dir>.+):$')
|
||||
|
||||
def __init__(self):
|
||||
"""Prepare archive content for operations"""
|
||||
@@ -246,18 +256,16 @@ class Adb(object):
|
||||
|
||||
def __su_check(self):
|
||||
"""Check if we are able to get elevated privileges"""
|
||||
cmd = [self.conf.adb_command] + 'shell su -c whoami'.split()
|
||||
try:
|
||||
with open(os.devnull, "w") as fnull:
|
||||
result = subprocess.check_output('adb shell su -c '
|
||||
'whoami'.split(),
|
||||
stderr=fnull)
|
||||
with open(os.devnull, 'w') as fnull:
|
||||
result = subprocess.check_output(cmd, stderr=fnull)
|
||||
|
||||
except subprocess.CalledProcessError:
|
||||
return
|
||||
|
||||
if 'root' in result:
|
||||
self._got_root = True
|
||||
return
|
||||
|
||||
def _find_target(self, needle):
|
||||
"""Find link target"""
|
||||
@@ -298,16 +306,18 @@ class Adb(object):
|
||||
|
||||
def _retrieve_single_dir_list(self, dir_):
|
||||
"""Retrieve file list using adb"""
|
||||
command = ["adb", "shell", "su", "-c"]
|
||||
command.append(self.conf.box['rls'].format(dir_))
|
||||
lscmd = self.conf.box['rls'].format(dir_)
|
||||
if self._got_root:
|
||||
lscmd = 'su -c "{}"'.format(lscmd)
|
||||
command = [self.conf.adb_command, 'shell', lscmd]
|
||||
|
||||
try:
|
||||
if self.conf.debug:
|
||||
print "executing", " ".join(command)
|
||||
print 'executing', ' '.join(command)
|
||||
|
||||
lines = subprocess.check_output(command)
|
||||
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
|
||||
|
||||
lines = [l.strip() for l in lines.split('\n') if l.strip()]
|
||||
@@ -320,16 +330,16 @@ class Adb(object):
|
||||
return
|
||||
|
||||
self._entries.append(entry)
|
||||
if entry.type == "l":
|
||||
if entry.type == 'l':
|
||||
self._links[entry.filepath] = entry
|
||||
self._retrieve_single_dir_list(entry.link_target)
|
||||
else:
|
||||
for line in lines:
|
||||
current_dir_re = self.current_re.match(line)
|
||||
if current_dir_re:
|
||||
current_dir = current_dir_re.groupdict()["dir"]
|
||||
current_dir = current_dir_re.groupdict()['dir']
|
||||
if not current_dir:
|
||||
current_dir = "/"
|
||||
current_dir = '/'
|
||||
continue
|
||||
|
||||
reg_match = self.file_re.match(line)
|
||||
@@ -337,7 +347,7 @@ class Adb(object):
|
||||
continue
|
||||
|
||||
entry = File(**reg_match.groupdict())
|
||||
if entry.name in (".", ".."):
|
||||
if entry.name in ('.', '..'):
|
||||
continue
|
||||
|
||||
entry.update(current_dir)
|
||||
@@ -347,35 +357,39 @@ class Adb(object):
|
||||
|
||||
self._entries.append(entry)
|
||||
|
||||
if entry.type == "l":
|
||||
if entry.type == 'l':
|
||||
self._links[entry.filepath] = entry
|
||||
|
||||
def _retrieve_file_list(self, root=None):
|
||||
"""Retrieve file list using adb"""
|
||||
command = ["adb", "shell", "su", "-c"]
|
||||
|
||||
if not root:
|
||||
command.append(self.conf.box['ls'])
|
||||
lscmd = self.conf.box['ls']
|
||||
else:
|
||||
command.append(self.conf.box['rls'].format(root.filepath))
|
||||
lscmd = self.conf.box['rls'].format(root.filepath)
|
||||
|
||||
if self._got_root:
|
||||
lscmd = 'su -c "{}"'.format(lscmd)
|
||||
|
||||
command = [self.conf.adb_command, 'shell', lscmd]
|
||||
|
||||
try:
|
||||
if self.conf.debug:
|
||||
print "executing", " ".join(command)
|
||||
print 'executing', ' '.join(command)
|
||||
|
||||
lines = subprocess.check_output(command)
|
||||
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
|
||||
|
||||
current_dir = root.dirname if root else "/"
|
||||
for line in lines.split("\n"):
|
||||
current_dir = root.dirname if root else '/'
|
||||
for line in lines.split('\n'):
|
||||
line = line.strip()
|
||||
current_dir_re = self.current_re.match(line)
|
||||
if current_dir_re:
|
||||
current_dir = current_dir_re.groupdict()["dir"]
|
||||
current_dir = current_dir_re.groupdict()['dir']
|
||||
if not current_dir:
|
||||
current_dir = "/"
|
||||
current_dir = '/'
|
||||
continue
|
||||
|
||||
reg_match = self.file_re.match(line)
|
||||
@@ -383,7 +397,7 @@ class Adb(object):
|
||||
continue
|
||||
|
||||
entry = File(**reg_match.groupdict())
|
||||
if entry.name in (".", ".."):
|
||||
if entry.name in ('.', '..'):
|
||||
continue
|
||||
|
||||
entry.update(current_dir)
|
||||
@@ -392,16 +406,16 @@ class Adb(object):
|
||||
continue
|
||||
|
||||
self._entries.append(entry)
|
||||
if root is None and entry.type == "d":
|
||||
if root is None and entry.type == 'd':
|
||||
self._retrieve_file_list(entry)
|
||||
|
||||
if entry.type == "l":
|
||||
if entry.type == 'l':
|
||||
self._links[entry.filepath] = entry
|
||||
|
||||
def run(self, fname):
|
||||
"""Not supported"""
|
||||
sys.stderr.write("Not supported - or maybe you are on compatible "
|
||||
"architecture?\n")
|
||||
sys.stderr.write('Not supported - or maybe you are on compatible '
|
||||
'architecture?\n')
|
||||
return 1
|
||||
|
||||
def list(self):
|
||||
@@ -416,13 +430,7 @@ class Adb(object):
|
||||
self._retrieve_file_list()
|
||||
|
||||
self._normalize_links()
|
||||
# with open(os.path.join(os.path.dirname(os.path.realpath(__file__)),
|
||||
# # "list.pcl"), "w") as fob:
|
||||
# "list.pcl")) as fob:
|
||||
# import cPickle
|
||||
# # cPickle.dump(self._entries, fob)
|
||||
# self._entries = cPickle.load(fob)
|
||||
sys.stdout.write("".join([str(entry) for entry in self._entries]))
|
||||
sys.stdout.write(''.join([str(entry) for entry in self._entries]))
|
||||
return 0
|
||||
|
||||
def copyout(self, src, dst):
|
||||
@@ -431,11 +439,11 @@ class Adb(object):
|
||||
sys.stderr.write(self.error)
|
||||
return 1
|
||||
|
||||
cmd = ["adb", "pull", src, dst]
|
||||
cmd = [self.conf.adb_command, 'pull', src, dst]
|
||||
if self.conf.debug:
|
||||
sys.stderr.write(" ".join(cmd) + "\n")
|
||||
sys.stderr.write(' '.join(cmd) + '\n')
|
||||
|
||||
with open(os.devnull, "w") as fnull:
|
||||
with open(os.devnull, 'w') as fnull:
|
||||
try:
|
||||
err = subprocess.call(cmd, stdout=fnull, stderr=fnull)
|
||||
except subprocess.CalledProcessError:
|
||||
@@ -449,15 +457,14 @@ class Adb(object):
|
||||
if self.error:
|
||||
sys.stderr.write(self.error)
|
||||
return 1
|
||||
if not dst.startswith("/"):
|
||||
dst = "/" + dst
|
||||
if not dst.startswith('/'):
|
||||
dst = '/' + dst
|
||||
|
||||
# cmd = ["adb", "push", pipes.quote(src), pipes.quote(dst)]
|
||||
cmd = ["adb", "push", src, dst]
|
||||
cmd = [self.conf.adb_command, 'push', src, dst]
|
||||
if self.conf.debug:
|
||||
sys.stderr.write(" ".join(cmd) + "\n")
|
||||
sys.stderr.write(' '.join(cmd) + '\n')
|
||||
|
||||
with open(os.devnull, "w") as fnull:
|
||||
with open(os.devnull, 'w') as fnull:
|
||||
try:
|
||||
err = subprocess.call(cmd, stdout=fnull, stderr=fnull)
|
||||
except subprocess.CalledProcessError:
|
||||
@@ -465,8 +472,8 @@ class Adb(object):
|
||||
return 1
|
||||
|
||||
if err != 0:
|
||||
sys.stderr.write("Cannot push the file, "
|
||||
"%s, error %d" % (dst, err))
|
||||
sys.stderr.write('Cannot push the file, '
|
||||
'%s, error %d' % (dst, err))
|
||||
return 1
|
||||
return 0
|
||||
|
||||
@@ -476,14 +483,14 @@ class Adb(object):
|
||||
sys.stderr.write(self.error)
|
||||
return 1
|
||||
|
||||
cmd = ["adb", "shell", "rm", dst]
|
||||
cmd = [self.conf.adb_command, 'shell', 'rm', dst]
|
||||
try:
|
||||
err = subprocess.check_output(cmd)
|
||||
except subprocess.CalledProcessError:
|
||||
sys.stderr.write('Error executing adb shell')
|
||||
return 1
|
||||
|
||||
if err != "":
|
||||
if err != '':
|
||||
sys.stderr.write(err)
|
||||
return 1
|
||||
return 0
|
||||
@@ -494,14 +501,14 @@ class Adb(object):
|
||||
sys.stderr.write(self.error)
|
||||
return 1
|
||||
|
||||
cmd = ["adb", "shell", "rm", "-r", dst]
|
||||
cmd = [self.conf.adb_command, 'shell', 'rm', '-r', dst]
|
||||
try:
|
||||
err = subprocess.check_output(cmd)
|
||||
except subprocess.CalledProcessError:
|
||||
sys.stderr.write('Error executing adb shell')
|
||||
return 1
|
||||
|
||||
if err != "":
|
||||
if err != '':
|
||||
sys.stderr.write(err)
|
||||
return 1
|
||||
return 0
|
||||
@@ -512,17 +519,17 @@ class Adb(object):
|
||||
sys.stderr.write(self.error)
|
||||
return 1
|
||||
|
||||
if not dst.startswith("/"):
|
||||
dst = "/" + dst
|
||||
if not dst.startswith('/'):
|
||||
dst = '/' + dst
|
||||
|
||||
cmd = ["adb", "shell", "mkdir", dst]
|
||||
cmd = [self.conf.adb_command, 'shell', 'mkdir', dst]
|
||||
try:
|
||||
err = subprocess.check_output(cmd)
|
||||
except subprocess.CalledProcessError:
|
||||
sys.stderr.write('Error executing adb shell')
|
||||
return 1
|
||||
|
||||
if err != "":
|
||||
if err != '':
|
||||
sys.stderr.write(err)
|
||||
return 1
|
||||
return 0
|
||||
@@ -586,5 +593,5 @@ def main():
|
||||
return args.func(args)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
|
||||
Reference in New Issue
Block a user