class status(Module): # pylint: disable=invalid-name """Module class for getting the status of a device""" adb = DtfAdb.DtfAdb() def execute(self, args): # pylint: disable=unused-argument """Main module executor""" found = False serial = DtfAdb.get_mode_serial() devices = self.adb.get_devices() for device in devices: if device['serial'] == serial: found = True break print("Status:", end=" ") if found: print('Connected') else: print('Not Connected') print("Serial Number: %s" % serial) return 0
class status(Module): """Module class for getting the status of a device""" adb = DtfAdb.DtfAdb() def execute(self, args): """Main module executor""" found = False serial = prop.get_prop('Info', 'serial') devices = self.adb.get_devices() for device in devices: if device['serial'] == serial: found = True break print "Status:", if found: print "Connected" else: print "Not Connected" print "Serial Number: %s" % serial return 0
def do_init(self): """Perform the initialization""" log.i(TAG, "Project initialization started.") signal.signal(signal.SIGINT, self.do_shutdown) if os.path.isfile(utils.CONFIG_FILE_NAME): log.e(TAG, "Configuration file already exists!") return -1 compat.raw_input("\nPlease connect test device " "(press Enter to continue) ") # This might get in the way. try: del os.environ['ANDROID_SERIAL'] except KeyError: pass self.adb = adb.DtfAdb(no_serial=True) log.i(TAG, "Restarting adb...") self.adb.kill_server() self.adb.start_server() log.i(TAG, "Waiting for a device to be connected...") time.sleep(1) init_device = self.determine_device() if init_device is None: log.e(TAG, "Error determining device.") return -2 # Is this device offline? if init_device['status'] != adb.STATUS_DEVICE: log.e(TAG, "Cannot initialize offline/bootloader device!") log.e(TAG, "Try either: ") log.e(TAG, " 1. Run: adb kill-server && dtf init") log.e(TAG, " 2. Reboot the device.") return -3 # Initialize device if self.initialize_device(init_device) != 0: log.e(TAG, "Error initializing device!") return -4 log.i(TAG, "Device initialization complete!") return 0
def do_init(self): """Perform the initialization""" log.i(TAG, "Project initialization started.") signal.signal(signal.SIGINT, self.do_shutdown) if os.path.isfile(utils.CONFIG_FILE_NAME): log.e(TAG, "Configuration file already exists!") return -1 raw_input( "\nPlease connect the test device (press Enter to continue) ") # This might get in the way. try: del os.environ['ANDROID_SERIAL'] except KeyError: pass self.adb = DtfAdb.DtfAdb(no_serial=True) log.i(TAG, "Restarting adb...") self.adb.kill_server() self.adb.start_server() log.i(TAG, "Waiting for a device to be connected...") time.sleep(1) devices = self.get_devices() if len(devices) == 0: log.e(TAG, "No devices found, exiting.") return -2 elif len(devices) == 1: serial = devices[0] res = raw_input("Got serial '%s', is this correct? [Y/n] " % serial) if res.lower() == 'n': log.e(TAG, "Initialization aborted.") return -3 else: print "Found many devices. Please select from the following list:" i = 1 for device in devices: print "#%d. %s" % (i, device) i += 1 res = raw_input("\nWhich device #? ") try: int_res = int(res) serial = devices[int_res - 1] except (ValueError, IndexError): log.e(TAG, "Invalid input!") return -4 if self.initialize_device(serial) != 0: log.e(TAG, "Error initializing device!") return -5 else: log.i(TAG, "Device initialization complete!") return 0
def initialize_device(self, device_serial): """Perform the actual initialization""" log.d(TAG, "Preparing device: %s" % device_serial) touch(utils.CONFIG_FILE_NAME) set_prop('Info', 'serial', device_serial) # Since we have a serial now, lets create a new DtfAdb instance self.adb = DtfAdb.DtfAdb() # Kernel self.adb.shell_command('cat /proc/version') kernel = self.adb.get_output()[0] log.d(TAG, "Kernel version: %s" % kernel) set_prop('Info', 'kernel', kernel) # SDK sdk = self.getprop('ro.build.version.sdk') log.d(TAG, "Using SDK API %s" % sdk) set_prop('Info', 'SDK', sdk) self.adb.shell_command('set') set_output = self.adb.get_output() # $PATH path = get_set_value(set_output, 'PATH') if path is None: log.e(TAG, "Unable to get $PATH variable!") self.do_shutdown(None, None) log.d(TAG, "PATH : %s" % path) set_prop('Info', 'path', path) # $BOOTCLASSPTH bootclasspath = get_set_value(set_output, 'BOOTCLASSPATH') if bootclasspath is None: log.e(TAG, "Unable to get $BOOTCLASSPATH variable!") self.do_shutdown(None, None) log.d(TAG, "BOOTCLASSPATH : %s" % bootclasspath) set_prop('Info', 'bootclasspath-jars', bootclasspath) # Version string version_string = self.generate_version_string() log.d(TAG, "Using version string: %s" % version_string) set_prop('Info', 'version-string', version_string) # Determine CPU bitness cpu_bits = self.determine_cpu_bits() if cpu_bits is None: self.do_shutdown(None, None) log.d(TAG, "Using %s-bit CPU" % cpu_bits) set_prop('Info', 'cpu-bits', cpu_bits) # Set the VM type (Dalvik|Art) vm_type = self.determine_vm_type(sdk, cpu_bits) if vm_type is None: self.do_shutdown(None, None) log.d(TAG, "Determined runtime: %s" % vm_type) set_prop('Info', 'vmtype', vm_type) # Make project directories mkdir(REPORTS_DIRECTORY) mkdir(DBS_DIRECTORY) mkdir(LOCAL_MODULES_DIRECTORY) set_prop('Local', 'reports-dir', REPORTS_DIRECTORY) set_prop('Local', 'db-dir', DBS_DIRECTORY) # Invoke client installation rtn = pkg.launch_builtin_module('client', ['install']) if rtn != 0: log.w(TAG, "Unable to install dtf client. Try manually.") return 0
class client(Module): """Module class for dtf client""" adb = DtfAdb.DtfAdb() @classmethod def usage(cls): """Display module usage""" print "dtf Client Manager" print "Subcommands:" print " install Install the dtf client on device." print " status Print the install status of the client." print " remove Uninstall the dtf client." print " upload Upload file to dtf client directory." print "" return 0 def do_install(self): """Install the dtf client on device""" log.i(self.name, "Waiting for device to be connected...") self.adb.wait_for_device() log.i(self.name, "Removing old client if it exists...") self.adb.uninstall(DTF_CLIENT) log.i(self.name, "Installing dtf client...") self.adb.install(DTF_CLIENT_PATH) cmd = "am startservice -a com.dtf.action.name.INITIALIZE" self.adb.shell_command(cmd) busybox_path = "/data/data/%s/files/busybox" % DTF_CLIENT prop.set_prop('Info', 'busybox', busybox_path) log.i(self.name, "dtf client installed.") return 0 def do_status(self): """Print the install status of the client""" if self.adb.is_installed(DTF_CLIENT): print "dtf Client Status: Installed" print "" else: print "dtf Client Status: Not Installed" print "" def do_remove(self): """Uninstall the dtf client""" log.i(self.name, "Waiting for device to be connected...") self.adb.wait_for_device() log.i(self.name, "Removing dtf client...") self.adb.uninstall(DTF_CLIENT) prop.del_prop('Info', 'busybox') log.i(self.name, "dtf client removed!") return 0 def do_upload(self, args): """Upload file to dtf client directory""" parser = ArgumentParser(prog='client upload', description='Upload file to device.') parser.add_argument('--path', metavar="val", dest='upload_path', default=DEFAULT_PATH, help="Specify a upload point.") parser.add_argument('file_name', type=str, help='The file to upload.') args = parser.parse_args(args) file_name = args.file_name upload_path = args.upload_path if not isfile(file_name): log.e(self.name, "File does not exist: %s" % file_name) return -1 log.i(self.name, "Waiting for device to be connected...") self.adb.wait_for_device() # Is client installed? if not self.adb.is_installed(DTF_CLIENT): log.e(self.name, "dtf Client is not installed!") return -1 self.adb.push(file_name, upload_path) upload_file_name = "%s/%s" % (upload_path, file_name) dtf_upload_path = "/data/data/%s/" % DTF_CLIENT cmd = ("run-as %s cp %s %s" % (DTF_CLIENT, upload_file_name, dtf_upload_path)) self.adb.shell_command(cmd) return 0 def execute(self, args): """Main module executor""" self.name = self.__self__ rtn = 0 if len(args) < 1: return self.usage() sub_cmd = args.pop(0) if sub_cmd == 'install': rtn = self.do_install() elif sub_cmd == 'status': rtn = self.do_status() elif sub_cmd == 'remove': rtn = self.do_remove() elif sub_cmd == 'upload': rtn = self.do_upload(args) else: print "Sub-command '%s' not found!" % sub_cmd rtn = self.usage() return rtn
def initialize_device(self, init_device): """Perform the actual initialization""" device_serial = init_device['serial'] log.d(TAG, "Preparing device: %s" % device_serial) utils.touch(utils.CONFIG_FILE_NAME) set_prop('Info', 'serial', device_serial) # Set the client section. set_prop('Client', 'mode', adb.MODE_USB) # Since we have a serial now, lets create a new DtfAdb instance self.adb = adb.DtfAdb() # Kernel self.adb.shell_command('cat /proc/version') kernel = self.adb.get_output()[0] log.d(TAG, "Kernel version: %s" % kernel) set_prop('Info', 'kernel', kernel) # SDK sdk = self.getprop('ro.build.version.sdk') log.d(TAG, "Using SDK API %s" % sdk) set_prop('Info', 'SDK', sdk) if int(sdk) > const.API_MAX: log.w( TAG, "API %s isn't supported by dtf (yet), results may vary!" % sdk) self.adb.shell_command('set') set_output = self.adb.get_output() # $PATH path = get_set_value(set_output, 'PATH') if path is None: log.e(TAG, "Unable to get $PATH variable!") self.do_shutdown(None, None) log.d(TAG, "PATH : %s" % path) set_prop('Info', 'path', path) # $BOOTCLASSPTH bootclasspath = get_set_value(set_output, 'BOOTCLASSPATH') if bootclasspath is None: log.e(TAG, "Unable to get $BOOTCLASSPATH variable!") self.do_shutdown(None, None) log.d(TAG, "BOOTCLASSPATH : %s" % bootclasspath) set_prop('Info', 'bootclasspath-jars', bootclasspath) # Version string version_string = self.generate_version_string() log.d(TAG, "Using version string: %s" % version_string) set_prop('Info', 'version-string', version_string) # Determine architecture and CPU bitness arch, cpu_bits = self.determine_cpu_arch() if cpu_bits is None: self.do_shutdown(None, None) log.d(TAG, "CPU Architecture: %s" % arch) set_prop("Info", "cpu-arch", arch) log.d(TAG, "Using %s-bit CPU" % cpu_bits) set_prop('Info', 'cpu-bits', cpu_bits) # Set the VM type (Dalvik|Art) vm_type = self.determine_vm_type(sdk, cpu_bits) if vm_type is None: self.do_shutdown(None, None) log.d(TAG, "Determined runtime: %s" % vm_type) set_prop('Info', 'vmtype', vm_type) # Determine SEAndroid se_state = self.determine_seandroid_state() log.d(TAG, "Determine SEAndroid state: %s" % se_state) set_prop('Info', 'seandroid-state', se_state) # Setup the directory structure self.make_project_directories() # Set directory related properties set_prop('Local', 'reports-dir', utils.REPORTS_DIRECTORY) set_prop('Local', 'db-dir', utils.DBS_DIRECTORY) # Invoke client installation rtn = pkg.launch_builtin_module('client', ['install']) if rtn != 0: log.w(TAG, "Unable to install dtf client. Try manually.") return 0