def main_run(arglist, security_override=False, do_exit=True): """ Main function to be called with arguments list without the name of the program, aka $0 resp. sys.argv[0]. The security override and the exit boolean are only meant for test purposes """ # get a dictionary of discovered action plugins discovered_actions = actions_mgr.get_discovered_actions() # parse accordingly the arguments parsed_args = arguments.parse( arglist, "rdiff-backup {ver}".format(ver=Globals.version), actions_mgr.get_generic_parsers(), actions_mgr.get_parent_parsers_compat200(), discovered_actions) # instantiate the action object from the dictionary, handing over the # parsed arguments action = discovered_actions[parsed_args.action](parsed_args, Log, ErrorLog) # compatibility plug, we need verbosity set properly asap _parse_cmdlineoptions_compat200(parsed_args) # validate that everything looks good before really starting _validate_call(parsed_args.action, action.pre_check) # compatibility plug if parsed_args.action == "info": _output_info(exit=True) # now start for real, conn_act and action are the same object with action.connect() as conn_act: # For test purposes if security_override: Globals.security_level = "override" _validate_call(parsed_args.action, conn_act.check) _validate_call(parsed_args.action, conn_act.setup) return_val = 0 try: return_val = _take_action(conn_act.connected_locations) except SystemExit: raise except (Exception, KeyboardInterrupt) as exc: errmsg = robust.is_routine_fatal(exc) if errmsg: Log.exception(2, 6) Log.FatalError(errmsg) else: Log.exception(2, 2) raise if do_exit: sys.exit(return_val) else: # for test purposes return return_val
def test_parse_function(self): """ - verify that the --version option exits and make a few more smoke tests of the parse option """ disc_actions = actions_mgr.get_discovered_actions() # verify that the --version option exits the program with self.assertRaises(SystemExit): values = arguments.parse( ["--version"], "testing 0.0.1", actions_mgr.get_generic_parsers(), actions_mgr.get_parent_parsers_compat200(), disc_actions) # positive test of the parsing values = arguments.parse(["list", "increments", "dummy_test_repo"], "testing 0.0.2", actions_mgr.get_generic_parsers(), actions_mgr.get_parent_parsers_compat200(), disc_actions) self.assertEqual("list", values.action) self.assertEqual("increments", values.entity) self.assertIn("dummy_test_repo", values.locations) # negative test of the parsing due to too many or wrong arguments with self.assertRaises(SystemExit): values = arguments.parse( ["backup", "from", "to", "toomuch"], "testing 0.0.3", actions_mgr.get_generic_parsers(), actions_mgr.get_parent_parsers_compat200(), disc_actions) with self.assertRaises(SystemExit): values = arguments.parse( ["restore", "--no-such-thing", "from", "to"], "testing 0.0.4", actions_mgr.get_generic_parsers(), actions_mgr.get_parent_parsers_compat200(), disc_actions)
def error_check_Main(arglist): """Run Main on arglist, suppressing stack trace for routine errors""" parsed_args = arguments.parse( arglist, "rdiff-backup {ver}".format(ver=Globals.version), actions_mgr.get_generic_parsers(), actions_mgr.get_parent_parsers_compat200(), actions_mgr.get_discovered_actions()) _parse_cmdlineoptions_compat200(parsed_args) if parsed_args.action == "info": _output_info(exit=True) try: _Main(parsed_args) except SystemExit: raise except (Exception, KeyboardInterrupt) as exc: errmsg = robust.is_routine_fatal(exc) if errmsg: Log.exception(2, 6) Log.FatalError(errmsg) else: Log.exception(2, 2) raise
Adds the version option to the given parser The option is setup with the given version string. Returns nothing, the parser is modified "in-place". """ parser.add_argument("-V", "--version", action="version", version=version_string, help="[opt] output the rdiff-backup version and exit") # === MAIN === if __name__ == "__main__": """ We simulate the usage of arguments parsing in rdiff-backup. Call `python3 arguments.py [--new] --help` for usage. """ from rdiffbackup import actions_mgr disc_actions = actions_mgr.get_discovered_actions() values = parse(sys.argv[1:], "british-agent 0.0.7", actions_mgr.get_generic_parsers(), actions_mgr.get_parent_parsers_compat200(), disc_actions) action_object = disc_actions[values.action](values) action_object.print_values() # in real life, the action_object would then do the action for which # it's been created
def _main_run(arglist, security_override=False): """ Internal main function to be called with arguments list without the name of the program, aka $0 resp. sys.argv[0]. The security override is only meant for test purposes The function returns with an error code. """ # get a dictionary of discovered action plugins discovered_actions = actions_mgr.get_discovered_actions() # parse accordingly the arguments parsed_args = arguments.parse( arglist, "rdiff-backup {ver}".format(ver=Globals.version), actions_mgr.get_generic_parsers(), actions_mgr.get_parent_parsers_compat200(), discovered_actions) # we need verbosity set properly asap if parsed_args.terminal_verbosity is not None: log.Log.setterm_verbosity(parsed_args.terminal_verbosity) log.Log.setverbosity(parsed_args.verbosity) # compatibility plug _parse_cmdlineoptions_compat200(parsed_args) # instantiate the action object from the dictionary, handing over the # parsed arguments action = discovered_actions[parsed_args.action](parsed_args, log.Log, log.ErrorLog) # validate that everything looks good before really starting ret_val = action.pre_check() if ret_val != 0: log.Log( "Action {act} failed on {func}.".format(act=parsed_args.action, func="pre_check"), log.Log.ERROR) return ret_val # now start for real, conn_act and action are the same object with action.connect() as conn_act: # For test purposes only, hence we allow ourselves to overwrite a # "private" variable if security_override: from rdiff_backup import Security Security._security_level = "override" ret_val = conn_act.check() if ret_val != 0: log.Log( "Action {act} failed on {func}.".format(act=parsed_args.action, func="check"), log.Log.ERROR) return ret_val ret_val = conn_act.setup() if ret_val != 0: log.Log( "Action {act} failed on {func}.".format(act=parsed_args.action, func="setup"), log.Log.ERROR) return ret_val ret_val = conn_act.run() if ret_val != 0: log.Log( "Action {act} failed on {func}.".format(act=parsed_args.action, func="run"), log.Log.ERROR) return ret_val return ret_val