def main_process(cmd): devices = Fun.devices_list_with_connection_check() if len(devices) == 0: Fun.log('no device connected') sys.exit(2) for dev in devices: Thread(target=collect_dump_info, args=(dev, cmd)).start()
def install_r(device, apk_path, r): install_cmd = Res.install_r if r else Res.install try: command = Res.adb_s + device + install_cmd + '%s' % apk_path return FunCom.p_open(command) except Exception as e: FunCom.log(e)
def monkey_work(device_id, app_name=Res.app_name): browser = app_name device = device_id conf = Fun.get_conf_parser(Res.monkey_conf_file) Fun.assert_options_presents(conf, ['mail', 'ftp', 'apk', 'monkey_params']) sender = conf.get('mail', 'sender') mail_pwd = conf.get('mail', 'pwd') subject = conf.get('mail', 'subject') receivers = conf.get('mail', 'receivers').split(',') anr_receivers = conf.get('mail', 'anr_receivers').split(',') pkg_name = conf.get('apk', 'package_name') subject = conf.get('apk', 'name') + subject keyword = conf.get('apk', 'keyword') events = conf.get('monkey_params', 'events') throttle = conf.get('monkey_params', 'throttle') apk_path = Fun.get_abspath(Res.apk_path) android_version = Fun.get_android_version(device_id) device_info = Fun.parse_device_info(device_id) logfile = Fun.get_abspath( Fun.path_join(Res.log_path, device_info + Res.log_suffix)) package_installed_once = False Mon.push_lib_to_phone(device) while True: if not package_installed_once: try: # todo Mon.prepare_log_file_path_file(logfile) Mon.refresh_with_apk(device, apk_path, pkg_name, logfile, package_installed_once) package_installed_once = True # todo Mon.skip(device) start_monkey_and_analyze(android_version, anr_receivers, browser, device, device_info, events, keyword, logfile, mail_pwd, pkg_name, receivers, sender, subject, throttle) except Exception as e: Fun.log(e) else: # todo Fun.kill_monkey(device) # todo FunLch.start_launcher_omit_splash(device, False, False) Fun.sleep(2) start_monkey_and_analyze(android_version, anr_receivers, browser, device, device_info, events, keyword, logfile, mail_pwd, pkg_name, receivers, sender, subject, throttle)
def exception_handle(device_id, logcat_to_file=False): # if logcat_to_file: FunCom.log_cat_to_file(device_id) device_info = FunCom.parse_device_info(device_id) dir_to_store_logfile = FunCom.get_abspath( FunCom.path_join( Res.log_path, device_info + Res.underline + Res.logcat_tag + FunCom.current_time())) FunCom.make_dir_if_not_exist(dir_to_store_logfile) FunCom.log('Exception handle id:' + device_id + " dir to store log::" + dir_to_store_logfile) FunCom.p_open(Res.asb_shell_pull_log_file(device_id, dir_to_store_logfile))
def information_collection(apk_tag, dev, path): Fun.log('trigger gc') absolute_tmp_dump_path = os.path.realpath(path) Fun.p_open( Res.adb_grab_heap_dump_file_with_pkg(dev, absolute_tmp_dump_path, Res.pkg_name)) Fun.sleep(10) lines = Fun.p_open(Res.asb_shell_dump_mem_info(dev)).readlines() name = apk_tag + Res.dump + Fun.current_time() dump_mem_info_store_to_file(name, lines, path) Grab.grab_dump_and_convert(dev, name, absolute_tmp_dump_path, Res.pkg_name)
def count_info(tag, total_number_dalvik_size, dalvik_size, dalvik_alloc): FunCom.sleep(15) mem_info = FunCom.p_open( 'adb -s ' + tag + ' shell dumpsys mem_info com.kcg.com.fun').readlines() for info_item in mem_info: if 'Dalvik Heap' in info_item: info_item_info_arr = info_item.split(' ') if len(info_item_info_arr) > 4: total_number_dalvik_size.append(info_item_info_arr[1]) dalvik_size.append(info_item_info_arr[-3]) dalvik_alloc.append(info_item_info_arr[-2]) FunCom.log(str(info_item_info_arr[1]) + '===============')
def start_monkey_and_analyze(android_version, anr_receivers, browser, device, device_info, events, keyword, logfile, mail_pwd, pkg_name, receivers, sender, subject, throttle): Fun.kill_monkey(device) Mon.prepare_log_file_path_file(logfile) # todo Mon.start(device, pkg_name, events, throttle, logfile) try: Mon.log_trick(device, logfile, sender, mail_pwd, receivers, anr_receivers, subject, logfile, browser, device_info, android_version, keyword) except Exception as e: Fun.log(e)
def battery_instrument_main(): FunCom.log('launch battery instrument ') x = 0 while True: for app_file in FunPkg.get_apk_file_in_project(): try: tag = FunCom.devices_list_with_connection_check()[0] FunCom.p_open(Res.adb_dump_battery_unplug(tag)) FunCom.send_key_home(tag) FunPkg.uninstall_and_install(tag, app_file, Res.pkg_name) FunCom.serial_clicks_by_control_file(tag) FunCom.sleep(20) perform_operations(tag, FunCom.get_device_uid(tag), x, app_file) x += 1 except Exception as e: FunCom.log(e)
def create_or_erase(f): f = FunCom.get_abspath(FunCom.path_join(Res.project_path, f)) FunCom.log('to parse file: ' + f) if not FunCom.exists(f): FunCom.log('not exist, creating') FunCom.create_file(f) else: FunCom.log('exist, erasing') FunCom.erase_file_content(f)
def count_info_first(tag, total_dalvik_size1, dalvik_size1, dalvik_alloc1): for i in range(10): FunCom.p_open( 'adb -s ' + tag + ' shell am dumpheap com.kcg.com.fun /sdcard/click_test.hprof ') FunCom.sleep(2) FunCom.p_open('adb -s ' + tag + ' shell dumpsys mem_info com.kcg.com.fun') FunCom.sleep(2) FunCom.sleep(20) FunCom.p_open('adb -s ' + tag + ' shell dumpsys mem_info com.kcg.com.fun') mem_info = FunCom.p_open( 'adb -s ' + tag + ' shell dumpsys mem_info com.kcg.com.fun').readlines() for info_item in mem_info: if 'Dalvik Heap' in info_item: info_item_info_arr = info_item.split(' ') if len(info_item_info_arr) > 4: total_dalvik_size1.append(info_item_info_arr[1]) dalvik_size1.append(info_item_info_arr[-3]) dalvik_alloc1.append(info_item_info_arr[-2]) FunCom.log(str(info_item_info_arr[1]) + '-------------------')
def collect_dump_info(dev): Fun.log('start thread for device:' + dev) path = make_device_tag_dir(dev) for apk in pkg_list: inspect_apk_dump_with_internal(apk, dev, path)
from multiprocessing import freeze_support
import sys
import sys
def log_trick(device_id, monkey_log, sender, pwd, receivers, anr_receivers, subject, logfile, browser, device, version, keyword): FunCom.log('start log analyze') # todo # exception_handle(device_id) if FunCom.exists(monkey_log): f = open(monkey_log) lines = f.read() exception = "Exception" crash = "CRASH" anr = "ANR" native_crash = 'Short Msg: Native crash' not_running = 'is your activity running' focus_on = "Bitmap" logcat_to_file = focus_on in lines if exception in lines or crash in lines: crash_result = lines[lines.index(crash) - 200:] crash_result = crash_result.replace('\n', '<br>') exception_handle(device_id, logcat_to_file) if check_key_word(crash_result, keyword): Sender.sendException(crash_result, sender, pwd, receivers, subject, logfile, browser, device, version) FunCom.log("monkey finished with exception") else: FunCom.log('monkey finished with system exception\n') FunCom.log('<<<<<<<<<<<<<<<<<<<<<\n' + crash_result) elif anr in lines: FunCom.log("monkey finished with anr") FunCom.log(lines[lines.index(anr):lines.index(anr) + 10000]) crash_result = lines[lines.index(anr):lines.index(anr) + 200] crash_result = crash_result.replace('\n', '<br>') exception_handle(device_id, logcat_to_file) Sender.sendException(crash_result, sender, pwd, anr_receivers, subject.replace('Crash', 'ANR'), logfile, browser, device, version) elif native_crash in lines: exception_handle(device_id, logcat_to_file) FunCom.log('monkey finished with native crash') elif not_running in lines: FunCom.log('monkey finished with activity not running') f.close() FunCom.log('>>> monkey finished...\n\n')
def memory_instrument_main(): number = sys.argv[1] # number=10 x = 0 app_map = FunPkg.get_apk_file_in_project() while True: apk_path = os.getcwd() devices_info = FunCom.connected_devices_arr() devices_num = len(devices_info) if devices_num > 0: tag = devices_info[0] for app_map_item in range(len(app_map)): try: total_number_dalvik_size = [] dalvik_size = [] dalvik_alloc = [] first_time = [] end_time = [] levels = [] capacity = [] computed = [] uid_item_info = [] app_map_name = app_map[app_map_item] total_dalvik_size1 = [] dalvik_size1 = [] dalvik_alloc1 = [] app_map_name_path = apk_path + "/apk/" + app_map_name.strip() FunCom.log(app_map_name_path) FunPkg.uninstall(tag, Res.pkg_name) FunPkg.install(tag, app_map_name_path) FunCom.sleep(10) for i in range(500): FunCom.serial_clicks_by_control_file(tag) FunCom.p_open('adb -s ' + tag + ' shell dumpsys battery unplug') count_info_first(tag, total_dalvik_size1, dalvik_size1, dalvik_alloc1) datetime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) first_time.append(datetime) uid_info = FunCom.get_device_uid(tag) FunCom.p_open('adb -s ' + tag + ' shell ' + 'dumpsys batterystats --reset') FunCom.serial_operation_by_control_file(tag) FunCom.log('------') access_to_electricity(tag, end_time, levels, capacity, computed, uid_item_info, uid_info) count_info(tag, total_number_dalvik_size, dalvik_size, dalvik_alloc) if total_number_dalvik_size[i] < total_dalvik_size1[i]: total_dalvik_size1.pop(i) total_number_dalvik_size.pop(i) dalvik_size.pop(i) dalvik_size1.pop(i) dalvik_alloc1.pop(i) dalvik_alloc.pop(i) first_time.pop(i) end_time.pop(i) levels.pop(i) capacity.pop(i) computed.pop(i) uid_item_info.pop(i) write_data_into_excel( x, total_number_dalvik_size, dalvik_size, dalvik_alloc, app_map_name, tag, total_dalvik_size1, dalvik_size1, dalvik_alloc1, first_time, end_time, levels, capacity, computed, uid_item_info) FunPkg.clear(tag) if len(total_number_dalvik_size) == int(number): break x += 1 except Exception as e: FunCom.log(e) if app_map_item == len(app_map) - 1: app_map_item == 0
def collect_dump_info(dev, cmd): Fun.log('start thread for device:' + dev) FunCom.serial_operation_by_control_file(dev, '/case/%s.txt' % 'all_apps')
# coding=utf-8
import sys