def adb_connect(device, timeout=None): _check_env() command = "adb connect " + device if ":5555" in device: logger.debug(command) output, _ = check_output(command, shell=True, timeout=timeout) logger.debug(output) #### due to a rare adb bug sometimes an extra :5555 is appended to the IP address if output.find("5555:5555") != -1: logger.debug("ADB BUG with extra 5555") command = "adb connect " + device.replace(":5555", "") tries = 0 while not poll_for_file(device, "/proc/cpuinfo"): logger.debug("adb connect failed, retrying now...") tries += 1 if tries > MAX_TRIES: raise DeviceError("Cannot connect to adb server on the device.") logger.debug(command) output, _ = check_output(command, shell=True, timeout=timeout) time.sleep(10) if output.find("connected to") == -1: raise DeviceError("Could not connect to {}".format(device))
def create_uiauto_project(path, name, target='1'): sdk_path = get_sdk_path() android_path = os.path.join(sdk_path, 'tools', 'android') package_name = 'com.arm.wlauto.uiauto.' + name.lower() # ${ANDROID_HOME}/tools/android create uitest-project -n com.arm.wlauto.uiauto.linpack -t 1 -p ../test2 command = '{} create uitest-project --name {} --target {} --path {}'.format(android_path, package_name, target, path) try: check_output(command, shell=True) except subprocess.CalledProcessError as e: if 'is is not valid' in e.output: message = 'No Android SDK target found; have you run "{} update sdk" and download a platform?' raise CommandError(message.format(android_path)) build_script = os.path.join(path, 'build.sh') with open(build_script, 'w') as wfh: template = string.Template(UIAUTO_BUILD_SCRIPT) wfh.write(template.substitute({'package_name': package_name})) os.chmod(build_script, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) source_file = _f(os.path.join(path, 'src', os.sep.join(package_name.split('.')[:-1]), 'UiAutomation.java')) with open(source_file, 'w') as wfh: wfh.write(render_template('UiAutomation.java', {'name': name, 'package_name': package_name}))
def adb_connect(device, timeout=None): _check_env() command = "adb connect " + device if ":" in device: port = device.split(':')[-1] logger.debug(command) output, _ = check_output(command, shell=True, timeout=timeout) logger.debug(output) #### due to a rare adb bug sometimes an extra :5555 is appended to the IP address if output.find('{}:{}'.format(port, port)) != -1: logger.debug('ADB BUG with extra port') command = "adb connect " + device.replace(':{}'.format(port), '') tries = 0 output = None while not poll_for_file(device, "/proc/cpuinfo"): logger.debug("adb connect failed, retrying now...") tries += 1 if tries > MAX_TRIES: raise DeviceError('Cannot connect to adb server on the device.') logger.debug(command) output, _ = check_output(command, shell=True, timeout=timeout) time.sleep(10) if tries and output.find('connected to') == -1: raise DeviceError('Could not connect to {}'.format(device))
def create_uiauto_project(path, name, target='1'): sdk_path = get_sdk_path() android_path = os.path.join(sdk_path, 'tools', 'android') package_name = 'com.arm.wlauto.uiauto.' + name.lower() # ${ANDROID_HOME}/tools/android create uitest-project -n com.arm.wlauto.uiauto.linpack -t 1 -p ../test2 command = '{} create uitest-project --name {} --target {} --path {}'.format( android_path, package_name, target, path) check_output(command, shell=True) build_script = os.path.join(path, 'build.sh') with open(build_script, 'w') as wfh: template = string.Template(UIAUTO_BUILD_SCRIPT) wfh.write(template.substitute({'package_name': package_name})) os.chmod(build_script, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) source_file = _f( os.path.join(path, 'src', os.sep.join(package_name.split('.')[:-1]), 'UiAutomation.java')) with open(source_file, 'w') as wfh: wfh.write( render_template('UiAutomation.java', { 'name': name, 'package_name': package_name }))
def _generate_workgen_config(self, user_file, output_directory): output_file = os.path.join(output_directory, 'unkind.json') # use workgen dry run option to generate a use case # file with proper JSON grammar on host first try: check_output('python {} -d -o {} {}'.format( self.workgen_script, output_file, user_file), shell=True) except CalledProcessError as e: message = 'Could not generate config using workgen, got "{}"' raise WorkloadError(message.format(e)) return output_file
def _generate_workgen_config(self, user_file, output_directory): output_file = os.path.join(output_directory, 'unkind.json') # use workgen dry run option to generate a use case # file with proper JSON grammar on host first try: check_output('python {} -d -o {} {}'.format(self.workgen_script, output_file, user_file), shell=True) except CalledProcessError as e: message = 'Could not generate config using workgen, got "{}"' raise WorkloadError(message.format(e)) return output_file
def adb_shell(device, command, timeout=None, check_exit_code=False, as_root=False): # NOQA _check_env() if as_root: command = 'echo "{}" | su'.format(escape_double_quotes(command)) device_string = '-s {}'.format(device) if device else '' full_command = 'adb {} shell "{}"'.format(device_string, escape_double_quotes(command)) logger.debug(full_command) if check_exit_code: actual_command = "adb {} shell '({}); echo $?'".format( device_string, escape_single_quotes(command)) raw_output, error = check_output(actual_command, timeout, shell=True) if raw_output: try: output, exit_code, _ = raw_output.rsplit('\n', 2) except ValueError: exit_code, _ = raw_output.rsplit('\n', 1) output = '' else: # raw_output is empty exit_code = '969696' # just because output = '' exit_code = exit_code.strip() if exit_code.isdigit(): if int(exit_code): message = 'Got exit code {}\nfrom: {}\nSTDOUT: {}\nSTDERR: {}'.format( exit_code, full_command, output, error) raise DeviceError(message) elif am_start_error.findall(output): message = 'Could not start activity; got the following:' message += '\n{}'.format(am_start_error.findall(output)[0]) raise DeviceError(message) else: # not all digits if am_start_error.findall(output): message = 'Could not start activity; got the following:' message += '\n{}'.format(am_start_error.findall(output)[0]) raise DeviceError(message) else: raise DeviceError( 'adb has returned early; did not get an exit code. Was kill-server invoked?' ) else: # do not check exit code output, _ = check_output(full_command, timeout, shell=True) return output
def adb_command(device, command, timeout=None): _check_env() device_string = '-s {}'.format(device) if device else '' full_command = "adb {} {}".format(device_string, command) logger.debug(full_command) output, _ = check_output(full_command, timeout, shell=True) return output
def update_result(self, context): # pylint: disable=r0201 self.logger.debug("Waiting for atrace to finish dumping data") self.p.wait() context.device.pull_file(self.output_file, context.output_directory) cmd = "python {} --from-file={} -o {}" cmd = cmd.format( os.path.join(os.environ['ANDROID_HOME'], "platform-tools/systrace/systrace.py"), os.path.join(context.output_directory, "atrace.txt"), os.path.join(context.output_directory, "systrace.html")) self.logger.debug(cmd) _, error = check_output(cmd.split(" "), timeout=10) if error: raise InstrumentError(error) context.add_iteration_artifact('atrace.txt', path=os.path.join( context.output_directory, "atace.txt"), kind='data', description='atrace dump.') context.add_iteration_artifact('systrace.html', path=os.path.join( context.output_directory, "systrace.html"), kind='data', description='Systrace HTML report.')
def _scp(self, source, dest, timeout=30): # NOTE: the version of scp in Ubuntu 12.04 occasionally (and bizarrely) # fails to connect to a device if port is explicitly specified using -P # option, even if it is the default port, 22. To minimize this problem, # only specify -P for scp if the port is *not* the default. port_string = '-P {}'.format(self.port) if (self.port and self.port != 22) else '' keyfile_string = '-i {}'.format(self.keyfile) if self.keyfile else '' command = '{} -r {} {} {} {}'.format(scp, keyfile_string, port_string, source, dest) pass_string = '' logger.debug(command) if self.password: command = _give_password(self.password, command) try: check_output(command, timeout=timeout, shell=True) except subprocess.CalledProcessError as e: raise subprocess.CalledProcessError(e.returncode, e.cmd.replace(pass_string, ''), e.output) except TimeoutError as e: raise TimeoutError(e.command.replace(pass_string, ''), e.output)
def adb_shell(device, command, timeout=None, check_exit_code=False, as_root=False): # NOQA _check_env() if as_root: command = 'echo "{}" | su'.format(escape_double_quotes(command)) device_string = "-s {}".format(device) if device else "" full_command = 'adb {} shell "{}"'.format(device_string, escape_double_quotes(command)) logger.debug(full_command) if check_exit_code: actual_command = "adb {} shell '({}); echo $?'".format(device_string, escape_single_quotes(command)) raw_output, error = check_output(actual_command, timeout, shell=True) if raw_output: try: output, exit_code, _ = raw_output.rsplit("\n", 2) except ValueError: exit_code, _ = raw_output.rsplit("\n", 1) output = "" else: # raw_output is empty exit_code = "969696" # just because output = "" exit_code = exit_code.strip() if exit_code.isdigit(): if int(exit_code): message = "Got exit code {}\nfrom: {}\nSTDOUT: {}\nSTDERR: {}".format( exit_code, full_command, output, error ) raise DeviceError(message) elif am_start_error.findall(output): message = "Could not start activity; got the following:" message += "\n{}".format(am_start_error.findall(output)[0]) raise DeviceError(message) else: # not all digits if am_start_error.findall(output): message = "Could not start activity; got the following:" message += "\n{}".format(am_start_error.findall(output)[0]) raise DeviceError(message) else: raise DeviceError("adb has returned early; did not get an exit code. Was kill-server invoked?") else: # do not check exit code output, _ = check_output(full_command, timeout, shell=True) return output
def _scp(self, source, dest, timeout=30): # NOTE: the version of scp in Ubuntu 12.04 occasionally (and bizarrely) # fails to connect to a device if port is explicitly specified using -P # option, even if it is the default port, 22. To minimize this problem, # only specify -P for scp if the port is *not* the default. port_string = '-P {}'.format(self.port) if (self.port and self.port != 22) else '' keyfile_string = '-i {}'.format(self.keyfile) if self.keyfile else '' command = '{} -r {} {} {} {}'.format(scp, keyfile_string, port_string, source, dest) pass_string = '' logger.debug(command) if self.password: command = _give_password(self.password, command) try: check_output(command, timeout=timeout, shell=True) except subprocess.CalledProcessError as e: raise CalledProcessErrorWithStderr(e.returncode, e.cmd.replace(pass_string, ''), output=e.output, error=getattr(e, 'error', '')) except TimeoutError as e: raise TimeoutError(e.command.replace(pass_string, ''), e.output)
def create_uiauto_project(path, name, target='1'): sdk_path = get_sdk_path() android_path = os.path.join(sdk_path, 'tools', 'android') package_name = 'com.arm.wlauto.uiauto.' + name.lower() # ${ANDROID_HOME}/tools/android create uitest-project -n com.arm.wlauto.uiauto.linpack -t 1 -p ../test2 command = '{} create uitest-project --name {} --target {} --path {}'.format(android_path, package_name, target, path) check_output(command, shell=True) build_script = os.path.join(path, 'build.sh') with open(build_script, 'w') as wfh: template = string.Template(UIAUTO_BUILD_SCRIPT) wfh.write(template.substitute({'package_name': package_name})) os.chmod(build_script, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) source_file = _f(os.path.join(path, 'src', os.sep.join(package_name.split('.')[:-1]), 'UiAutomation.java')) with open(source_file, 'w') as wfh: wfh.write(render_template('UiAutomation.java', {'name': name, 'package_name': package_name}))
def update_result(self, context): # pylint: disable=r0201 self.logger.debug("Waiting for atrace to finish dumping data") self.p.wait() context.device.pull_file(self.output_file, context.output_directory) cmd = "python {} --from-file={} -o {}" cmd = cmd.format(os.path.join(os.environ['ANDROID_HOME'], "platform-tools/systrace/systrace.py"), os.path.join(context.output_directory, "atrace.txt"), os.path.join(context.output_directory, "systrace.html")) self.logger.debug(cmd) _, error = check_output(cmd.split(" "), timeout=10) if error: raise InstrumentError(error) context.add_iteration_artifact('atrace.txt', path=os.path.join(context.output_directory, "atace.txt"), kind='data', description='atrace dump.') context.add_iteration_artifact('systrace.html', path=os.path.join(context.output_directory, "systrace.html"), kind='data', description='Systrace HTML report.')
def fastboot_command(command, timeout=None): _check_env() full_command = "fastboot {}".format(command) logger.debug(full_command) output, _ = check_output(full_command, timeout, shell=True) return output
def adb_shell(device, command, timeout=None, check_exit_code=False, as_root=False): # NOQA # pylint: disable=too-many-branches, too-many-locals, too-many-statements _check_env() if as_root: command = 'echo \'{}\' | su'.format(escape_single_quotes(command)) device_string = '-s {}'.format(device) if device else '' full_command = 'adb {} shell "{}"'.format(device_string, escape_double_quotes(command)) logger.debug(full_command) if check_exit_code: actual_command = "adb {} shell '({}); echo; echo $?'".format(device_string, escape_single_quotes(command)) try: raw_output, error = check_output(actual_command, timeout, shell=True) except CalledProcessErrorWithStderr as e: raw_output = e.output error = e.error exit_code = e.returncode if exit_code == 1: logger.debug("Exit code 1 could be either the return code of the command or mean ADB failed") if raw_output: if raw_output.endswith('\r\n'): newline = '\r\n' elif raw_output.endswith('\n'): newline = '\n' else: raise WAError("Unknown new line separator in: {}".format(raw_output)) try: output, exit_code, _ = raw_output.rsplit(newline, 2) except ValueError: exit_code, _ = raw_output.rsplit(newline, 1) output = '' else: # raw_output is empty exit_code = '969696' # just because output = '' exit_code = exit_code.strip() if exit_code.isdigit(): if int(exit_code): message = 'Got exit code {}\nfrom: {}\nSTDOUT: {}\nSTDERR: {}'.format(exit_code, full_command, output, error) raise DeviceError(message) elif am_start_error.findall(output): message = 'Could not start activity; got the following:' message += '\n{}'.format(am_start_error.findall(output)[0]) raise DeviceError(message) else: # not all digits if am_start_error.findall(output): message = 'Could not start activity; got the following:' message += '\n{}'.format(am_start_error.findall(output)[0]) raise DeviceError(message) else: raise DeviceError('adb has returned early; did not get an exit code. Was kill-server invoked?') else: # do not check exit code try: output, _ = check_output(full_command, timeout, shell=True) except CalledProcessErrorWithStderr as e: output = e.output error = e.error exit_code = e.returncode if e.returncode == 1: logger.debug("Got Exit code 1, could be either the return code of the command or mean ADB failed") return output
def run(self, context): self.logger.debug(self.command) self.raw_output, _ = check_output(self.command, shell=True, timeout=self.run_timeout, ignore='all')
def adb_shell(device, command, timeout=None, check_exit_code=False, as_root=False): # NOQA # pylint: disable=too-many-branches, too-many-locals, too-many-statements _check_env() if as_root: command = 'echo \'{}\' | su'.format(escape_single_quotes(command)) device_part = ['-s', device] if device else [] device_string = ' {} {}'.format(*device_part) if device_part else '' full_command = 'adb{} shell "{}"'.format(device_string, escape_double_quotes(command)) logger.debug(full_command) if check_exit_code: adb_shell_command = '({}); echo \"\n$?\"'.format(command) actual_command = ['adb'] + device_part + ['shell', adb_shell_command] try: raw_output, error = check_output(actual_command, timeout, shell=False) except CalledProcessErrorWithStderr as e: raw_output = e.output error = e.error exit_code = e.returncode if exit_code == 1: logger.debug( "Exit code 1 could be either the return code of the command or mean ADB failed" ) if raw_output: if raw_output.endswith('\r\n'): newline = '\r\n' elif raw_output.endswith('\n'): newline = '\n' else: raise WAError( "Unknown new line separator in: {}".format(raw_output)) try: output, exit_code, _ = raw_output.rsplit(newline, 2) except ValueError: exit_code, _ = raw_output.rsplit(newline, 1) output = '' else: # raw_output is empty exit_code = '969696' # just because output = '' exit_code = exit_code.strip() if exit_code.isdigit(): if int(exit_code): message = 'Got exit code {}\nfrom: {}\nSTDOUT: {}\nSTDERR: {}'.format( exit_code, full_command, output, error) raise DeviceError(message) elif am_start_error.findall(output): message = 'Could not start activity; got the following:' message += '\n{}'.format(am_start_error.findall(output)[0]) raise DeviceError(message) else: # not all digits if am_start_error.findall(output): message = 'Could not start activity; got the following:' message += '\n{}'.format(am_start_error.findall(output)[0]) raise DeviceError(message) else: message = 'adb has returned early; did not get an exit code. '\ 'Was kill-server invoked?\nOUTPUT:\n-----\n{}\n'\ '-----ERROR:\n-----\n{}\n-----' raise DeviceError(message.format(raw_output, error)) else: # do not check exit code try: output, error = check_output(full_command, timeout, shell=True) if output is None: output = error elif error is not None: output = '\n'.join([output, error]) except CalledProcessErrorWithStderr as e: output = e.error or e.output exit_code = e.returncode if e.returncode == 1: logger.debug( "Got Exit code 1, could be either the return code of the command or mean ADB failed" ) return output