Esempio n. 1
0
 def _search_solr(self, line):
     """Perform a SOLR search.
 """
     try:
         query_str = self._create_solr_query(line)
         client = cli_client.CLICNClient(
             **self._cn_client_connect_params_from_session())
         object_list = client.search(
             queryType=d1_common.const.DEFAULT_SEARCH_ENGINE,
             query=query_str,
             start=self._session.get(session.START_NAME),
             rows=self._session.get(session.COUNT_NAME),
         )
         cli_util.print_info(self._pretty(object_list.toxml('utf-8')))
     except d1_common.types.exceptions.ServiceFailure as e:
         e = "%".join(str(e).splitlines())  # Flatten line
         regexp = re.compile(
             r"errorCode: (?P<error_code>\d+)%.*%Status code: (?P<status_code>\d+)"
         )
         result = regexp.search(e)
         if ((result is not None) and (result.group(u'error_code') == '500')
                 and (result.group(u'status_code') == '400')):  # noqa: E129
             result = re.search(
                 r"<b>description</b> <u>(?P<description>[^<]+)</u>", e)
             msg = re.sub(
                 u'&([^;]+);', lambda m: unichr(htmlentitydefs.
                                                name2codepoint[m.group(1)]),
                 result.group(u'description'))
             cli_util.print_info(u'Warning: %s' % msg)
         else:
             cli_util.print_error(u'Unexpected error:\n%s' % str(e))
Esempio n. 2
0
def handle_unexpected_exception(max_traceback_levels=100):
  """Suppress stack traces for common errors and provide hints for how to
  resolve them."""
  exc_type, exc_msgs = sys.exc_info()[:2]
  if exc_type.__name__ == 'SSLError':
    cli_util.print_error(
      """HTTPS / TLS / SSL / X.509v3 Certificate Error:
  An HTTPS connection could not be established. Verify that a DataONE node
  responds at the URL provided in the cn-url or mn-url session variable. If the
  URL is valid and if you intended to connect without authentication, make sure
  that the session variable, "anonymous", is set to True. If you intended to
  connect with authentication, make sure that the parameter, "cert-file", points
  to a valid certificate from CILogon. If the certificate has the private
  key in a separate file, also set "key-file" to the private key file.
  Otherwise, set "key-file" to None. Note that CILogon certificates must be
  renewed after 18 hours.
"""
    )
  elif exc_type.__name__ == 'timeout':
    cli_util.print_error(
      """Timeout error:
  A connection to a DataONE node timed out. Verify that a DataONE node responds
  at the URL provided in the cn-url or mn-url session variable.
"""
    )
  else:
    _print_unexpected_exception(max_traceback_levels)
Esempio n. 3
0
 def _prompt_edit_or_cancel(self, err_msg):
     while True:
         cli_util.print_error(err_msg)
         r = input('Edit again or Cancel (E/C)? ').strip()
         if r in ('E', 'e'):
             return False
         if r in ('C', 'c'):
             return True
Esempio n. 4
0
 def test_1180(self):
   """print()"""
   with d1_test.d1_test_case.capture_std() as (out_stream, err_stream):
     msg = 'test_msg'
     cli_util.print_debug(msg)
     cli_util.print_error(msg)
     cli_util.print_warn(msg)
     cli_util.print_info(msg)
     self.sample.assert_equals(out_stream.getvalue(), 'print')
Esempio n. 5
0
 def _launch_text_editor(self, path):
     editor = self._get_editor_command()
     cli_util.print_info('Launching editor: {}'.format(editor))
     try:
         subprocess.call([editor, path])
     except OSError:
         cli_util.print_error(
             'Unable to launch editor. Please set the editor session variable\n'
             'or the EDITOR environment variable to the filename of a valid editor\n'
             'executable on your system.')
Esempio n. 6
0
 def save(self, pickle_file_path=None, suppress_error=False):
   if pickle_file_path is None:
     pickle_file_path = self.get_default_pickle_file_path()
   try:
     with open(cli_util.os.path.expanduser(pickle_file_path), 'wb') as f:
       pickle.dump(self.__dict__, f, 2)
   except (NameError, IOError) as e:
     if not suppress_error:
       cli_util.print_error(
         'Unable to save session to file: {}\n{}'.
         format(pickle_file_path, str(e))
       )
Esempio n. 7
0
 def load(self, pickle_file_path=None, suppress_error=False):
   if pickle_file_path is None:
     pickle_file_path = self.get_default_pickle_file_path()
   try:
     with open(cli_util.os.path.expanduser(pickle_file_path), 'rb') as f:
       self.__dict__.update(pickle.load(f))
     #self._verify_session_variables()
   except (NameError, IOError, ImportError) as e:
     if not suppress_error:
       cli_util.print_error(
         'Unable to load session from file: {}\n{}'.
         format(pickle_file_path, str(e))
       )
Esempio n. 8
0
 def test_1180(self):
     """print()"""
     with d1_test.d1_test_case.capture_std() as (out_stream, err_stream):
         msg = 'test_msg'
         cli_util.print_debug(msg)
         cli_util.print_error(msg)
         cli_util.print_warn(msg)
         cli_util.print_info(msg)
     assert 'DEBUG    test_msg\n' \
       'ERROR    test_msg\n' \
       'WARN     test_msg\n' \
       '         test_msg\n' == \
       out_stream.getvalue()
Esempio n. 9
0
 def default(self, line):
   """Called on an input line when the command prefix is not recognized.
   """
   args = self._split_args(line, 0, 99)
   cli_util.print_error(u'Unknown command: {}'.format(args[0]))
Esempio n. 10
0
 def _print_ping_result(self, result, url):
     if result:
         cli_util.print_info('Responded:       {}'.format(url))
     else:
         cli_util.print_error('Did not respond: {}'.format(url))
Esempio n. 11
0
def main():
  if not check_dependencies.are_modules_importable():
    raise Exception('Dependency check failed')

  print('DataONE Command Line Interface ({})'.format(cli.__version__))

  parser = optparse.OptionParser(
    usage='usage: %prog [command] ...', option_list=option_list
  )
  options, commands = parser.parse_args()

  d1_cli = cli.CLI()
  handle_options(d1_cli, options)

  log_setup(options.debug)

  # If the user passed commands on the command line, run them.
  for command in commands:
    try:
      d1_cli.onecmd(command)
    except (cli_exceptions.InvalidArguments, cli_exceptions.CLIError) as e:
      cli_util.print_error(e)
    except d1_common.types.exceptions.DataONEException as e:
      cli_util.print_error('DataONE Node returned error:')
      cli_util.print_error(e)
    except:
      if options.debug:
        raise
      handle_unexpected_exception()

  # If interactive mode has been turned off, exit without entering main loop.
  if not options.interactive:
    return

  # Enter the main processing loop.
  while True:
    try:
      d1_cli.cmdloop()
    except KeyboardInterrupt as e:
      d1_cli.do_exit('')
    except SystemExit:
      break
    except (cli_exceptions.InvalidArguments, cli_exceptions.CLIError) as e:
      cli_util.print_error(e)
      #raise
    except d1_common.types.exceptions.DataONEException as e:
      # Suppress trace information in DataONEExceptions if not in debug mode.
      if not options.debug:
        e.traceInformation = None
      cli_util.print_error('DataONE Node returned error:')
      cli_util.print_error(e)
    except:
      if options.debug:
        raise
      handle_unexpected_exception()
Esempio n. 12
0
def _print_unexpected_exception(max_traceback_levels=100):
  exc_class, exc_msgs, exc_traceback = sys.exc_info()
  cli_util.print_error('Error:')
  cli_util.print_error('  Name: {}'.format(exc_class.__name__))
  cli_util.print_error('  Value: {}'.format(exc_msgs))
  try:
    exc_args = exc_msgs.__dict__["args"]
  except KeyError:
    exc_args = "<no args>"
  cli_util.print_error('  Args: {}'.format(exc_args))
  cli_util.print_error('  Traceback:')
  for tb in traceback.format_tb(exc_traceback, max_traceback_levels):
    cli_util.print_error('    {}'.format(tb))
Esempio n. 13
0
def handle_options(cli, options):
  try:
    if options.algorithm:
      cli.d1.session_set_parameter(session.CHECKSUM_NAME, options.algorithm)
    if options.anonymous:
      cli.d1.session_set_parameter(session.ANONYMOUS_NAME, options.anonymous)
    if options.authoritative_mn:
      cli.d1.session_set_parameter(
        session.AUTH_MN_NAME, options.authoritative_mn
      )
    if options.cert_file:
      cli.d1.session_set_parameter(
        session.CERT_FILENAME_NAME, options.cert_file
      )
    if options.count:
      cli.d1.session_set_parameter(session.COUNT_NAME, options.count)
    if options.cn_url:
      cli.d1.session_set_parameter(session.CN_URL_NAME, options.cn_url)
    if options.cn_host:
      url = ''.join((
        d1_common.const.DEFAULT_CN_PROTOCOL, '://', options.cn_host,
        d1_common.const.DEFAULT_CN_PATH
      ))
      cli.d1.session_set_parameter(session.CN_URL_NAME, url)
    if options.from_date:
      cli.d1.session_set_parameter(session.FROM_DATE_NAME, options.from_date)
    if options.key_file:
      cli.d1.session_set_parameter(session.KEY_FILENAME_NAME, options.key_file)
    if options.mn_url:
      cli.d1.session_set_parameter(session.MN_URL_NAME, options.mn_url)
    if options.mn_host:
      url = ''.join((
        d1_common.const.DEFAULT_MN_PROTOCOL, '://', options.mn_host,
        d1_common.const.DEFAULT_MN_PATH
      ))
      cli.d1.session_set_parameter(session.MN_URL_NAME, url)
    if options.object_format:
      cli.d1.session_set_parameter(session.FORMAT_NAME, options.object_format)
    if options.query_string:
      cli.d1.session_set_parameter(
        session.QUERY_STRING_NAME, options.query_string
      )
    if options.rights_holder:
      cli.d1.session_set_parameter(session.OWNER_NAME, options.rights_holder)
    if options.search_object_format:
      try:
        cli.d1.session_set_parameter(
          session.SEARCH_FORMAT_NAME, options.search_object_format
        )
      except ValueError as e:
        cli_util.print_error(e.args[0])
    if options.start:
      cli.d1.session_set_parameter(session.START_NAME, options.start)
    if options.to_date:
      cli.d1.session_set_parameter(session.TO_DATE_NAME, options.to_date)
    if options.verbose:
      cli.d1.session_set_parameter(session.VERBOSE_NAME, options.verbose)
    if options.editor:
      cli.d1.session_set_parameter(session.EDITOR_NAME, options.editor)
    # Replication.
    if options.action_allowReplication is not None:
      if options.action_allowReplication:
        cli.d1.replication_policy_set_replication_allowed(True)
      else:
        cli.d1.replication_policy_set_replication_allowed(False)
    if options.action_numReplicas:
      cli.d1.replication_policy_set_number_of_replicas(
        options.action_numReplicas
      )
    if options.action_blockNode:
      cli.d1.get_replication_policy().add_blocked(options.action_blockNode)
    if options.action_preferNode:
      cli.d1.get_replication_policy().add_preferred(options.action_preferNode)
  except cli_exceptions.InvalidArguments as e:
    cli_util.print_error(e)
  except:
    cli_util._handle_unexpected_exception()