def main()

in daisy_workflows/image_import/inspection/src/boot_inspect/cli.py [0:0]


def main():
  format_options_and_help = {
    'json': 'JSON without newlines. Suitable for consumption by '
            'another program.',
    'human': 'Readable format that includes newlines and indents.',
    'daisy': 'Key-value format supported by Daisy\'s serial log collector.',
  }

  parser = argparse.ArgumentParser(
    description='Find boot-related properties of a disk.')
  parser.add_argument(
    '--format',
    choices=format_options_and_help.keys(),
    default='human',
    help=' '.join([
      '`%s`: %s' % (key, value)
      for key, value in format_options_and_help.items()
    ]))
  parser.add_argument(
    '--device',
    help='a block device (e.g. /dev/sdb).'
  )
  parser.add_argument(
    '--disk_file',
    help='The local path of a virtual disk file to inspect.'
  )

  args = parser.parse_args()
  if args.device is None and args.disk_file is None:
    print('either --disk_file or --device has to be specified')
    sys.exit(1)
  if args.device is not None and args.disk_file is not None:
    print('either --disk_file or --device has to be specified, but not both')
    sys.exit(1)

  disk_to_inspect = args.disk_file

  if args.device is not None:
    disk_to_inspect = args.device
    wait_for_device(disk_to_inspect)
  elif file_exists(args.disk_file) is False:
    sys.exit(1)

  results = inspect_pb2.InspectionResults()
  try:
    g = guestfs.GuestFS(python_return_dict=True)
    g.add_drive_opts(disk_to_inspect, readonly=1)
    g.launch()
  except BaseException as e:
    print('Failed to mount guest: ', e)
    results.ErrorWhen = inspect_pb2.InspectionResults.ErrorWhen.MOUNTING_GUEST
    globals()['_output_' + args.format](results)
    return

  try:
    print('Inspecting OS')
    results = inspection.inspect_device(g)
  except BaseException as e:
    print('Failed to inspect OS: ', e)
    results.ErrorWhen = inspect_pb2.InspectionResults.ErrorWhen.INSPECTING_OS

  try:
    boot_results = inspection.inspect_boot_loader(g, disk_to_inspect)
    results.bios_bootable = boot_results.bios_bootable
    results.uefi_bootable = boot_results.uefi_bootable
    results.root_fs = boot_results.root_fs
  except BaseException as e:
    print('Failed to inspect boot loader: ', e)
    results.ErrorWhen = \
        inspect_pb2.InspectionResults.ErrorWhen.INSPECTING_BOOTLOADER

  globals()['_output_' + args.format](results)