def test_command_loading(self, _get_user_command_path): _get_user_command_path.return_value = self.user_command_path available_commands = Command.get_available_commands() # Check that we have the command list that we expect self.assertEquals( sorted(available_commands), sorted( [b.replace('-', '_') for b in self.expected_builtins] + ["user_command_1"] ), ) # Check that we can load (i.e. import) every builtin command for expected_builtin in self.expected_builtins: self.assertIn(expected_builtin.replace("-", "_"), available_commands) cmd_cls = Command.load_command_class(expected_builtin) self.assertIsNotNone(cmd_cls) self.assertEquals(cmd_cls.get_name(), expected_builtin) # Check that we can load the user command user_cmd_cls = Command.load_command_class("user-command-1") self.assertIsNotNone(user_cmd_cls) self.assertEquals(user_cmd_cls.get_name(), "user-command-1") # Check that load_command_class() returns None for junk commands unexpected_cmd = Command.load_command_class("no-such-command") self.assertIsNone(unexpected_cmd)
def test_command_loading(self, _get_user_command_path): _get_user_command_path.return_value = self.user_command_path available_commands = Command.get_available_commands() # Check that we have the command list that we expect self.assertEquals( sorted(available_commands), sorted([b.replace('-', '_') for b in self.expected_builtins] + ["user_command_1"]), ) # Check that we can load (i.e. import) every builtin command for expected_builtin in self.expected_builtins: self.assertIn(expected_builtin.replace("-", "_"), available_commands) cmd_cls = Command.load_command_class(expected_builtin) self.assertIsNotNone(cmd_cls) self.assertEquals(cmd_cls.get_name(), expected_builtin) # Check that we can load the user command user_cmd_cls = Command.load_command_class("user-command-1") self.assertIsNotNone(user_cmd_cls) self.assertEquals(user_cmd_cls.get_name(), "user-command-1") # Check that load_command_class() returns None for junk commands unexpected_cmd = Command.load_command_class("no-such-command") self.assertIsNone(unexpected_cmd)
def _generate_usage(self): usage = "Usage: ripe-atlas <command> [arguments]\n\n" usage += "Commands:\n" longest_command = 0 classes = [] for c in Command.get_available_commands(): if c == "shibboleet": continue cmd_class = Command.load_command_class(c) classes.append(cmd_class) cmd_name = cmd_class.get_name() if len(cmd_name) > longest_command: longest_command = len(cmd_name) for cmd_cls in classes: usage += "\t{} {}\n".format( cmd_cls.get_name().ljust(longest_command + 1), cmd_cls.DESCRIPTION, ) usage += ("\nFor help on a particular command, try " "ripe-atlas <command> --help") return usage
def compare_type(cmd_name, api_model, expected_differences): print(cmd_name) cmd = Command.load_command_class("measure")(["measure", cmd_name]).create() cmd.add_arguments() args = {} for arg in cmd.parser._actions: args[arg.dest] = arg seen_diffs = False for field_name, model_field in sorted(api_model["properties"].items()): if field_name in expected_differences and expected_differences[ field_name] is None: continue if model_field["readOnly"]: continue explicit_values = expected_differences.get(field_name, {}) opt_name = explicit_values.get("alias", field_name) if opt_name in args: cmd_field = args.get(opt_name) expected_default = explicit_values.get( "default", model_field.get("defaultValue")) if cmd_field.default != expected_default: print("\t", field_name, "DEFAULT", repr(cmd_field.default), repr(expected_default)) seen_diffs |= True expected_min = explicit_values.get("minimum", model_field.get("minimum")) cmd_min = getattr(cmd_field.type, "minimum", None) if cmd_min != expected_min: print("\t", field_name, "MINIMUM", cmd_min, expected_min) seen_diffs |= True expected_max = explicit_values.get("maximum", model_field.get("maximum")) cmd_max = getattr(cmd_field.type, "maximum", None) if cmd_max != expected_max: print("\t", field_name, "MAXIMUM", cmd_max, expected_max) seen_diffs |= True else: print("\t", field_name, "\t", "MISSING") seen_diffs |= True if not seen_diffs: print("\t", "OK")
def autocomplete(self): """ This function is highly inspired from Django's own autocomplete manage.py. For more documentation check https://github.com/django/django/blob/1.9.4/django/core/management/__init__.py#L198-L270 """ def print_options(options, curr): """ Prints matching with current word available autocomplete options in a formatted way to look good on bash. """ sys.stdout.write(' '.join( sorted(filter(lambda x: x.startswith(curr), options)))) # If we are not autocompleting continue as normal if 'RIPE_ATLAS_AUTO_COMPLETE' not in os.environ: return cwords = os.environ['COMP_WORDS'].split()[1:] cword = int(os.environ['COMP_CWORD']) try: curr = cwords[cword - 1] except IndexError: curr = '' commands = list(Command.get_available_commands()) # base caller ripe-atlas if cword == 1: print_options(commands, curr) # special measure command elif cword == 2 and cwords[0] == "measure": print_options(BaseFactory.TYPES.keys(), curr) # rest of commands elif cwords[0] in commands: cmd = self.fetch_command_class(cwords[0], cwords) cmd.add_arguments() options = [ sorted(s_opt.option_strings)[0] for s_opt in cmd.parser._actions if s_opt.option_strings ] previous_options = [x for x in cwords[1:cword - 1]] options = [opt for opt in options if opt not in previous_options] print_options(options, curr) sys.exit(1)
def fetch_command_class(self, command, arg_options): """Fetches the class responsible for the given command.""" cmd_cls = Command.load_command_class(command) if cmd_cls is None: # Module containing the command class wasn't found raise RipeAtlasToolsException("No such command") # # If the imported module contains a `Factory` class, execute that # to get the `cmd` we're going to use. Otherwise, we expect there # to be a `Command` class in there. # if issubclass(cmd_cls, Factory): cmd = cmd_cls(arg_options).create() else: cmd = cmd_cls(*self.args, **self.kwargs) return cmd
def test_deprecated_aliases(self): aliases = [ ("measurement", "measurement-info"), ("measurements", "measurement-search"), ("probe", "probe-info"), ("probes", "probe-search"), ] # Check that each alias is loaded correctly and outputs a warning stderr = sys.stderr sys.stderr = StringIO() try: for alias, cmd_name in aliases: sys.stderr.truncate() cmd_cls = Command.load_command_class(alias) self.assertIn( "{} is a deprecated alias for {}".format(alias, cmd_name), sys.stderr.getvalue(), ) self.assertIsNotNone(cmd_cls) self.assertEquals(cmd_cls.get_name(), cmd_name) finally: sys.stderr = stderr
import os import tempfile from ripe.atlas.tools.commands.base import Command hiddenimports = [] hiddenimports.append("OpenSSL") hiddenimports.append("cffi") commands = [] mod = "ripe.atlas.tools.commands.{}" for command in Command.get_available_commands(): hiddenimports.append(mod.format(command)) commands.append(command) tmp_dir = tempfile.mkdtemp() with open(os.path.join(tmp_dir, "commands.list"), "w") as tmp: tmp.write(" ".join(commands)) datas = [ (tmp.name, "",) ]