Ejemplo n.º 1
0
 def convert(self, command: List[str]) -> Command:
     if len(command) == 1:
         return parse_help(
             command, self.execute_with_sep(command), max_length=self.max_length
         )
     else:
         commands = []
         for sep in self.subcommand_sep:
             man_text = self.execute_with_sep(command, sep)
             commands.append(
                 parse_help(command, man_text, max_length=self.max_length)
             )
         return Command.best(commands)
Ejemplo n.º 2
0
def test_round_trip(bwamem_help):
    command = parse_help(["bwa", "mem"], bwamem_help)

    # Dump
    buffer = StringIO()
    yaml.dump(command, buffer)

    # Load
    buffer.seek(0)
    output = yaml.load(buffer)

    # Assert the round trip worked
    assert command == output
Ejemplo n.º 3
0
def test_all(test: HelpText):
    """
    A comprehensive end-to-end test that tests the parser and converters, using the test data files
    """
    with open(resource_filename("test", test.path)) as fp:
        help_text = fp.read()

    cmd = parse_help(test.cmd, help_text)

    # Check that the help text is included in the command
    assert cmd.help_text == help_text

    test.run_assertions(cmd, explore=False)
Ejemplo n.º 4
0
def test_long_text():
    """
    This tests the case where the parse function is handed an inordinate amount of text. In this case, we shouldn't
    bother parsing, and just return an empty command
    """
    text = "\n".join([
        "".join(
            random.choices(
                string.ascii_letters + " ",
                weights=[1] * len(string.ascii_letters) + [5],
                k=100,
            )) for i in range(2000)
    ])
    command = parse_help(["some", "command"], text=text)
    assert len(command.positional) == 0
    assert len(command.named) == 0
Ejemplo n.º 5
0
    def convert(
        self,
        cmd: List[str],
    ) -> Command:
        """
        Determine the best Command instance for a given command line tool, by trying many
        different help flags, such as --help and -h, then return the Command. Use this if you know the command you want to
        parse, but you don't know which flags it responds to with help text. Unlike :py:func:`aclimatise.explore_command`,
        this doesn't even attempt to parse subcommands.

        :param cmd: The command to analyse, e.g. ['wc'] or ['bwa', 'mem']
        :param flags: A list of help flags to try, e.g. ['--help', '-h'], in order how which one you would prefer to use.
        Generally [] aka no flags should be last
        :param executor: A class that provides the means to run a command. You can use the pre-made classes or write your own.
        """
        # For each help flag, run the command and then try to parse it
        logger.info("Trying flags for {}".format(" ".join(cmd)))
        commands = []
        for flag in self.flags:
            help_cmd = cmd + flag
            logger.info("Trying {}".format(" ".join(help_cmd)))
            try:
                final = self.execute(help_cmd)
                result = parse_help(cmd, final, max_length=self.max_length)
                result.generated_using = flag
                commands.append(result)
            except (ParseBaseException, UnicodeDecodeError) as e:
                # If parsing fails, this wasn't the right flag to use
                continue

        # Sort by flags primarily, and if they're equal, return the command with the longest help text, and if they're equal
        # return the command with the most help flags. This helps ensure we get ["bedtools", "--help"] instead of
        # ["bedtools"]
        best = Command.best(commands)
        logger.info("The best help flag seems to be {}".format(
            " ".join(best.command + best.generated_using)))
        return best
Ejemplo n.º 6
0
def test_bwa(parser, bwamem_help):
    # Parse help
    command = parse_help(["bwa", "mem"], text=bwamem_help)

    assert len(command.named) == 36
    assert len(command.positional) == 3
Ejemplo n.º 7
0
def test_bwa_root(bwa_help):
    command = parse_help(["bwa"], bwa_help)
    assert len(command.named) == 0
    assert len(command.positional) == 14
    assert command.positional[0].name == "index"
    assert command.positional[-1].name == "bwt2sa"