Ejemplo n.º 1
0
def venv_resolve_deps(
    deps,
    which,
    project,
    pre=False,
    clear=False,
    allow_global=False,
    pypi_mirror=None,
):
    from .vendor.vistir.misc import fs_str
    from .vendor import delegator
    from . import resolver
    import json

    if not deps:
        return []
    resolver = escape_grouped_arguments(resolver.__file__.rstrip("co"))
    cmd = "{0} {1} {2} {3} {4}".format(
        escape_grouped_arguments(which("python", allow_global=allow_global)),
        resolver,
        "--pre" if pre else "",
        "--clear" if clear else "",
        "--system" if allow_global else "",
    )
    with temp_environ():
        os.environ = {fs_str(k): fs_str(val) for k, val in os.environ.items()}
        os.environ["PIPENV_PACKAGES"] = str("\n".join(deps))
        if pypi_mirror:
            os.environ["PIPENV_PYPI_MIRROR"] = str(pypi_mirror)
        os.environ["PIPENV_VERBOSITY"] = str(environments.PIPENV_VERBOSITY)
        c = delegator.run(cmd, block=True)
    try:
        assert c.return_code == 0
    except AssertionError:
        if environments.is_verbose():
            click_echo(c.out, err=True)
            click_echo(c.err, err=True)
        else:
            click_echo(c.err[(int(len(c.err) / 2) - 1):], err=True)
        sys.exit(c.return_code)
    if environments.is_verbose():
        click_echo(c.out.split("RESULTS:")[0], err=True)
    try:
        return json.loads(c.out.split("RESULTS:")[1].strip())

    except IndexError:
        raise RuntimeError("There was a problem with locking.")
Ejemplo n.º 2
0
def locked_repository(requirement):
    from .vendor.vistir.path import create_tracked_tempdir
    if not requirement.is_vcs:
        return
    original_base = os.environ.pop("PIP_SHIMS_BASE_MODULE", None)
    os.environ["PIP_SHIMS_BASE_MODULE"] = fs_str("pipenv.patched.notpip")
    src_dir = create_tracked_tempdir(prefix="pipenv-", suffix="-src")
    try:
        with requirement.req.locked_vcs_repo(src_dir=src_dir) as repo:
            yield repo
    finally:
        if original_base:
            os.environ["PIP_SHIMS_BASE_MODULE"] = original_base
Ejemplo n.º 3
0
def venv_resolve_deps(deps,
                      which,
                      project,
                      pre=False,
                      clear=False,
                      allow_global=False,
                      pypi_mirror=None,
                      dev=False,
                      pipfile=None,
                      lockfile=None):
    from .vendor.vistir.misc import fs_str
    from .vendor.vistir.compat import Path, to_native_string, JSONDecodeError
    from .vendor.vistir.path import create_tracked_tempdir
    from . import resolver
    import json

    vcs_deps = []
    vcs_lockfile = {}
    results = []
    pipfile_section = "dev_packages" if dev else "packages"
    lockfile_section = "develop" if dev else "default"
    vcs_section = "vcs_{0}".format(pipfile_section)
    vcs_deps = getattr(project, vcs_section, [])
    if not deps and not vcs_deps:
        return {}

    if not pipfile:
        pipfile = getattr(project, pipfile_section, None)
    if not lockfile:
        lockfile = project._lockfile
    req_dir = create_tracked_tempdir(prefix="pipenv", suffix="requirements")
    if vcs_deps:
        with create_spinner(text=fs_str("Pinning VCS Packages...")) as sp:
            vcs_reqs, vcs_lockfile = get_vcs_deps(
                project,
                which=which,
                clear=clear,
                pre=pre,
                allow_global=allow_global,
                dev=dev,
            )
            vcs_deps = [req.as_line() for req in vcs_reqs if req.editable]
    cmd = [
        which("python", allow_global=allow_global),
        Path(resolver.__file__.rstrip("co")).as_posix()
    ]
    if pre:
        cmd.append("--pre")
    if clear:
        cmd.append("--clear")
    if allow_global:
        cmd.append("--system")
    with temp_environ():
        os.environ = {fs_str(k): fs_str(val) for k, val in os.environ.items()}
        os.environ["PIPENV_PACKAGES"] = str("\n".join(deps))
        if pypi_mirror:
            os.environ["PIPENV_PYPI_MIRROR"] = str(pypi_mirror)
        os.environ["PIPENV_VERBOSITY"] = str(environments.PIPENV_VERBOSITY)
        os.environ["PIPENV_REQ_DIR"] = fs_str(req_dir)
        os.environ["PIP_NO_INPUT"] = fs_str("1")
        with create_spinner(text=fs_str("Locking...")) as sp:
            c = resolve(cmd, sp)
            results = c.out
            if vcs_deps:
                with temp_environ():
                    os.environ["PIPENV_PACKAGES"] = str("\n".join(vcs_deps))
                    sp.text = to_native_string("Locking VCS Dependencies...")
                    vcs_c = resolve(cmd, sp)
                    vcs_results, vcs_err = vcs_c.out, vcs_c.err
            else:
                vcs_results, vcs_err = "", ""
            sp.green.ok(environments.PIPENV_SPINNER_OK_TEXT.format("Success!"))
    outputs = [results, vcs_results]
    if environments.is_verbose():
        for output in outputs:
            click_echo(output.split("RESULTS:")[0], err=True)
    try:
        results = json.loads(results.split("RESULTS:")[1].strip())
        if vcs_results:
            # For vcs dependencies, treat the initial pass at locking (i.e. checkout)
            # as the pipfile entry because it gets us an actual ref to use
            vcs_results = json.loads(vcs_results.split("RESULTS:")[1].strip())
            vcs_lockfile = prepare_lockfile(vcs_results, vcs_lockfile.copy(),
                                            vcs_lockfile)
        else:
            vcs_results = []

    except (IndexError, JSONDecodeError):
        for out, err in [(c.out, c.err), (vcs_results, vcs_err)]:
            click_echo(out.strip(), err=True)
            click_echo(err.strip(), err=True)
        raise RuntimeError("There was a problem with locking.")
    lockfile[lockfile_section] = prepare_lockfile(results, pipfile,
                                                  lockfile[lockfile_section])
    lockfile[lockfile_section].update(vcs_lockfile)
Ejemplo n.º 4
0
def venv_resolve_deps(
    deps,
    which,
    project,
    pre=False,
    clear=False,
    allow_global=False,
    pypi_mirror=None,
):
    from .vendor.vistir.misc import fs_str
    from .vendor.vistir.compat import Path, to_native_string, JSONDecodeError
    from .vendor.vistir.path import create_tracked_tempdir
    from .cmdparse import Script
    from .core import spinner
    from .vendor.pexpect.exceptions import EOF, TIMEOUT
    from .vendor import delegator
    from . import resolver
    from ._compat import decode_output
    import json

    if not deps:
        return []

    req_dir = create_tracked_tempdir(prefix="pipenv", suffix="requirements")
    cmd = [
        which("python", allow_global=allow_global),
        Path(resolver.__file__.rstrip("co")).as_posix()
    ]
    if pre:
        cmd.append("--pre")
    if clear:
        cmd.append("--clear")
    if allow_global:
        cmd.append("--system")
    with temp_environ():
        os.environ = {fs_str(k): fs_str(val) for k, val in os.environ.items()}
        os.environ["PIPENV_PACKAGES"] = str("\n".join(deps))
        if pypi_mirror:
            os.environ["PIPENV_PYPI_MIRROR"] = str(pypi_mirror)
        os.environ["PIPENV_VERBOSITY"] = str(environments.PIPENV_VERBOSITY)
        os.environ["PIPENV_REQ_DIR"] = fs_str(req_dir)
        os.environ["PIP_NO_INPUT"] = fs_str("1")
        out = to_native_string("")
        EOF.__module__ = "pexpect.exceptions"
        with spinner(text=fs_str("Locking..."), spinner_name=environments.PIPENV_SPINNER,
                nospin=environments.PIPENV_NOSPIN) as sp:
            c = delegator.run(Script.parse(cmd).cmdify(), block=False, env=os.environ.copy())
            _out = decode_output("")
            result = None
            while True:
                try:
                    result = c.expect(u"\n", timeout=environments.PIPENV_TIMEOUT)
                except (EOF, TIMEOUT):
                    pass
                if result is None:
                    break
                _out = c.subprocess.before
                if _out is not None:
                    _out = decode_output("{0}".format(_out))
                    out += _out
                    sp.text = to_native_string("{0}".format(_out[:100]))
                if environments.is_verbose():
                    if _out is not None:
                        sp._hide_cursor()
                        sp.write(_out.rstrip())
                        sp._show_cursor()
            c.block()
            if c.return_code != 0:
                sp.red.fail(environments.PIPENV_SPINNER_FAIL_TEXT.format(
                    "Locking Failed!"
                ))
                click_echo(c.out.strip(), err=True)
                click_echo(c.err.strip(), err=True)
                sys.exit(c.return_code)
            else:
                sp.green.ok(environments.PIPENV_SPINNER_OK_TEXT.format("Success!"))
    if environments.is_verbose():
        click_echo(c.out.split("RESULTS:")[0], err=True)
    try:
        return json.loads(c.out.split("RESULTS:")[1].strip())

    except (IndexError, JSONDecodeError):
        click_echo(c.out.strip(), err=True)
        click_echo(c.err.strip(), err=True)
        raise RuntimeError("There was a problem with locking.")