Esempio n. 1
0
 def list_local_devices():
     '''获取本地设备列表
     '''
     from adb import ADB
     result = []
     for device_id in ADB.list_device():
         result.append(AndroidDevice(device_id))
     return result
Esempio n. 2
0
def copy_android_driver(device_id='', force=False):
    '''测试前的测试桩拷贝
    '''
    import os
    from adb import ADB
    if not device_id:
        device_list = ADB.list_device()
        device_list = [dev for dev in device_list if dev[1] == 'device']
        if len(device_list) == 0:
            raise RuntimeError('当前没有插入手机')
        elif len(device_list) == 1:
            device_id = device_list[0][0]
        elif len(device_list) > 1:
            i = 0
            text = '当前设备列表:\n'
            for dev in device_list:
                # if dev[1] != 'device': continue
                i += 1
                text += '%d. %s\n' % (i, dev[0])
            print text
            while True:
                result = raw_input('请输入要拷贝测试桩的设备序号:')
                if result.isdigit():
                    if int(result) > len(device_list):
                        print >> sys.stderr, '序号范围为: [1, %d]' % len(device_list)
                        time.sleep(0.1)
                        continue
                    device_id = device_list[int(result) - 1][0]
                else:
                    if not result in [dev[0] for dev in device_list]:
                        print >> sys.stderr, '设备序列号不存在: %r' % result
                        time.sleep(0.1)
                        continue
                    device_id = result
                break
            print '您将向设备"%s"拷贝测试桩……' % device_id
    adb = ADB.open_device(device_id)
    cur_path = os.path.dirname(os.path.abspath(__file__))
    dst_path = '/data/local/tmp/qt4a/'

    current_version_file = os.path.join(cur_path, 'tools', 'version.txt')
    f = open(current_version_file, 'r')
    vurrent_version = int(f.read())
    f.close()
    
    if not force:
        version_file = dst_path + 'version.txt'
        version = adb.run_shell_cmd('cat %s' % version_file)
    
        if version and not 'No such file or directory' in version and vurrent_version <= int(version):
            # 不需要拷贝测试桩
            if not adb.get_package_path('com.test.qt4amockapp'):
                logger.warn('install QT4AMockApp')
                adb.install_apk(os.path.join(cur_path, 'tools', 'QT4AMockApp.apk'))
            logger.warn('忽略本次测试桩拷贝:当前版本为%s,设备中版本为%s' % (vurrent_version, version))
            return
    
    if not adb.is_rooted():
        result = adb.run_shell_cmd('id', True)
        if not 'uid=0(root)' in result:
            raise RuntimeError('设备未root:%s' % result)
        
    cpu_abi = adb.get_cpu_abi()
    print '当前系统的CPU架构为:%s' % cpu_abi
    use_pie = False
    if adb.get_sdk_version() >= 21: use_pie = True
    file_list = [os.path.join(cpu_abi, 'inject'), os.path.join(cpu_abi, 'libdexloader.so'), os.path.join(cpu_abi, 'setpropex'), os.path.join(cpu_abi, 'libandroidhook.so'), 'AndroidSpy.jar', 'androidhook.jar', 'SpyHelper.jar', 'SpyHelper.sh']
    
    if adb.is_selinux_opened():
        # 此时如果还是开启状态说明关闭selinux没有生效,主要是三星手机上面
        adb.run_shell_cmd('chcon u:object_r:shell_data_file:s0 %slibdexloader.so' % dst_path, True)  # 恢复文件context,否则拷贝失败
        adb.run_shell_cmd('chcon u:object_r:shell_data_file:s0 %slibandroidhook.so' % dst_path, True)
        
    for file in file_list:
        file_path = os.path.join(cur_path, 'tools', file)
        if use_pie and not '.' in file: file_path += '_pie'
        adb.push_file(file_path, dst_path + os.path.split(file)[-1])
    dst_dir = dst_path[:dst_path.rfind('/')]
    # adb.run_shell_cmd('chmod 777 %s' % dst_dir, True)
    # logger.info(adb.run_shell_cmd('chmod 777 %sinject' % (dst_path), True))
    adb.chmod('%sinject' % dst_path, 777)
    # adb.run_shell_cmd('chmod 777 %sAndroidSpy.jar' % (dst_path), True)
    try:
        print adb.run_shell_cmd('rm -R %scache' % dst_path, True)  # 删除目录 rm -rf
    except RuntimeError, e:
        logger.warn('%s' % e)