Exemplo n.º 1
0
def mk_python(basename,
              prefix=None,
              base_prefix=None,
              executable=True,
              content=None,
              folder=None,
              version=None):
    if version is None:
        m = RE_VERSION.match(basename)
        if m:
            if not folder:
                folder = os.path.join(".pyenv/versions", basename)

            version = m.group(2)
            basename = "python"

    if not folder:
        folder = ".pyenv/versions"

    path = runez.resolved_path(folder)
    if not prefix:
        prefix = path

    if not base_prefix:
        base_prefix = prefix

    path = os.path.join(path, "bin", basename)
    if not content:
        content = [version, prefix, base_prefix]

    content = "#!/bin/bash\n%s\n" % "\n".join("echo %s" % s for s in content)
    runez.write(path, content, logger=None)
    if executable:
        runez.make_executable(path, logger=None)
Exemplo n.º 2
0
def test_ensure_freeze(_, __, temp_base):
    # Test edge case for _installed_module()
    with SoftLock(temp_base) as lock:
        fake_pex = os.path.join(temp_base, "bin/pex")
        runez.touch(fake_pex)
        runez.make_executable(fake_pex)
        v = SharedVenv(lock, None)
        assert v._installed_module("pex")
Exemplo n.º 3
0
 def __init__(self, cfg, name, version):
     self.cfg = cfg
     self.name = name
     self.version = version
     self.entry_points = {name: name}
     self.pspec = PackageSpec(cfg, name, version=version)
     self.pspec.save_manifest(self.entry_points)
     self.venv = PythonVenv(self.pspec, create=False)
     self.venv_exe = os.path.join(self.pspec.install_path, "bin", name)
     runez.write(self.venv_exe, "#!/bin/bash\necho %s" % version)
     runez.make_executable(self.venv_exe)
Exemplo n.º 4
0
 def _install(self, target, source):
     wrapper = PICKLEY_WRAPPER if self.package_name == system.PICKLEY else GENERIC_WRAPPER
     contents = wrapper.lstrip().format(
         hook=self.hook,
         bg=self.bg,
         name=runez.quoted(self.package_name),
         pickley=runez.quoted(system.SETTINGS.base.full_path(
             system.PICKLEY)),
         source=runez.quoted(source),
     )
     runez.write(target, contents)
     runez.make_executable(target)
Exemplo n.º 5
0
def test_executable(temp_folder):
    with runez.CaptureOutput(dryrun=True) as logged:
        assert runez.make_executable("some-file") == 1
        assert "Would make some-file executable" in logged

    assert runez.touch("some-file") == 1
    assert runez.make_executable("some-file") == 1
    assert runez.is_executable("some-file")
    assert runez.make_executable("some-file") == 0

    assert runez.delete("some-file") == 1
    assert not runez.is_executable("some-file")

    with runez.CaptureOutput() as logged:
        assert runez.make_executable("/dev/null/some-file", fatal=False) == -1
        assert "does not exist, can't make it executable" in logged
Exemplo n.º 6
0
def test_wrapper(temp_base):
    repeater = os.path.join(temp_base, "repeat.sh")
    target = os.path.join(temp_base, system.PICKLEY)

    runez.write(repeater, "#!/bin/bash\n\necho :: $*\n")
    runez.make_executable(repeater)

    # Actual wrapper
    d = DeliveryMethodWrap(system.PackageSpec(system.PICKLEY))
    d.install(target, repeater)
    assert runez.run(target, "auto-upgrade",
                     "foo") == RunResult(":: auto-upgrade foo", "", 0)
    assert runez.run(target, "--debug", "auto-upgrade",
                     "foo") == RunResult(":: --debug auto-upgrade foo", "", 0)
    assert runez.run(target, "settings",
                     "-d") == RunResult(":: settings -d", "", 0)

    # Verify that we're triggering background auto-upgrade as expected
    d.hook = "echo "
    d.bg = ""
    d.install(target, repeater)

    result = runez.run(target, "settings", "-d")
    assert "nohup" in result.output
    assert "repeat.sh settings -d" in result.output

    result = runez.run(target, "auto-upgrade", "foo")
    assert "nohup" not in result.output
    assert "repeat.sh auto-upgrade foo" in result.output

    result = runez.run(target, "--debug", "auto-upgrade", "foo")
    assert "nohup" not in result.output
    assert "repeat.sh --debug auto-upgrade foo" in result.output

    runez.delete(repeater)
    result = runez.run(target, "foo", fatal=False)
    assert result.failed
    assert "Please reinstall with" in result.full_output

    assert os.path.exists(target)
    assert uninstall_existing(target, fatal=False) == 1
    assert not os.path.exists(target)
Exemplo n.º 7
0
    def _install(self, pspec, target, source):
        pickley = pspec.cfg.base.full_path(PICKLEY)
        if pspec.dashed == PICKLEY:
            wrapper = PICKLEY_WRAPPER

        else:
            wrapper = GENERIC_WRAPPER
            if runez.DEV.project_folder and not os.path.exists(pickley):
                # We're running from development venv
                pickley = pspec.cfg.program_path

        contents = wrapper.lstrip().format(
            hook=self.hook,
            bg=self.bg,
            name=runez.quoted(pspec.dashed, adapter=None),
            pickley=runez.quoted(pickley, adapter=None),
            source=runez.quoted(source, adapter=None),
        )
        runez.delete(target, logger=False)
        runez.write(target, contents, logger=False)
        runez.make_executable(target, logger=False)
Exemplo n.º 8
0
def test_capture(temp_folder, logged):
    chatter = runez.resolved_path("chatter")
    assert runez.write(chatter, CHATTER.strip(), fatal=False) == 1
    assert runez.make_executable(chatter, fatal=False) == 1

    assert runez.run(chatter, fatal=False) == "chatter"

    r = runez.run(chatter, include_error=True, fatal=False)
    assert r.startswith("chatter")
    assert "No such file" in r

    assert "Running: chatter" in logged
Exemplo n.º 9
0
def test_failed_read(*_):
    with runez.CaptureOutput() as logged:
        assert runez.get_lines("bar", fatal=False) is None
        assert "Can't read" in logged.pop()

        assert runez.write("bar", "some content", fatal=False)
        assert "Can't write" in logged.pop()

        assert runez.copy("some-file", "bar", fatal=False) == -1
        assert "Can't delete" in logged
        assert "Can't copy" in logged.pop()

        assert runez.make_executable("some-file", fatal=False) == -1
        assert "Can't chmod" in logged.pop()
Exemplo n.º 10
0
def test_facultative(cli):
    runez.save_json({"pinned": {
        "virtualenv": {
            "facultative": True
        }
    }}, dot_meta("config.json"))

    # Empty file -> proceed with install as if it wasn't there
    runez.touch("virtualenv")
    cli.expect_success("-n install virtualenv",
                       "Would state: Installed virtualenv")

    # Simulate pickley wrapper
    runez.write("virtualenv", "echo installed by pickley")
    runez.make_executable("virtualenv")
    cli.expect_success("-n install virtualenv",
                       "Would state: Installed virtualenv")

    # Unknown executable -> skip pickley installation (since facultative)
    runez.write("virtualenv", "echo foo")
    runez.make_executable("virtualenv")
    cli.expect_success(
        "-n install virtualenv",
        "Skipping installation of virtualenv: not installed by pickley")
    cli.expect_success("-n check virtualenv",
                       "skipped, not installed by pickley")

    # --force ignores 'facultative' setting
    cli.expect_failure("-n install --force virtualenv",
                       "Can't automatically uninstall virtualenv")

    # Simulate pickley symlink delivery
    dummy_target = dot_meta("foo")
    runez.touch(dummy_target)
    runez.symlink(dummy_target, "virtualenv")
    cli.expect_success("-n install virtualenv",
                       "Would state: Installed virtualenv")
Exemplo n.º 11
0
def test_failure(monkeypatch):
    monkeypatch.setattr(io, "open", runez.conftest.exception_raiser())
    monkeypatch.setattr(os, "unlink", runez.conftest.exception_raiser("bad unlink"))
    monkeypatch.setattr(shutil, "copy", runez.conftest.exception_raiser())
    monkeypatch.setattr(os.path, "exists", lambda _: True)
    monkeypatch.setattr(os.path, "isfile", lambda _: True)
    monkeypatch.setattr(os.path, "getsize", lambda _: 10)
    with runez.CaptureOutput() as logged:
        with patch("runez.file._do_delete"):
            with patch("pathlib.Path.exists", return_value=True):
                assert runez.copy("some-file", "bar", fatal=False) == -1
                assert "Can't copy" in logged.pop()

        assert runez.delete("some-file", fatal=False) == -1
        assert "Can't delete" in logged
        assert "bad unlink" in logged.pop()

        assert runez.write("bar", "some content", fatal=False)
        assert "Can't write" in logged.pop()

        if not runez.SYS_INFO.platform_id.is_windows:
            assert runez.make_executable("some-file", fatal=False) == -1
            assert "Can't chmod" in logged.pop()
Exemplo n.º 12
0
def test_relocate_venv_successfully(temp_base):
    with runez.CaptureOutput() as logged:
        original = "line 1: source\nline 2\n"
        runez.write("foo/bar/bin/baz", original, logger=logging.debug)
        runez.write("foo/bar/bin/empty", "", logger=logging.debug)
        runez.write("foo/bar/bin/python", "", logger=logging.debug)
        runez.make_executable("foo/bar/bin/baz")
        runez.make_executable("foo/bar/bin/empty")
        runez.make_executable("foo/bar/bin/python")
        assert "Created" in logged.pop()

        # Simulate already seen
        expected = ["line 1: source\n", "line 2\n"]
        assert system.relocate_venv("foo",
                                    "source",
                                    "dest",
                                    fatal=False,
                                    _seen={"foo"}) == 0
        assert runez.get_lines("foo/bar/bin/baz") == expected
        assert not logged

        # Simulate failure to write
        with patch("runez.write", return_value=-1):
            assert system.relocate_venv("foo", "source", "dest",
                                        fatal=False) == -1
        assert runez.get_lines("foo/bar/bin/baz") == expected
        assert not logged

        # Simulate effective relocation, by folder
        expected = ["line 1: dest\n", "line 2\n"]
        assert system.relocate_venv("foo", "source", "dest", fatal=False) == 1
        assert runez.get_lines("foo/bar/bin/baz") == expected
        assert "Relocated " in logged

        # Second relocation is a no-op
        assert system.relocate_venv("foo", "source", "dest", fatal=False) == 0

        # Test relocating a single file
        runez.write("foo/bar/bin/baz", original, logger=logging.debug)
        assert system.relocate_venv("foo/bar/bin/baz",
                                    "source",
                                    "dest",
                                    fatal=False) == 1
        assert "Relocated " in logged
Exemplo n.º 13
0
def test_executable(temp_folder):
    with runez.CaptureOutput(dryrun=True) as logged:
        assert runez.make_executable("some-file") == 1
        assert "Would make some-file executable" in logged.pop()
        assert runez.make_executable("some-file", logger=False) == 1
        assert not logged

    with runez.CaptureOutput() as logged:
        assert runez.touch("some-file") == 1
        assert "Touched some-file" in logged.pop()
        assert runez.delete("some-file") == 1
        assert "Deleted some-file" in logged.pop()
        assert runez.touch("some-file", logger=logging.debug) == 1
        assert "Touched some-file" in logged.pop()
        assert runez.make_executable("some-file", logger=logging.debug) == 1
        assert "Made 'some-file' executable" in logged.pop()
        assert runez.is_executable("some-file")
        assert runez.make_executable("some-file") == 0
        assert not logged

        assert runez.touch("some-file", logger=False) == 1
        assert runez.delete("some-file", logger=False) == 1
        assert not runez.is_executable("some-file")
        assert not logged

        assert runez.make_executable("/dev/null/some-file", fatal=False) == -1
        assert "does not exist, can't make it executable" in logged.pop()

        assert runez.make_executable("/dev/null/some-file",
                                     fatal=False,
                                     logger=None) == -1  # Don't log anything
        assert not logged

        assert runez.make_executable("/dev/null/some-file",
                                     fatal=False,
                                     logger=False) == -1  # Log errors only
        assert "does not exist, can't make it executable" in logged.pop()
Exemplo n.º 14
0
def test_python_installation(_, __, ___, ____, temp_base):
    system.DESIRED_PYTHON = "/dev/null/foo"
    p = system.target_python(fatal=False)
    assert not p.is_valid
    assert p.shebang() == "/dev/null/foo"

    system.DESIRED_PYTHON = None
    assert system.target_python(fatal=False).is_valid

    assert not system.PythonInstallation("").is_valid

    p = system.PythonInstallation("foo")
    assert str(p) == "python 'foo'"
    assert not p.is_valid
    assert p.problem == "No python installation 'foo' found"
    assert p.program_name == "python 'foo'"

    p = system.PythonInstallation("pythonx")
    assert not p.is_valid
    # assert p.problem == "pythonx is not installed"
    assert p.program_name == "pythonx"

    p = system.PythonInstallation("/usr/bin/python")
    assert str(p) == "/usr/bin/python [2.7]"
    assert p.is_valid
    assert p.problem is None
    assert p.program_name == "python2.7"
    assert p.short_name == "py27"
    assert p.executable == "/usr/bin/python"
    assert p.shebang(universal=True) == "/usr/bin/env python"
    assert p.shebang() == "/usr/bin/python"

    p = system.PythonInstallation("3.6")
    assert str(p) == "/test/python3.6/bin/python [3.6]"
    assert p.is_valid
    assert p.problem is None
    assert p.program_name == "python3.6"
    assert p.short_name == "py36"
    assert p.executable == "/test/python3.6/bin/python"
    assert p.shebang() == "/usr/bin/env python3.6"

    system.SETTINGS.cli.contents["python_installs"] = temp_base
    runez.touch("foo")
    runez.touch("python3.5")
    runez.touch("python3.7")

    p = system.PythonInstallation("python3")
    assert not p.is_valid
    assert p.problem == "'/test/python3' is not a valid python installation"
    assert p.program_name == "python3"

    p = system.PythonInstallation("py3.7")
    assert not p.is_valid
    assert p.problem == "python3.7 is not installed"

    runez.delete("python3.7")
    runez.touch("3.7.0/bin/python")
    runez.make_executable("3.7.0/bin/python")
    p = system.PythonInstallation("3.7")
    assert p.is_valid
    assert p.short_name == "py37"
    assert p.executable == os.path.join(temp_base, "3.7.0/bin/python")
Exemplo n.º 15
0
def test_bootstrap(cli, monkeypatch):
    with patch("pickley.bstrap.which", side_effect=mocked_which):
        with patch("pickley.bstrap.os.path.expanduser",
                   side_effect=mocked_expanduser):
            runez.write(
                ".local/bin/pickley",
                "#!/bin/sh\necho 0.1")  # Pretend we have an old pickley
            runez.make_executable(".local/bin/pickley")

            with patch("pickley.bstrap.get_python_version",
                       return_value=(3, 6)):  # urllib fails
                monkeypatch.setenv("__PYVENV_LAUNCHER__", "oh apple, why?"
                                   )  # macos oddity env var, should be removed
                cli.run("-n", main=bstrap.main)
                assert cli.succeeded
                assert "__PYVENV_LAUNCHER__" not in os.environ
                assert "Replacing older pickley 0.1" in cli.logged
                assert "Would run: python virtualenv.pyz" in cli.logged
                assert "Would run: .local/bin/.pickley/pickley/pickley-" in cli.logged

            # Simulate multiple base candidates given
            cli.run("-n", "-b", "~/.local/bin:foo/bar", main=bstrap.main)
            assert cli.failed
            assert "not suitable: ~/.local/bin, foo/bar" in cli.logged

            # Simulate seeding
            cli.run("0.1",
                    "-b",
                    "~/.local/bin",
                    "-m",
                    "my-mirror",
                    "-c",
                    '{"pyenv":"~/.pyenv"}',
                    main=bstrap.main)
            assert cli.succeeded
            assert "Seeding .local/bin/.pickley/config.json with {'pyenv': '~/.pyenv'}" in cli.logged
            assert "Seeding .config/pip/pip.conf with my-mirror" in cli.logged
            assert "pickley version 0.1 is already installed" in cli.logged
            assert list(runez.readlines(".config/pip/pip.conf")) == [
                "[global]", "index-url = my-mirror"
            ]
            assert list(
                runez.readlines(".local/bin/.pickley/config.json")) == [
                    "{", '  "pyenv": "~/.pyenv"', "}"
                ]

            monkeypatch.setenv("PATH", "foo/bar:%s" % os.environ["PATH"])
            runez.ensure_folder("foo/bar", logger=None)
            cli.run("-n", "-b", "~/.local/bin:foo/bar", main=bstrap.main)
            assert cli.succeeded
            assert "base: foo/bar" in cli.logged

            with patch("pickley.bstrap.which", return_value=None):
                with patch("pickley.bstrap.is_executable", return_value=False):
                    # Simulate no python 3
                    cli.run("-n", main=bstrap.main)
                    assert cli.failed
                    assert "Could not find python3 on this machine" in cli.logged

            with patch("pickley.bstrap.get_python_version",
                       return_value=(3, 9)):  # urllib fails
                cli.run("-n", main=bstrap.main)
                assert cli.succeeded
                assert " -mvenv " in cli.logged