def main()

in ioXt/uraniborg/scripts/python/automate_observation.py [0:0]


def main():
  args = parse_arguments()
  logger = set_up_logging(args)

  if not supported_platform(logger):
    logger.error("Sorry, your OS is currently unsupported for this script.")
    return

  if not verify_hubble(args, logger):
    return

  if not adb_installed(logger):
    return

  if not AdbWrapper.start_server(logger):
    return

  connected_devices = AdbWrapper.devices(logger)
  if not connected_devices or len(connected_devices) < 1:
    logger.error("No devices connected!")
    return

  logger.debug("There are %d connected device(s)", len(connected_devices))
  # Here we'll handle the cases where there can be 1 or more devices connected
  if len(connected_devices) > 1:
    # TODO(billylau): Branch off based on user-input - do all, or selectively
    logger.warning("More than 1 device connected!")

  results = {}
  for target_device in connected_devices:
    if target_device.unauthorized:
      logger.error("Please authorize device with serial number %s for ADB via "
                   "device GUI.", target_device.serial_number)
      continue

    # set up an adb_wrapper to be used throughout for this target device
    adb_wrapper = AdbWrapper(target_device.serial_number, logger)

    if is_hubble_installed(adb_wrapper, logger):
      logger.debug("Removing previous Hubble installation...")
      if not remove_previous_installation(adb_wrapper):
        logger.error("Failed to remove previous Hubble installation.")
        continue

    if is_xiaomi_phone(adb_wrapper, logger):
      logger.info("This is a Xiaomi phone.")
      adb_push_hubble(adb_wrapper, args.hubble)
      if not launch_xiaomi_file_explorer(adb_wrapper):
        logger.error("Failed to launch Xiaomi file explorer")
      while not is_hubble_installed(adb_wrapper, logger):
        logger.warning("Please manually install Hubble by launching the "
                       "\"Files Manager\" app (it may have been launched "
                       "for you) and navigate to the \"Downloads\" folder.")
        input("Press [ENTER] when you are done.")
    else:
      logger.info("This is not a Xiaomi phone. Regular workflow continues...")
      if not install_hubble(adb_wrapper, args, logger):
        logger.error("Error installing Hubble: %s", args.error_message)
        continue
    clear_logcat(adb_wrapper)

    if not launch_hubble(adb_wrapper):
      logger.error("Failed to launch Hubble: %s", adb_wrapper.error_message)
      continue
    results_source = wait_for_results(adb_wrapper, logger)

    if not results_source:
      logger.error("Failed to obtain results from Hubble execution.")
      continue
    use_old_classification = args.use_old_results_classification is not None
    results_dir = extract_results(adb_wrapper, results_source, args.output,
                                  target_device, logger, use_old_classification)

    if not results_dir:
      logger.error("Failed to extract results from target device (%s).",
                   target_device.serial_number)

    extract_selinux_policies(adb_wrapper, results_dir, logger)
    results[target_device.serial_number] = results_dir

  for device in results:
    logger.info("SUCCESS! Hubble was successfully deployed and executed on "
                "connected device %s.", device)
    logger.info("Hubble output files can be found at: %s", results[device])