Exemplo n.º 1
0
def xargs(cmd, varargs, **kwargs):
    """A simplified implementation of xargs.

    negate: Make nonzero successful and zero a failure
    """
    negate = kwargs.pop('negate', False)
    retcode = 0
    stdout = b''
    stderr = b''

    try:
        parse_shebang.normexe(cmd[0])
    except parse_shebang.ExecutableNotFoundError as e:
        return e.to_output()

    for run_cmd in partition(cmd, varargs, **kwargs):
        proc_retcode, proc_out, proc_err = cmd_output(
            *run_cmd, encoding=None, retcode=None
        )
        # This is *slightly* too clever so I'll explain it.
        # First the xor boolean table:
        #     T | F |
        #   +-------+
        # T | F | T |
        # --+-------+
        # F | T | F |
        # --+-------+
        # When negate is True, it has the effect of flipping the return code
        # Otherwise, the retuncode is unchanged
        retcode |= bool(proc_retcode) ^ negate
        stdout += proc_out
        stderr += proc_err

    return retcode, stdout, stderr
Exemplo n.º 2
0
def xargs(cmd, varargs, **kwargs):
    """A simplified implementation of xargs.

    negate: Make nonzero successful and zero a failure
    """
    negate = kwargs.pop('negate', False)
    retcode = 0
    stdout = b''
    stderr = b''

    try:
        parse_shebang.normexe(cmd[0])
    except parse_shebang.ExecutableNotFoundError as e:
        return e.to_output()

    for run_cmd in partition(cmd, varargs, **kwargs):
        proc_retcode, proc_out, proc_err = cmd_output(*run_cmd,
                                                      encoding=None,
                                                      retcode=None)
        # This is *slightly* too clever so I'll explain it.
        # First the xor boolean table:
        #     T | F |
        #   +-------+
        # T | F | T |
        # --+-------+
        # F | T | F |
        # --+-------+
        # When negate is True, it has the effect of flipping the return code
        # Otherwise, the retuncode is unchanged
        retcode |= bool(proc_retcode) ^ negate
        stdout += proc_out
        stderr += proc_err

    return retcode, stdout, stderr
Exemplo n.º 3
0
def test_normexe_is_a_directory(tmpdir):
    with tmpdir.as_cwd():
        tmpdir.join('exe').ensure_dir()
        exe = os.path.join('.', 'exe')
        with pytest.raises(OSError) as excinfo:
            parse_shebang.normexe(exe)
        msg, = excinfo.value.args
        assert msg == f'Executable `{exe}` is a directory'
Exemplo n.º 4
0
def test_normexe_is_a_directory(tmpdir):
    with tmpdir.as_cwd():
        tmpdir.join('exe').ensure_dir()
        exe = os.path.join('.', 'exe')
        with pytest.raises(OSError) as excinfo:
            parse_shebang.normexe(exe)
        msg, = excinfo.value.args
        assert msg == 'Executable `{}` is a directory'.format(exe)
Exemplo n.º 5
0
def xargs(cmd, varargs, **kwargs):
    """A simplified implementation of xargs.

    negate: Make nonzero successful and zero a failure
    target_concurrency: Target number of partitions to run concurrently
    """
    negate = kwargs.pop('negate', False)
    target_concurrency = kwargs.pop('target_concurrency', 1)
    retcode = 0
    stdout = b''
    stderr = b''

    try:
        parse_shebang.normexe(cmd[0])
    except parse_shebang.ExecutableNotFoundError as e:
        return e.to_output()

    partitions = partition(cmd, varargs, target_concurrency, **kwargs)

    def run_cmd_partition(run_cmd):
        return cmd_output(*run_cmd, encoding=None, retcode=None)

    threads = min(len(partitions), target_concurrency)
    with _thread_mapper(threads) as thread_map:
        results = thread_map(run_cmd_partition, partitions)

        for proc_retcode, proc_out, proc_err in results:
            # This is *slightly* too clever so I'll explain it.
            # First the xor boolean table:
            #     T | F |
            #   +-------+
            # T | F | T |
            # --+-------+
            # F | T | F |
            # --+-------+
            # When negate is True, it has the effect of flipping the return
            # code. Otherwise, the returncode is unchanged.
            retcode |= bool(proc_retcode) ^ negate
            stdout += proc_out
            stderr += proc_err

    return retcode, stdout, stderr
Exemplo n.º 6
0
def test_normexe_not_executable(tmpdir):  # pragma: win32 no cover
    tmpdir.join('exe').ensure()
    with tmpdir.as_cwd(), pytest.raises(OSError) as excinfo:
        parse_shebang.normexe('./exe')
    assert excinfo.value.args == ('Executable `./exe` is not executable',)
Exemplo n.º 7
0
def test_normexe_does_not_exist_sep():
    with pytest.raises(OSError) as excinfo:
        parse_shebang.normexe('./i-dont-exist-lol')
    assert excinfo.value.args == ('Executable `./i-dont-exist-lol` not found',)
Exemplo n.º 8
0
def test_normexe_gives_full_path():
    assert parse_shebang.normexe('echo') == _echo_exe()
    assert os.sep in _echo_exe()
Exemplo n.º 9
0
def test_normexe_already_full_path():
    assert parse_shebang.normexe(sys.executable) == sys.executable
Exemplo n.º 10
0
def test_normexe_gives_full_path():
    expected = distutils.spawn.find_executable('echo')
    assert parse_shebang.normexe('echo') == expected
    assert os.sep in expected
Exemplo n.º 11
0
def test_normexe_gives_full_path():
    expected = distutils.spawn.find_executable('echo')
    assert parse_shebang.normexe('echo') == expected
    assert os.sep in expected
Exemplo n.º 12
0
def test_normexe_already_full_path():
    assert parse_shebang.normexe(sys.executable) == sys.executable
Exemplo n.º 13
0
def test_normexe_does_not_exist():
    with pytest.raises(OSError) as excinfo:
        parse_shebang.normexe('i-dont-exist-lol')
    assert excinfo.value.args == ('Executable `i-dont-exist-lol` not found',)