예제 #1
0
def test_run_logged_pipe_command_log_cmd_file(tmp_stdout_file, tmp_cmd_file):
    """
    Test :py:func:`riboviz.process_utils.run_logged_pipe_command`
    using a single file to capture both standard output and standard
    error and a file to capture commands sent to the operating
    system.

    :param tmp_stdout_file: Output log file
    :type tmp_stdout_file: str or unicode
    :param tmp_cmd_file: Command file
    :type tmp_cmd_file: str or unicode
    """
    path = os.path.realpath(__file__)
    num_lines = len([line for line in open(path)])
    cmd1 = ["cat", path, "no-such-file", path]
    cmd2 = ["wc", "-l"]
    try:
        process_utils.run_logged_pipe_command(cmd1, cmd2, tmp_stdout_file,
                                              tmp_cmd_file)
    except AssertionError:
        pass
    lines = [line.rstrip('\n') for line in open(tmp_stdout_file)]
    assert len(lines) == 2
    assert lines[0] == "cat: no-such-file: No such file or directory"
    assert str(num_lines * 2) == lines[1]  # Output from wc
    with open(tmp_cmd_file) as f:
        actual_cmds = f.readlines()
    assert len(actual_cmds) == 1
    expected_cmd = "%s | %s" % (utils.list_to_str(cmd1),
                                utils.list_to_str(cmd2))
    assert actual_cmds[0].rstrip('\n') == expected_cmd
예제 #2
0
def test_run_logged_pipe_command_log_cmd_file_dry_run(tmp_stdout_file,
                                                      tmp_cmd_file):
    """
    Test :py:func:`riboviz.process_utils.run_logged_pipe_command`
    using a single file to capture both standard output and standard
    error and a file to capture commands sent to the operating
    system, with the ``dry_run`` parameter set to ``True``.

    :param tmp_stdout_file: Output log file
    :type tmp_stdout_file: str or unicode
    :param tmp_cmd_file: Command file
    :type tmp_cmd_file: str or unicode
    """
    path = os.path.realpath(__file__)
    cmd1 = ["cat", path, "no-such-file", path]
    cmd2 = ["wc", "-l"]
    process_utils.run_logged_pipe_command(cmd1, cmd2, tmp_stdout_file,
                                          tmp_cmd_file, True)
    with open(tmp_stdout_file) as f:
        lines = f.readlines()
    assert len(lines) == 0
    with open(tmp_cmd_file) as f:
        actual_cmds = f.readlines()
    assert len(actual_cmds) == 1
    expected_cmd = "%s | %s" % (utils.list_to_str(cmd1),
                                utils.list_to_str(cmd2))
    assert actual_cmds[0].rstrip('\n') == expected_cmd
예제 #3
0
def test_run_logged_redirect_command_cmd_file_dry_run(tmp_stderr_file,
                                                      tmp_redirect_file,
                                                      tmp_cmd_file):
    """
    Test :py:func:`riboviz.process_utils.run_logged_redirect_command`
    using a file to capture standard error and a file to capture
    commands sent to the operating system, with the ``dry_run``
    parameter set to ``True``.

    :param tmp_stderr_file: Error log file
    :type tmp_stderr_file: str or unicode
    :param tmp_redirect_file: File for redirected output
    :type tmp_redirect_file: str or unicode
    :param tmp_cmd_file: Command file
    :type tmp_cmd_file: str or unicode
    """
    path = os.path.realpath(__file__)
    cmd = ["cat", path, "no-such-file.txt", path]
    process_utils.run_logged_redirect_command(cmd, tmp_redirect_file,
                                              tmp_stderr_file, tmp_cmd_file,
                                              True)
    with open(tmp_redirect_file) as f:
        lines = f.readlines()
    assert len(lines) == 0
    with open(tmp_stderr_file) as f:
        lines = f.readlines()
    assert len(lines) == 0
    with open(tmp_cmd_file) as f:
        actual_cmds = f.readlines()
    assert len(actual_cmds) == 1
    expected_cmd = "%s > %s" % (utils.list_to_str(cmd), tmp_redirect_file)
    assert actual_cmds[0].rstrip('\n') == expected_cmd
예제 #4
0
def test_run_logged_redirect_command_cmd_file(tmp_stderr_file,
                                              tmp_redirect_file, tmp_cmd_file):
    """
    Test :py:func:`riboviz.process_utils.run_logged_redirect_command`
    using a file to capture standard error and a file to capture
    commands sent to the operating system.

    :param tmp_stderr_file: Error log file
    :type tmp_stderr_file: str or unicode
    :param tmp_redirect_file: File for redirected output
    :type tmp_redirect_file: str or unicode
    :param tmp_cmd_file: Command file
    :type tmp_cmd_file: str or unicode
    """
    path = os.path.realpath(__file__)
    cmd = ["cat", path, "no-such-file.txt", path]
    try:
        process_utils.run_logged_redirect_command(cmd, tmp_redirect_file,
                                                  tmp_stderr_file,
                                                  tmp_cmd_file)
    except AssertionError:
        pass
    # Compare path to captured redirect.
    with open(path) as expected, open(tmp_redirect_file) as actual:
        for line1, line2 in zip(expected, actual):
            assert line1 == line2
    lines = [line.rstrip('\n') for line in open(tmp_stderr_file)]
    assert len(lines) == 1
    assert lines[0] == \
        "cat: no-such-file.txt: No such file or directory"
    with open(tmp_cmd_file) as f:
        actual_cmds = f.readlines()
    assert len(actual_cmds) == 1
    expected_cmd = "%s > %s" % (utils.list_to_str(cmd), tmp_redirect_file)
    assert actual_cmds[0].rstrip('\n') == expected_cmd
예제 #5
0
def test_run_logged_command_cmd_file_cmd_to_log(tmp_stdout_file, tmp_cmd_file):
    """
    Test :py:func:`riboviz.process_utils.run_logged_command` using a
    single file to capture both standard output and standard error and
    a file to capture commands sent to the operating system, where the
    command to be logged differs from that submitted.

    :param tmp_stdout_file: Output log file
    :type tmp_stdout_file: str or unicode
    :param tmp_cmd_file: Command file
    :type tmp_cmd_file: str or unicode
    """
    path = os.path.realpath(__file__)
    cmd = ["ls", path, "no-such-file.txt", path]
    cmd_to_log = ["ls", path, "'no-such-file.txt'", path]
    try:
        process_utils.run_logged_command(cmd,
                                         tmp_stdout_file,
                                         tmp_cmd_file,
                                         cmd_to_log=cmd_to_log)
    except AssertionError:
        pass
    lines = [line.rstrip('\n') for line in open(tmp_stdout_file)]
    assert len(lines) == 3
    assert lines[0] == \
        "ls: cannot access 'no-such-file.txt': No such file or directory" \
        or lines[0] == \
        "ls: cannot access no-such-file.txt: No such file or directory"
    assert lines[1] == path  # Output from ls
    assert lines[2] == path  # Output from ls
    with open(tmp_cmd_file) as f:
        actual_cmds = f.readlines()
    assert len(actual_cmds) == 1
    assert actual_cmds[0].rstrip('\n') == utils.list_to_str(cmd_to_log)
예제 #6
0
def run_logged_command(cmd,
                       log_file,
                       cmd_file=None,
                       dry_run=False,
                       cmd_to_log=None):
    """
    Run operating system command via Python ``subprocess`` and capture
    standard output and standard error into a log file. Uses
    :py:func:`run_command`.

    If ``cmd_file`` is not ``None`` then the command submitted to the
    operating system is recorded into ``cmd_file``.

    If ``dry_run`` is ``True`` then the command will not be submitted
    to the operating system. Using this with ``cmd_file`` allows a
    record of the command that *would* be submitted to be made.

    For cases where the command to submit may differ depending on
    whether it is run via the command-line or via Python
    ``subprocess``, ``cmd_to_log`` can be used to provide the version
    of the command that needs to be inserted into the ``cmd_file``.

    :param cmd: Commnand and arguments
    :type cmd: list(str or unicode)
    :param log_file: Log file
    :type log_file: str or unicode
    :param cmd_file: Bash commands file
    :type cmd_file: str or unicode
    :param dry_run: Do not submit command to operating system?
    :type dry_run: bool
    :param cmd_to_log: Command to log
    :type cmd_to_log: list(str or unicode)
    :raise FileNotFoundError: if the command to run cannot be found
    :raise AssertionError: If the command returns a non-zero exit code
    """
    if cmd_file is not None:
        if cmd_to_log is not None:
            cmd_to_log_str = utils.list_to_str(cmd_to_log)
        else:
            cmd_to_log_str = utils.list_to_str(cmd)
        with open(cmd_file, "a") as f:
            f.write(cmd_to_log_str + "\n")
    if dry_run:
        return
    with open(log_file, "a") as f:
        run_command(cmd, f, f)
예제 #7
0
def run_logged_pipe_command(cmd1,
                            cmd2,
                            log_file,
                            cmd_file=None,
                            dry_run=False):
    """
    Run operating system command via Python ``subprocess`` and pipe
    output into another command and capture standard output and
    standard error into a log file. Uses :py:func:`run_pipe_command`.

    If ``cmd_file`` is not ``None`` then the command submitted to the
    operating system is recorded into ``cmd_file``.

    If ``dry_run`` is ``True`` then the command will not be submitted
    to the operating system. Using this with ``cmd_file`` allows a
    record of the command that *would* be submitted to be made.

    :param cmd1: Commnand and arguments
    :type cmd1: list(str or unicode)
    :param cmd2: Commnand and arguments
    :type cmd2: list(str or unicode)
    :param log_file: Log file
    :type log_file: str or unicode
    :param cmd_file: Bash commands file
    :type cmd_file: str or unicode
    :param dry_run: Do not submit command to operating system?
    :type dry_run: bool
    :raise FileNotFoundError: if the commands to run cannot be found
    :raise AssertionError: If the commands return a non-zero exit code
    """
    if cmd_file is not None:
        with open(cmd_file, "a") as f:
            f.write(("%s | %s\n" %
                     (utils.list_to_str(cmd1), utils.list_to_str(cmd2))))
    if dry_run:
        return
    with open(log_file, "a") as f:
        run_pipe_command(cmd1, cmd2, f, f)
예제 #8
0
def test_list_to_str_empty():
    """
    Test :py:func:`riboviz.utils.list_to_str` with an empty list.
    """
    list_str = utils.list_to_str([])
    assert list_str == "", "Unexpected string {}".format(list_str)
예제 #9
0
def test_list_to_str_int():
    """
    Test :py:func:`riboviz.utils.list_to_str` with a list of ``int``.
    """
    list_str = utils.list_to_str([1, 2, 3])
    assert list_str == "1 2 3", "Unexpected string {}".format(list_str)
예제 #10
0
def test_list_to_str():
    """
    Test :py:func:`riboviz.utils.list_to_str` with a list of ``str``.
    """
    list_str = utils.list_to_str(["ab", "cd", "ef"])
    assert list_str == "ab cd ef", "Unexpected string {}".format(list_str)