def dumpsys_activity(self, path): dumpsys_name = "dumpsys_{}.txt".format(self.sn) # adb shell dumpsys activity cmd = "adb -s {} shell dumpsys activity > {}/{}".format( self.sn, path, dumpsys_name) logging.info(cmd) timeout_command.run(cmd)
def enable_wifi_manager(self): # 启动Wifi Manager cmd = "adb -s {} shell am start -n com.ntflc.wifimanager/.MainActivity".format( self.sn) timeout_command.run(cmd) time.sleep(1) # 返回桌面 cmd = "adb -s {} shell input keyevent KEYCODE_HOME".format(self.sn) timeout_command.run(cmd)
def __set_pkg_info(self): # 判断路径是否存在 if not os.path.exists(self.path): logging.error("package from '{}' not found".format(self.path)) sys.exit(-1) # 获取文件名 self.filename = os.path.basename(self.path) # 获取包名 cmd = "aapt dump badging {} | grep package".format(self.path) rst = timeout_command.run(cmd) logging.info("[pkg_info] rst : {}".format(rst)) if rst is None: logging.warning("[pkg_info] time out: {}".format(cmd)) elif "ERROR" in cmd: logging.warning("[pkg_info] cannot execute: {}".format(cmd)) logging.warning("[pkg_info] result: {}".format(rst)) else: try: package_name = re.findall(r"name='([a-zA-Z._]+)'", rst)[0] self.name = package_name except Exception as e: logging.warning( "[pkg_info] failed to regex package name from {}. {}". format(rst, e)) try: package_version = re.findall(r"versionCode='([\d._]+)'", rst)[0] self.versioncode = package_version except Exception as e: logging.warning( "[pkg_info] failed to regex package version from {}. {}". format(rst, e)) # 获取启动Activity cmd = "aapt dump badging {} | grep launchable-activity".format( self.path) rst = timeout_command.run(cmd) if rst is None: logging.warning("[pkg_info] time out: {}".format(cmd)) elif "ERROR" in cmd: logging.warning("[pkg_info] cannot execute: {}".format(cmd)) logging.warning("[pkg_info] result: {}".format(rst)) else: try: activity_list = re.findall(r"name='(.+?)'", rst) main_activity = "" for activity in activity_list: if not activity.startswith( "com.squareup") and not activity.startswith( "com.github"): main_activity = activity break self.activity = main_activity except Exception as e: logging.warning( "[pkg_info] failed to regex main activity from {}. {}". format(rst, e))
def __set_device_info(self): # 获取本机所有设备名 rst = timeout_command.run("adb devices") sn_list = re.findall(r"(.+?)\s+device\n", rst) # 判断device是否存在 if self.sn not in sn_list: logging.error("device [{}] not found".format(self.sn)) sys.exit(-1) # 获取系统版本号 cmd = "adb -s {} shell getprop ro.build.version.release".format( self.sn) rst = timeout_command.run(cmd) if rst is None: logging.warning("[device_info] time out: {}".format(cmd)) elif "error" in rst: logging.warning("[device_info] cannot execute: {}".format(cmd)) logging.warning("[device_info] result: {}".format(rst)) else: try: os_version = re.findall(r"\d.\d.\d|\d.\d|[A-Z]", rst)[0] self.os = os_version except Exception as e: logging.warning( "[device_info] failed to regex os from {}. {}".format( rst, e)) # 获取分辨率 cmd = "adb -s {} shell dumpsys window | grep init".format(self.sn) rst = timeout_command.run(cmd) if rst is None: logging.warning("[device_info] time out: {}".format(cmd)) elif "error" in rst: logging.warning("[device_info] cannot execute: {}".format(cmd)) logging.warning("[device_info] result: {}".format(rst)) else: try: screen = re.findall(r"init=(\d{3,4}x\d{3,4})", rst)[0] self.screen = screen except Exception as e: logging.warning( "[device_info] failed to regex screen from {}. {}".format( rst, e)) # 获取设备名 cmd = "adb -s {} shell getprop ro.product.model".format(self.sn) rst = timeout_command.run(cmd) if rst is None: logging.warning("[device_info] time out: {}".format(cmd)) elif "error" in rst: logging.warning("[device_info] cannot execute: {}".format(cmd)) logging.warning("[device_info] result: {}".format(rst)) else: try: model = rst.strip() self.model = model except Exception as e: logging.warning( "[device_info] failed to get model. {}".format(e))
def run_monkey(self, package, path, throttle=700, cnt=10000): #如果是launcher 启动后30s退出再执行monkey if package.name == "com.ksmobile.launcher": cmd = "adb -s {} shell am start -n com.ksmobile.launcher/com.ksmobile.launcher.SplashActivity".format( self.sn) timeout_command.run(cmd) time.sleep(30) cmd = "adb -s {} shell am force-stop {}".format( self.sn, package.name) os.popen(cmd) time.sleep(3) # 生成随机数 rand = random.randint(0, 65535) # 执行monkey命令 cmd = "adb -s {} shell monkey -p {} -s {} --ignore-crashes --ignore-timeouts --throttle {} -v {} > {}".format( self.sn, package.name, rand, throttle, cnt, path) logging.info(cmd) os.popen(cmd) time.sleep(5)
def dump_hprof(self, package, prj_info): timestamp = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())) timeout_command.run( "adb -s {} shell am dumpheap {} /data/local/tmp/{}.hprof".format( self.sn, package.name, timestamp)) if not os.path.exists("hprof/{}".format(package.name)): os.makedirs("hprof/{}".format(package.name)) path = os.getcwd() jar_path = "{}/hprof/run/anylize_hprof_rgl.jar".format(path) hprof_path = "{}/hprof/{}/".format(path, package.name) hprof = hprof_path + "{}.hprof".format(timestamp) activelist_path = "{}/hprof/run/".format( path) + prj_info["activitylist"] hprof_size = self.pull_hprof(hprof, hprof_path, timestamp) retry_num = 0 #判断下载的hprof文件大于20兆,确保文件完整 while hprof_size <= 2000000: hprof_size = self.pull_hprof(hprof, hprof_path, timestamp) retry_num += 1 time.sleep(5) print "当前重试次数", retry_num if retry_num == 10: break timeout_command.run( "adb -s {} shell rm /data/local/tmp/{}.hprof".format( self.sn, timestamp)) if hprof_size > 0: timeout_command.run("java -jar {} {} {} {} {}".format( jar_path, hprof, activelist_path, package.name, package.versioncode)) return "SUCCESS" else: return "FAILED"
def enable_simiasque(self): # 启动simiasque cmd = "adb -s {} shell am start -n org.thisisafactory.simiasque/org.thisisafactory.simiasque.MyActivity_".format( self.sn) timeout_command.run(cmd) time.sleep(1) # 返回桌面 cmd = "adb -s {} shell input keyevent KEYCODE_HOME".format(self.sn) timeout_command.run(cmd) time.sleep(1) # 打开simiasque开关 cmd = "adb -s {} shell am broadcast -a org.thisisafactory.simiasque.SET_OVERLAY --ez enable true".format( self.sn) timeout_command.run(cmd)
def uninstall(self, package): # 卸载包 cmd = "adb -s {} uninstall {}".format(self.sn, package.name) rst = timeout_command.run(cmd, 600) if rst is None: logging.error( "[uninstall] failed to uninstall, the command is: {}".format( cmd)) elif "Success" in rst: logging.info("[uninstall] succeeded in uninstalling {}".format( package.name)) elif "Failure" in rst: try: key = re.findall(r"Failure \[(.+?)\]", rst)[0] except Exception as e: logging.debug(e) key = "NULL" logging.error( "[uninstall] failed to uninstall {}, reason: {}".format( package.name, key))
def install(self, package): # 安装包 cmd = "adb -s {} install -r {}".format(self.sn, package.path) d = u2.connect_usb(self.sn) thread.start_new_thread(click_install, (d, )) rst = timeout_command.run(cmd, 600) if rst is None: logging.error( "[install] failed to install, the command is: {}".format(cmd)) sys.exit(-1) elif "Success" in rst: logging.info("[install] succeeded in installing {}".format( package.name)) elif "Failure" in rst: try: key = re.findall(r"Failure \[(.+?)\]", rst)[0] except Exception as e: logging.debug(e) key = "NULL" logging.error("[install] failed to install {}, reason: {}".format( package.name, key)) sys.exit(-1)
def turn_off_screen(self): cmd = "adb -s {} shell input keyevent POWER".format(self.sn) timeout_command.run(cmd)
def disable_simiasque(self): # 关闭simiasque开关 cmd = "adb -s {} shell am broadcast -a org.thisisafactory.simiasque.SET_OVERLAY --ez enable false".format( self.sn) timeout_command.run(cmd)