Skip to content

Commit

Permalink
Feature/update-env-check (#38)
Browse files Browse the repository at this point in the history
* Update ideviceinstaller; Add iPhone13 modal

* Update ideviceinstaller; Add iPhone13 modal

* update version code

* update env check

* remove useless ideviceinstaller

* solve conflict

* remove useless exception

* add IdeviceinstallerError

* remove return warning_message

* update code

* remove useless code

* remove png

* update code

* update traceback

* add tempfile
  • Loading branch information
yumiguan authored Nov 13, 2020
1 parent 05b4de4 commit 25c022d
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 28 deletions.
File renamed without changes.
Binary file added lyrebird_ios/bin/1.3.0/ideviceinstaller
Binary file not shown.
6 changes: 5 additions & 1 deletion lyrebird_ios/config/comparison_table_model.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,9 @@
"iPhone11,8": "iPhone XR",
"iPhone12,1": "iPhone 11",
"iPhone12,3": "iPhone 11 Pro",
"iPhone12,5": "iPhone 11 Pro Max"
"iPhone12,5": "iPhone 11 Pro Max",
"iPhone13,1": "iPhone 12 mini",
"iPhone13,2": "iPhone 12",
"iPhone13,3": "iPhone 12 Pro",
"iPhone13,4": "iPhone 12 Pro Max"
}
16 changes: 8 additions & 8 deletions lyrebird_ios/device_service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import shutil
import lyrebird
import traceback
from . import ios_helper
from lyrebird import context
from lyrebird.log import get_logger
Expand All @@ -23,14 +24,13 @@ def __init__(self):
self.reset_screenshot_dir()

def check_env(self):
error_message = ios_helper.check_environment()
if not error_message:
try:
ios_helper.check_environment()
self.status = self.RUNNING
_log.debug('iOS device listener start')
else:
except Exception:
self.status = self.STOP
_log.error(error_message)
return error_message
_log.error(f'iOS plugin stoped!\n {traceback.format_exc()}')

def devices_to_dict(self):
json_obj = {}
Expand All @@ -44,8 +44,8 @@ def run(self):
try:
self.handle()
context.application.socket_io.sleep(self.handle_interval)
except Exception as e:
_log.error(e)
except Exception:
_log.error(f'iOS plugin error!\n {traceback.format_exc()}')
self.status = self.STOP

def handle(self):
Expand Down Expand Up @@ -89,7 +89,7 @@ def publish_devices_info_event(online_devices, app_name):
'packageName': app_info['BundleID']
}
except Exception:
_log.error('Can\'t read app info')
_log.error(f'Read app info error!\n {traceback.format_exc()}')

lyrebird.publish('ios.device', devices, state=True)

Expand Down
101 changes: 83 additions & 18 deletions lyrebird_ios/ios_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
import time
import codecs
import plistlib
import tempfile
import subprocess
from pathlib import Path
from packaging import version
import lyrebird
from lyrebird.log import get_logger
from . import wda_helper
from pathlib import Path

_log = get_logger()

ideviceinstaller = Path(__file__).parent/'bin'/'ideviceinstaller'
ideviceinstaller = None
idevice_id = None
idevicescreenshot = None
ideviceinfo = None
Expand All @@ -22,6 +24,7 @@
screenshot_dir = os.path.abspath(os.path.join(storage, 'screenshot'))

PLIST_PATH = os.path.join(storage, 'plist')
SYSTEM_BIN = Path('/usr/local/bin')

ios_driver = wda_helper.Helper()

Expand All @@ -30,22 +33,66 @@ def check_environment():
检查用户环境,第三方依赖是否正确安装。
:return:
"""
global idevice_id, idevicescreenshot, ideviceinfo

if not os.path.exists('/usr/local/bin/ideviceinfo'):
return 'ideviceinfo command not found, check your libimobiledevice'
else:
p = subprocess.Popen('/usr/local/bin/ideviceinfo', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
err = p.stderr.read().decode()
if len(err):
return f'Something wrong with the ideviceinfo program: {err}'

if not os.path.exists('/usr/local/bin/idevicescreenshot'):
return 'ideviceinfo command not found, check your libimobiledevice'

idevice_id = '/usr/local/bin/idevice_id'
ideviceinfo = '/usr/local/bin/ideviceinfo'
idevicescreenshot = '/usr/local/bin/idevicescreenshot'
global ideviceinstaller, idevice_id, idevicescreenshot, ideviceinfo

# Check libmobiledevice, action when unavailable : block
p = subprocess.run('brew info --json libimobiledevice', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
code, output, err_str = p.returncode, p.stdout.decode(), p.stderr.decode()
if err_str or code or not output:
raise LibmobiledeviceError(f'Get libmobiledevice info error: {err_str}')

libimobiledevice_info = json.loads(output)
if not isinstance(libimobiledevice_info, list) and not len(libimobiledevice_info):
raise LibmobiledeviceError(f'Get unknown libmobiledevice info: {output}')

# Check idevice_id, action when unavailable : block
idevice_id_keywords = 'idevice_id'
idevice_id = SYSTEM_BIN/idevice_id_keywords
err_msg = check_environment_item(idevice_id_keywords, idevice_id)
if err_msg:
idevice_id = None
raise IdeviceidError(err_msg)

# Check ideviceinfo, action when unavailable : block
ideviceinfo_keywords = 'ideviceinfo'
ideviceinfo = SYSTEM_BIN/ideviceinfo_keywords
err_msg = check_environment_item(ideviceinfo_keywords, ideviceinfo)
if err_msg:
ideviceinfo = None
raise IdeviceinfoError(err_msg)

env_err_msg = []

# Check ideviceinstaller, action when unavailable : warning
lib_version = libimobiledevice_info[0].get('versions', {}).get('stable')
lib_version = '1.2.0' if version.parse(lib_version) < version.parse('1.3.0') else '1.3.0'

ideviceinstaller_keywords = 'ideviceinstaller'
ideviceinstaller = Path(__file__).parent/'bin'/lib_version/ideviceinstaller_keywords
err_msg = check_environment_item(ideviceinstaller_keywords, ideviceinstaller)
if err_msg:
env_err_msg.append(err_msg)
ideviceinstaller = None

# Check idevicescreenshot, action when unavailable : warning
idevicescreenshot_keywords = 'idevicescreenshot'
idevicescreenshot = SYSTEM_BIN/idevicescreenshot_keywords
temp_file = tempfile.NamedTemporaryFile().name
err_msg = check_environment_item(idevicescreenshot_keywords, idevicescreenshot, sub_command=temp_file)
if err_msg:
env_err_msg.append(err_msg)
idevicescreenshot = None

if env_err_msg:
_log.error('iOS Plugin environment warning:\n' + '.\n'.join(env_err_msg))

def check_environment_item(command, path, sub_command=''):
if not Path(path).exists():
return f'Command `{command}` not found, check your libimobiledevice'

p = subprocess.run(f'{str(path)} {sub_command}', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
err_str = p.stderr.decode()
return f'Execute command `{command}` error: {err_str}' if err_str else ''

def read_plist(plist_path):
return plistlib.readPlist(plist_path)
Expand Down Expand Up @@ -217,6 +264,8 @@ def get_device_plist(self, device_id):
plist_path = '%s/%s.plist' % (PLIST_PATH, self.device_id)
if not os.path.exists(PLIST_PATH):
os.mkdir(PLIST_PATH)
if not ideviceinstaller:
raise IdeviceinstallerError('Command `ideviceinstaller` is not ready! Check your libimobiledevice')
_cmd = f'{ideviceinstaller} -u {self.device_id} -l -o xml > {plist_path}'
p = subprocess.Popen(_cmd, shell=True)
p.wait()
Expand Down Expand Up @@ -280,3 +329,19 @@ def devices():
online_devices[device.device_id] = device

return online_devices


class LibmobiledeviceError(Exception):
pass


class IdeviceinstallerError(Exception):
pass


class IdeviceidError(Exception):
pass


class IdeviceinfoError(Exception):
pass
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name='lyrebird-ios',
version='0.3.2',
version='0.3.3',
packages=['lyrebird_ios'],
url='https://github.com/meituan/lyrebird-ios',
author='HBQA',
Expand Down

0 comments on commit 25c022d

Please sign in to comment.