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 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 _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
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, "",) ]