def auto_load_base(): lyrebird_conf = lyrebird.context.application.conf # 读取指定base文件,写入到base.json if lyrebird_conf.get('hunter.base'): base_path = lyrebird_conf.get('hunter.base') base = codecs.open(os.path.join(PLUGINS_CONF_DIR, base_path), 'r', 'utf-8').read() f = codecs.open(DEFAULT_BASE, 'w', 'utf-8') f.write(base) f.close() app_context.base_sha1 = get_file_sha1(DEFAULT_BASE) return json.loads(base) # 通过本地默认base文件获取base elif not os.path.exists(DEFAULT_BASE): copy_file(DEFAULT_BASE) if not codecs.open(DEFAULT_BASE, 'r', 'utf-8').read(): lyrebird.get_logger().error('Base is None.Please check your base file of API-Coverage.') else: json_obj = json.loads(codecs.open(DEFAULT_BASE, 'r', 'utf-8').read()) app_context.base_sha1 = get_file_sha1(DEFAULT_BASE) return json_obj
import os import shutil import lyrebird from lyrebird import context, get_logger from . import android_helper from . import config import traceback logger = get_logger() class DeviceService: """ Background service : Android devices monitor Poll devices status per 1 sec with adb command """ READY = 0 RUNNING = 1 STOP = 2 def __init__(self): self.status = self.READY self.handle_interval = 1 self.devices = {} self.reset_screenshot_dir() logger.debug('DeviceService OnCreate') def devices_to_dict(self): json_obj = {}
def handle(self, handler_context: HandlerContext): # UI自动化等需要mock的手段orgin url 需要调用这个方法 org_url = handler_context.get_origin_url() if not org_url: org_url = handler_context.request.url short_url = org_url.replace('http://', '').replace('https://', '').split('?')[0] # format之后的真正PATH,处理{num}这样的情况,emit给前端,做刷新table用,同时处理成小写 path = format_url.format_api(short_url).lower() # 获取handler_context.id,为前端展开看详情准备 path_id = handler_context.id device_ip = handler_context.request.headers.get('lyrebird.device.ip') # 当请求过来的时候,base还没有init,需要进行init处理 if not app_context.base_list: base_dict = BaseDataHandler().get_base_source() if isinstance(base_dict, Response): lyrebird.get_logger().error('API-Coverage base file is None.') else: mergeAlgorithm.first_result_handler(base_dict) if path in app_context.base_list: # merge到 context merge list中 mergeAlgorithm.merge_handler_new(path, path_id) # 在base里的需要去计算下覆盖率 mergeAlgorithm.coverage_handler() # path传给覆盖详情表格 lyrebird.emit('test_data message', path, namespace='/api_coverage') # 如果设备信息抓取到,进行上报 # if device_ip in app_context.info: report_worker(path, device_ip) # 如果path配置了对应的参数 if path in list(app_context.path_param_dic.keys()): ulr_list = app_context.path_param_dic[path] flag = 0 for item in ulr_list: if compare_query(item['url'], handler_context.request.url): mergeAlgorithm.merge_handler_new(item['url_base'], path_id) mergeAlgorithm.coverage_handler() lyrebird.emit('test_data message', item['url_base'], namespace='/api_coverage') # 如果设备信息抓取到,进行上报 # if device_ip in app_context.info: report_worker(item['url_base'], device_ip) flag = 1 # 如果参数组合不存在,提取关注的字段 if flag == 0: url_pgroup = '' params_list = [] for item in ulr_list: params_list.extend(item['params'].keys()) # 去重 for p in list(set(params_list)): val = handler_context.request.args.get(p) if url_pgroup: url_pgroup = url_pgroup + '&' + str(p) + '=' + str(val) else: url_pgroup = path + '?' + str(p) + '=' + str(val) mergeAlgorithm.merge_handler_new(url_pgroup, path_id) mergeAlgorithm.coverage_handler() lyrebird.emit('test_data message', url_pgroup, namespace='/api_coverage') # 如果设备信息抓取到,进行上报 # if device_ip in app_context.info: report_worker(url_pgroup, device_ip) # 如果不在base里,需要判断这些API是否被筛掉 else: # 如果不在筛除列表内,才进行merge等一系列算法 if not Filter().filter_all(path): # merge到 context merge list中 mergeAlgorithm.merge_handler_new(path, path_id) # 传给api_coverage前端的socket信息 lyrebird.emit('test_data message', path, namespace='/api_coverage') # 如果设备信息抓取到,进行上报 # if device_ip in app_context.info: report_worker(path, device_ip) else: pass