Beispiel #1
0
def test_io_error(monkeypatch):
    print_hello = tmp_script_commandline(
        """from __future__ import print_function
import os
import sys

print("hello")
sys.stdout.flush()

sys.exit(0)
""")

    stdout_from_callback = []

    def on_stdout(data):
        stdout_from_callback.append(data)

    stderr_from_callback = []

    def on_stderr(data):
        stderr_from_callback.append(data)

    def mock_read(*args, **kwargs):
        raise IOError("Nope")

    monkeypatch.setattr(
        "anaconda_project.internal.streaming_popen._read_from_stream",
        mock_read)

    with pytest.raises(IOError) as excinfo:
        streaming_popen.popen(print_hello, on_stdout, on_stderr)

    assert "Nope" in str(excinfo.value)
Beispiel #2
0
def _call_conda(extra_args,
                json_mode=False,
                platform=None,
                stdout_callback=None,
                stderr_callback=None):
    assert len(extra_args) > 0  # we deref extra_args[0] below

    cmd_list = _get_conda_command(extra_args)
    command_in_errors = " ".join(cmd_list)

    env = None
    if platform is not None:
        env = os.environ.copy()
        env['CONDA_SUBDIR'] = platform

    try:
        (p, stdout_lines,
         stderr_lines) = streaming_popen.popen(cmd_list,
                                               env=env,
                                               stdout_callback=stdout_callback,
                                               stderr_callback=stderr_callback)
    except OSError as e:
        raise CondaError("failed to run: %r: %r" %
                         (command_in_errors, repr(e)))
    errstr = "".join(stderr_lines)
    if p.returncode != 0:
        parsed = None
        message = errstr
        if json_mode:
            try:
                out = "".join(stdout_lines)
                parsed = json.loads(out)
                if parsed is not None and isinstance(parsed, dict):
                    # some versions of conda do 'error' and others
                    # both 'error' and 'message' and they appear to
                    # be the same.
                    for field in ('message', 'error'):
                        if field in parsed:
                            message = parsed[field]
                            break
            except Exception:
                pass

        raise CondaError('%s: %s' % (command_in_errors, message), json=parsed)
    elif errstr != '' and stderr_callback is None:
        # this is a sort of fallback because not all of our code
        # passes in a callback yet.
        for line in stderr_lines:
            print("%s %s: %s" % ("conda", extra_args[0], line.strip()),
                  file=sys.stderr)

    return "".join(stdout_lines)
Beispiel #3
0
def _call_pip(prefix, extra_args, stdout_callback=None, stderr_callback=None):
    cmd_list = _get_pip_command(prefix, extra_args)

    try:
        (p, stdout_lines,
         stderr_lines) = streaming_popen.popen(cmd_list,
                                               stdout_callback=stdout_callback,
                                               stderr_callback=stderr_callback)
    except OSError as e:
        raise PipError("failed to run: %r: %r" % (" ".join(cmd_list), repr(e)))
    errstr = "".join(stderr_lines)
    if p.returncode != 0:
        raise PipError('%s: %s' % (" ".join(cmd_list), errstr))
    elif errstr != '':
        for line in errstr.split("\n"):
            print("%s %s: %s" % (cmd_list[0], cmd_list[1], line),
                  file=sys.stderr)
    return "".join(stdout_lines)
Beispiel #4
0
def test_callbacks_are_none():
    print_stuff = tmp_script_commandline(u"""# -*- coding: utf-8 -*-
from __future__ import print_function
import sys

print("a")
print("b", file=sys.stderr)

sys.exit(0)
""")

    (p, out_lines, err_lines) = streaming_popen.popen(print_stuff, None, None)

    expected_out = add_lineseps(['a'])
    expected_err = add_lineseps(['b'])

    assert expected_out == out_lines
    assert expected_err == err_lines

    assert p.returncode is 0
def test_bad_utf8():
    print_bad = tmp_script_commandline(u"""# -*- coding: utf-8 -*-
from __future__ import print_function
import os
import sys

print("hello")
sys.stdout.flush()
# write some garbage
os.write(sys.stdout.fileno(), b"\\x42\\xff\\xef\\xaa\\x00\\x01\\xcc")
sys.stdout.flush()
print("goodbye")
sys.stdout.flush()

sys.exit(0)
""")

    stdout_from_callback = []

    def on_stdout(data):
        stdout_from_callback.append(data)

    stderr_from_callback = []

    def on_stderr(data):
        stderr_from_callback.append(data)

    (p, out_lines, err_lines) = streaming_popen.popen(print_bad, on_stdout,
                                                      on_stderr)
    sep_out = detect_linesep(out_lines)

    expected_out = add_lineseps([u'hello', u'B��\x00\x01�goodbye'],
                                sep_out)
    expected_err = []

    assert expected_out == out_lines
    assert expected_err == err_lines

    assert "".join(expected_out) == "".join(stdout_from_callback)
    assert "".join(expected_err) == "".join(stderr_from_callback)
Beispiel #6
0
def test_streaming():
    print_stuff = tmp_script_commandline(u"""# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
import time

def flush():
    time.sleep(0.05)
    sys.stdout.flush()
    sys.stderr.flush()

print("a")
flush()
print("x", file=sys.stderr)
flush()
print("b")
flush()
print("y", file=sys.stderr)
flush()
print("c")
flush()
print("z", file=sys.stderr)
flush()
print("d")
flush()
# print partial lines with multiple syscalls
for i in [1,2,3,4,5,6]:
  sys.stdout.write("%d" % i)
sys.stdout.write("\\n")
# print unicode stuff, throws exception on Windows
try:
    print("💯 🌟")
    flush()
except Exception:
    print("Windows")
# print many lines at once, and end on non-newline
sys.stdout.write("1\\n2\\n3\\n4\\n5\\n6")
flush()

sys.exit(2)
""")

    stdout_from_callback = []

    def on_stdout(data):
        stdout_from_callback.append(data)

    stderr_from_callback = []

    def on_stderr(data):
        stderr_from_callback.append(data)

    (p, out_lines, err_lines) = streaming_popen.popen(print_stuff, on_stdout,
                                                      on_stderr)

    expected_out = add_lineseps([
        u'a', u'b', u'c', u'd', u'123456', u'💯 🌟', u'1', u'2', u'3',
        u'4', u'5'
    ])
    if platform.system() == 'Windows':
        # Windows can't output unicode
        if _PY2:
            expected_out[5] = u'\U0001f4af \U0001f31f\r\n'
        else:
            expected_out[5] = u"Windows\r\n"

    expected_out.append(u'6')  # no newline after this one
    expected_err = add_lineseps([u'x', u'y', u'z'])

    assert expected_out == out_lines
    assert "".join(expected_out) == "".join(stdout_from_callback)

    assert expected_err == err_lines
    assert "".join(expected_err) == "".join(stderr_from_callback)

    assert p.returncode is 2