1
0
mirror of https://github.com/gryf/mc_adbfs.git synced 2025-12-18 20:10:21 +01:00

Added Python3 compatibility

This commit is contained in:
2018-03-11 16:09:22 +01:00
parent 755ce62321
commit e4a4aa8974
2 changed files with 33 additions and 22 deletions

View File

@@ -8,7 +8,7 @@ This is Midnight Commander extfs plugin for browsing Android device through
Rquirements Rquirements
=========== ===========
* Python 2.7 * Python 2.7 or 3.x (tested on 3.5.4)
* ``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

53
adbfs
View File

@@ -5,8 +5,10 @@ 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 import ConfigParser as configparser
except ImportError:
import configparser
import argparse import argparse
from datetime import datetime from datetime import datetime
import json import json
@@ -15,11 +17,23 @@ import re
import subprocess import subprocess
import sys import sys
__version__ = 0.10 __version__ = 0.11
XDG_CONFIG_HOME = os.getenv('XDG_CONFIG_HOME', os.path.expanduser('~/.config')) XDG_CONFIG_HOME = os.getenv('XDG_CONFIG_HOME', os.path.expanduser('~/.config'))
def check_output(command_list, stderr=None):
"""
For some reason, in py3 it was decided that command output should be bytes
instead of string. This little function will check if we have string or
bytes and in case of bytes it will convert it to string.
"""
result = subprocess.check_output(command_list, stderr=stderr)
if not isinstance(result, str):
result = result.decode('utf-8')
return result
class NoBoxFoundException(OSError): class NoBoxFoundException(OSError):
""" """
Exception raised in case of not found either toolbox or busybox on remote Exception raised in case of not found either toolbox or busybox on remote
@@ -78,8 +92,7 @@ class Conf(object):
cmd = [self.adb_command] + 'shell which'.split() cmd = [self.adb_command] + 'shell which'.split()
try: try:
with open(os.devnull, 'w') as fnull: with open(os.devnull, 'w') as fnull:
result = subprocess.check_output(cmd + ['busybox'], result = check_output(cmd + ['busybox'], stderr=fnull)
stderr=fnull)
if 'busybox' in result: if 'busybox' in result:
self.box = Conf.boxes['busybox'] self.box = Conf.boxes['busybox']
if self.suppress_colors: if self.suppress_colors:
@@ -93,8 +106,7 @@ class Conf(object):
try: try:
with open(os.devnull, 'w') as fnull: with open(os.devnull, 'w') as fnull:
result = subprocess.check_output(cmd + ['toybox'], result = check_output(cmd + ['toybox'], stderr=fnull)
stderr=fnull)
if 'toybox' in result: if 'toybox' in result:
self.box = Conf.boxes['toybox'] self.box = Conf.boxes['toybox']
@@ -105,8 +117,7 @@ class Conf(object):
try: try:
with open(os.devnull, 'w') as fnull: with open(os.devnull, 'w') as fnull:
result = subprocess.check_output(cmd + ['toolbox'], result = check_output(cmd + ['toolbox'], stderr=fnull)
stderr=fnull)
if 'toolbox' in result: if 'toolbox' in result:
self.box = Conf.boxes['toolbox'] self.box = Conf.boxes['toolbox']
@@ -125,7 +136,7 @@ class Conf(object):
try: try:
with open(os.devnull, 'w') as fnull: with open(os.devnull, 'w') as fnull:
result = subprocess.check_output(cmd, stderr=fnull) result = check_output(cmd, stderr=fnull)
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
result = '' result = ''
@@ -157,7 +168,7 @@ class Conf(object):
cmd = [self.adb_command, 'connect', self.adb_connect] cmd = [self.adb_command, 'connect', self.adb_connect]
with open(os.devnull, 'w') as fnull: with open(os.devnull, 'w') as fnull:
result = subprocess.check_output(cmd, stderr=fnull) result = check_output(cmd, stderr=fnull)
if result.split()[0] == 'connected': if result.split()[0] == 'connected':
subprocess.call([self.adb_command, 'wait-for-device']) subprocess.call([self.adb_command, 'wait-for-device'])
return return
@@ -178,7 +189,7 @@ class Conf(object):
if not os.path.exists(conf_fname): if not os.path.exists(conf_fname):
return return
cfg = ConfigParser.SafeConfigParser() cfg = configparser.SafeConfigParser()
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'),
@@ -190,7 +201,7 @@ class Conf(object):
for key, (function, attr) in cfg_map.items(): for key, (function, attr) in cfg_map.items():
try: try:
setattr(self, attr, function('adbfs', key)) setattr(self, attr, function('adbfs', key))
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): except (configparser.NoSectionError, configparser.NoOptionError):
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):
@@ -331,7 +342,7 @@ class Adb(object):
cmd = [self.conf.adb_command] + 'shell su -c whoami'.split() cmd = [self.conf.adb_command] + 'shell su -c whoami'.split()
try: try:
with open(os.devnull, 'w') as fnull: with open(os.devnull, 'w') as fnull:
result = subprocess.check_output(cmd, stderr=fnull) result = check_output(cmd, stderr=fnull)
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
return return
@@ -385,9 +396,9 @@ class Adb(object):
try: try:
if self.conf.debug: if self.conf.debug:
print 'executing', ' '.join(command) print('executing', ' '.join(command))
lines = subprocess.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 1
@@ -447,9 +458,9 @@ class Adb(object):
try: try:
if self.conf.debug: if self.conf.debug:
print 'executing', ' '.join(command) print('executing', ' '.join(command))
lines = subprocess.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 1
@@ -557,7 +568,7 @@ class Adb(object):
cmd = [self.conf.adb_command, 'shell', 'rm', dst] cmd = [self.conf.adb_command, 'shell', 'rm', dst]
try: try:
err = subprocess.check_output(cmd) err = check_output(cmd)
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
sys.stderr.write('Error executing adb shell') sys.stderr.write('Error executing adb shell')
return 1 return 1
@@ -575,7 +586,7 @@ class Adb(object):
cmd = [self.conf.adb_command, 'shell', 'rm', '-r', dst] cmd = [self.conf.adb_command, 'shell', 'rm', '-r', dst]
try: try:
err = subprocess.check_output(cmd) err = check_output(cmd)
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
sys.stderr.write('Error executing adb shell') sys.stderr.write('Error executing adb shell')
return 1 return 1
@@ -596,7 +607,7 @@ class Adb(object):
cmd = [self.conf.adb_command, 'shell', 'mkdir', dst] cmd = [self.conf.adb_command, 'shell', 'mkdir', dst]
try: try:
err = subprocess.check_output(cmd) err = check_output(cmd)
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
sys.stderr.write('Error executing adb shell') sys.stderr.write('Error executing adb shell')
return 1 return 1