def find_executable_path(device, executable_name, run_as_cmd=None): """Find a device executable from its name This function calls which on the device to retrieve the absolute path of the executable. Args: device: the AndroidDevice object to use. executable_name: the name of the executable to find. run_as_cmd: if necessary, run-as or su command to prepend Returns: The absolute path of the executable. Raises: RuntimeError: could not find the executable. """ cmd = ["which", executable_name] if run_as_cmd: cmd = run_as_cmd + cmd try: output, _ = device.shell(cmd) return adb.split_lines(output)[0] except adb.ShellError: raise RuntimeError("Could not find executable '{}' on " "device".format(executable_name))
def parse_ps_output(output): processes = dict() output = adb.split_lines(output.replace("\r", "")) columns = output.pop(0).split() try: pid_column = columns.index("PID") except ValueError: pid_column = 1 while output: columns = output.pop().split() process_name = columns[-1] pid = int(columns[pid_column]) if process_name in processes: processes[process_name].append(pid) else: processes[process_name] = [pid] return processes
def get_processes(device): """Return a dict from process name to list of running PIDs on the device.""" # Some custom ROMs use busybox instead of toolbox for ps. Without -w, # busybox truncates the output, and very long package names like # com.exampleisverylongtoolongbyfar.plasma exceed the limit. # # Perform the check for this on the device to avoid an adb roundtrip # Some devices might not have readlink or which, so we need to handle # this as well. ps_script = """ if [ ! -x /system/bin/readlink -o ! -x /system/bin/which ]; then ps; elif [ $(readlink $(which ps)) == "toolbox" ]; then ps; else ps -w; fi """ ps_script = " ".join([line.strip() for line in ps_script.splitlines()]) output, _ = device.shell([ps_script]) processes = dict() output = adb.split_lines(output.replace("\r", "")) columns = output.pop(0).split() try: pid_column = columns.index("PID") except ValueError: pid_column = 1 while output: columns = output.pop().split() process_name = columns[-1] pid = int(columns[pid_column]) if process_name in processes: processes[process_name].append(pid) else: processes[process_name] = [pid] return processes