docs: def run_agent(device_type):
      """Run a simple GPG-agent server."""
      p = argparse.ArgumentParser()
      p.add_argument('--homedir', default=os.environ.get('GNUPGHOME'))
      p.add_argument('-v', '--verbose', default=0, action='count')
      p.add_argument('--server', default=False, action='store_true',
                     help='Use stdin/stdout for communication with GPG.')
  
      p.add_argument('--pin-entry-binary', type=str, default='pinentry',
                     help='Path to PIN entry UI helper.')
      p.add_argument('--passphrase-entry-binary', type=str, default='pinentry',
                     help='Path to passphrase entry UI helper.')
      p.add_argument('--cache-expiry-seconds', type=float, default=float('inf'),
                     help='Expire passphrase from cache after this duration.')
  
      args, _ = p.parse_known_args()
  
      assert args.homedir
  
      log_file = os.path.join(args.homedir, 'gpg-agent.log')
      util.setup_logging(verbosity=args.verbose, filename=log_file)
  
      log.debug('sys.argv: %s', sys.argv)
      log.debug('os.environ: %s', os.environ)
      log.debug('pid: %d, parent pid: %d', os.getpid(), os.getppid())
      try:
          env = {'GNUPGHOME': args.homedir, 'PATH': os.environ['PATH']}
          pubkey_bytes = keyring.export_public_keys(env=env)
          device_type.ui = device.ui.UI(device_type=device_type,
                                        config=vars(args))
          device_type.ui.cached_passphrase_ack = util.ExpiringCache(
              seconds=float(args.cache_expiry_seconds))
          handler = agent.Handler(device=device_type(),
                                  pubkey_bytes=pubkey_bytes)
  
          sock_server = _server_from_assuan_fd(os.environ)
          if sock_server is None:
              sock_server = _server_from_sock_path(env)
  
          with sock_server as sock:
              for conn in agent.yield_connections(sock):
                  with contextlib.closing(conn):
                      try:
                          handler.handle(conn)
                      except agent.AgentStop:
                          log.info('stopping gpg-agent')
                          return
                      except IOError as e:
                          log.info('connection closed: %s', e)
                          return
                      except Exception as e:  # pylint: disable=broad-except
                          log.exception('handler failed: %s', e)
  
      except Exception as e:  # pylint: disable=broad-except
          log.exception('gpg-agent failed: %s', e)