diff --git a/lyrebird_ios/device_service.py b/lyrebird_ios/device_service.py index 0104cb7..cb8769c 100644 --- a/lyrebird_ios/device_service.py +++ b/lyrebird_ios/device_service.py @@ -1,3 +1,5 @@ +import os +import shutil import lyrebird from lyrebird import context from . import ios_helper @@ -18,6 +20,7 @@ def __init__(self): self.status = self.READY self.handle_interval = 1 self.devices = {} + self.reset_screenshot_dir() print('DeviceService OnCreate') def devices_to_dict(self): @@ -84,3 +87,8 @@ 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 6626f3e..2df371e 100644 --- a/lyrebird_ios/ios_helper.py +++ b/lyrebird_ios/ios_helper.py @@ -19,6 +19,7 @@ tmp_dir = os.path.abspath(os.path.join(storage, 'tmp')) crash_dir = os.path.abspath(os.path.join(storage, 'crash')) +screenshot_dir = os.path.abspath(os.path.join(storage, 'screenshot')) PLIST_PATH = os.path.join(storage, 'plist') error_msg = None @@ -274,11 +275,18 @@ def stop_log(self): self._log_process = None def take_screen_shot(self): + if not os.path.exists(screenshot_dir): + os.makedirs(screenshot_dir) file_name = self.model.replace(' ', '_') - p = subprocess.run(f'{idevicescreenshot} -u {self.device_id} {tmp_dir}/{file_name}.png', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + 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) err_str = p.stdout.decode() if p.returncode == 0: - return os.path.abspath(os.path.join(tmp_dir, '%s.png' % file_name)) + return dict({ + 'screen_shot_file': screen_shot_file, + 'timestrap': timestrap + }) else: _log.error(f'{err_str}') return '' diff --git a/lyrebird_ios/ui.py b/lyrebird_ios/ui.py index 03c7970..d87f731 100644 --- a/lyrebird_ios/ui.py +++ b/lyrebird_ios/ui.py @@ -14,6 +14,7 @@ storage = lyrebird.get_plugin_storage() tmp_dir = os.path.abspath(os.path.join(storage, 'tmp')) anr_dir = os.path.abspath(os.path.join(storage, 'anr')) +screenshot_dir = os.path.abspath(os.path.join(storage, 'screenshot')) if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) @@ -94,31 +95,43 @@ def logcat_start(self, device_id): def take_screen_shot(self, device_id): device = device_service.devices.get(device_id) - img_path = device.take_screen_shot() - if img_path: - return jsonify({'imgUrl': '/ui/plugin/iOS/api/src/screenshot/%s?time=%s' % (device_id, time.time())}) + img_info = device.take_screen_shot() + timestrap = img_info.get('timestrap') + if img_info.get('screen_shot_file'): + test = {'imgUrl': f'/ui/plugin/iOS/api/src/screenshot/{device_id}?time={timestrap}'} + return jsonify(test) else: return context.make_fail_response('Could not start screenshot service! ' 'Please make sure the idevicescreenshot command works correctly') - def get_screen_shot(self, msg): + def get_screen_shot(self, message): + if message.get('cmd') != 'screenshot': + return screen_shots = [] - for item in device_service.devices: - device = device_service.devices[item] - screen_shot_path = device.take_screen_shot() + device_list = message.get('id') + for device_id in device_list: + device = device_service.devices.get(device_id) + if not device: + continue + screen_shot_info = device.take_screen_shot() screen_shots.append( { - 'id': item, + 'id': device_id, 'screenshot': { - 'name': os.path.basename(screen_shot_path), - 'path': screen_shot_path + 'name': os.path.basename(screen_shot_info.get('screen_shot_file')), + 'path': screen_shot_info.get('screen_shot_file') } } ) lyrebird.publish('ios.screenshot', screen_shots, state=True) def get_screenshot_image(self, device_id): - return send_from_directory(tmp_dir, '%s.png' % device_service.devices.get(device_id).model.replace(' ', '_')) + if request.args.get('time'): + model = device_service.devices.get(device_id).model.replace(' ', '_') + timestrap = request.args.get('time') + return send_from_directory(screenshot_dir, f'{model}_{timestrap}.png') + else: + return None def make_dump_data(self, path): device_data = {} @@ -126,34 +139,6 @@ def make_dump_data(self, path): device_data['path'] = path return device_data - def dump_data(self): - """ - 获取所有设备相关信息,包括设备日志,崩溃日志,ANR日志,快照图片,APP_INFO等 - :return: name, path - e.g - [ - { - "name": "ios_log_{imei}.log", - "path": "/Users/lee/.lyrebird/plugins/lyrebird_ios/tmp/android_log_{imei}.log" - }, - { - "name": "ios_screenshot_{imei}.png", - "path": "/Users/lee/.lyrebird/plugins/lyrebird_ios/tmp/android_screenshot_{imei}.png" - } - ] - """ - res = [] - devices = device_service.devices - - for udid in devices: - device = devices[udid] - if device.log_file: - res.append(self.make_dump_data(device.log_file)) - if device.take_screen_shot(): - res.append(self.make_dump_data(device.take_screen_shot())) - - return jsonify(res) - def get_prop_file_path(self, device, device_id): device_prop_file_path = os.path.abspath(os.path.join(tmp_dir, '%s.info.txt' % device_id)) device_prop = device.device_info @@ -210,8 +195,6 @@ def on_create(self): self.add_url_rule('/api/stop_app//', view_func=self.stop_app) # 获取设备应用列表 self.add_url_rule('/api/apps/', view_func=self.app_list) - # 获取资源信息 - self.add_url_rule('/api/dump', view_func=self.dump_data) # 检查环境 self.add_url_rule('/api/check-env', view_func=self.check_env) # 获取默认配置 diff --git a/setup.py b/setup.py index 487a9f3..b1941dd 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setup( name='lyrebird-ios', - version='0.2.1', + version='0.2.2', packages=['lyrebird_ios'], url='https://github.com/meituan/lyrebird-ios', author='HBQA',