From bf758d6489f9d4c6a06ca58aea37bf457862194f Mon Sep 17 00:00:00 2001 From: zhaoye Date: Mon, 27 May 2019 17:05:12 +0800 Subject: [PATCH] fix bug - support iPhoneX (#23) * fix bug * fix codacy issue --- debug.py | 9 +++++++-- lyrebird_ios/device_service.py | 26 +++++++++++++------------- lyrebird_ios/ios_helper.py | 21 ++++++++++++++++----- setup.py | 4 ++-- 4 files changed, 38 insertions(+), 22 deletions(-) diff --git a/debug.py b/debug.py index 9ea19d8..a0c97c7 100644 --- a/debug.py +++ b/debug.py @@ -3,5 +3,10 @@ if __name__ == '__main__': - pip.main(['install', '.', '--upgrade']) - lyrebird.debug() + version_num = pip.__version__[:pip.__version__.find('.')] + if int(version_num) >= 10: + from pip import __main__ + __main__._main(['install', '.', '--upgrade']) + else: + pip.main(['install', '.', '--upgrade']) + lyrebird.main() diff --git a/lyrebird_ios/device_service.py b/lyrebird_ios/device_service.py index cb8769c..cd60ae4 100644 --- a/lyrebird_ios/device_service.py +++ b/lyrebird_ios/device_service.py @@ -21,7 +21,6 @@ def __init__(self): self.handle_interval = 1 self.devices = {} self.reset_screenshot_dir() - print('DeviceService OnCreate') def devices_to_dict(self): json_obj = {} @@ -31,7 +30,6 @@ def devices_to_dict(self): def run(self): self.status = self.RUNNING - print('iOS device listener start') while self.status == self.RUNNING: try: self.handle() @@ -39,7 +37,6 @@ def run(self): except Exception as e: _log.error(e) self.status = self.STOP - print('iOS device listener stop') def handle(self): devices = ios_helper.devices() @@ -62,7 +59,6 @@ def publish_devices_info_event(self, online_devices, app_name): devices = [] for item in online_devices: device_info = online_devices[item] - app_info = online_devices[item].get_app_info(app_name) message_info = { 'id': device_info.device_id, 'info': { @@ -72,14 +68,19 @@ def publish_devices_info_event(self, online_devices, app_name): 'sn': device_info.sn }, } - if app_info.get('AppName'): - message_info['app'] = { - 'name': app_info['AppName'], - 'version': app_info['VersionNumber'], - 'build': app_info['BuildNumber'], - 'bundleID': app_info['BundleID'] - } devices.append(message_info) + try: + app_info = device_info.get_app_info(app_name) + if app_info.get('AppName'): + message_info['app'] = { + 'name': app_info['AppName'], + 'version': app_info['VersionNumber'], + 'build': app_info['BuildNumber'], + 'bundleID': app_info['BundleID'] + } + except Exception: + _log.error('Can\'t read app info') + lyrebird.publish('ios.device', devices, state=True) @staticmethod @@ -87,8 +88,7 @@ def get_default_app_name(): plugin_conf = lyrebird.context.application.conf.get('plugin.ios', {}) default_bundle_id = plugin_conf.get('bundle_id', '') return default_bundle_id - + def reset_screenshot_dir(self): if os.path.exists(ios_helper.screenshot_dir): shutil.rmtree(ios_helper.screenshot_dir) - print('iOS device log file reset') diff --git a/lyrebird_ios/ios_helper.py b/lyrebird_ios/ios_helper.py index 2df371e..6969279 100644 --- a/lyrebird_ios/ios_helper.py +++ b/lyrebird_ios/ios_helper.py @@ -1,4 +1,9 @@ -import os, plistlib, subprocess, threading, codecs, json, time +import os +import plistlib +import subprocess +import codecs +import json +import time import lyrebird from lyrebird import context from lyrebird.log import get_logger @@ -152,6 +157,7 @@ class Device: """ 设备基类,主要属性包含 device_id, model, os_version等,主要方法包括截屏,获取信息等 """ + def __init__(self, device_id): self.device_id = device_id self.model = None @@ -222,7 +228,7 @@ def device_info(self): if not self._device_info: self._device_info = self.get_properties() return self._device_info - + def start_app(self, bundle_id, ip, port): ios_driver.bundle_id = bundle_id ios_driver.environment = { @@ -243,7 +249,7 @@ def stop_app(self): except AttributeError as e: pass return str(e) - return + return def get_properties(self): p = subprocess.run(f'{ideviceinfo} -u {self.device_id}', shell=True, stdout=subprocess.PIPE) @@ -260,7 +266,11 @@ 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) - p = subprocess.Popen(f'{ideviceinstaller} -u {self.device_id} -l -o xml > {plist_path}', shell=True) + if '-' in device_id: + _cmd = f'{ideviceinstaller} -l -o xml > {plist_path}' + else: + _cmd = f'{ideviceinstaller} -u {self.device_id} -l -o xml > {plist_path}' + p = subprocess.Popen(_cmd, shell=True) p.wait() def get_apps_list(self, device_id): @@ -280,7 +290,8 @@ def take_screen_shot(self): file_name = self.model.replace(' ', '_') timestrap = int(time.time()) screen_shot_file = os.path.abspath(os.path.join(screenshot_dir, f'{file_name}_{timestrap}.png')) - p = subprocess.run(f'{idevicescreenshot} -u {self.device_id} {screen_shot_file}', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + p = subprocess.run(f'{idevicescreenshot} -u {self.device_id} {screen_shot_file}', + shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) err_str = p.stdout.decode() if p.returncode == 0: return dict({ diff --git a/setup.py b/setup.py index 23a4ec2..875b5a1 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setup( name='lyrebird-ios', - version='0.2.4', + version='0.2.5', packages=['lyrebird_ios'], url='https://github.com/meituan/lyrebird-ios', author='HBQA', @@ -28,7 +28,7 @@ 'lyrebird_web': [ 'iOS = lyrebird_ios.ui:MyUI' ] - }, + }, install_requires=[ 'lyrebird', 'facebook-wda'