Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Expose installed binaries under PATH for custom packages #45

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions src/isolate/connections/_local/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@
ConnectionType = TypeVar("ConnectionType")


def binary_path_for(*search_paths: Path) -> str:
"""Return the binary search path for the given 'search_paths'.
It will be a combination of the 'bin/' folders in them and
the existing PATH environment variable."""

paths = []
for search_path in search_paths:
path = sysconfig.get_path("scripts", vars={"base": search_path})
paths.append(path)
# Some distributions (conda) might include both a 'bin' and
# a 'scripts' folder.

auxilary_binary_path = search_path / "bin"
if path != auxilary_binary_path and auxilary_binary_path.exists():
paths.append(str(auxilary_binary_path))

if "PATH" in os.environ:
paths.append(os.environ["PATH"])

return os.pathsep.join(paths)


def python_path_for(*search_paths: Path) -> str:
"""Return the PYTHONPATH for the library paths residing
in the given 'search_paths'. The order of the paths is
Expand Down Expand Up @@ -98,13 +120,22 @@ def get_env_vars(self) -> Dict[str, str]:
custom_vars = {}
custom_vars[AGENT_SIGNATURE] = "1"
custom_vars["PYTHONUNBUFFERED"] = "1"

# NOTE: we don't have to manually set PYTHONPATH here if we are
# using a single environment since python will automatically
# use the proper path.
if self.extra_inheritance_paths:
# The order here should reflect the order of the inheritance
# where the actual environment already takes precedence.
python_path = python_path_for(
custom_vars["PYTHONPATH"] = python_path_for(
self.environment_path, *self.extra_inheritance_paths
)
custom_vars["PYTHONPATH"] = python_path

# But the PATH must be always set since it will be not be
# automatically set by Python (think of this as ./venv/bin/activate)
custom_vars["PATH"] = binary_path_for(
self.environment_path, *self.extra_inheritance_paths
)

return {
**os.environ,
Expand Down
2 changes: 1 addition & 1 deletion src/isolate/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def Run(
future = local_pool.submit(
_proxy_to_queue,
queue=messages,
connection=connection,
connection=cast(LocalPythonGRPC, connection),
input=request.function,
)

Expand Down
25 changes: 25 additions & 0 deletions tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@ def test_invalid_project_building(self, tmp_path, monkeypatch):

assert not environment.exists()

@pytest.mark.parametrize("executable", ["python"])
def test_path_resolution(self, tmp_path, executable):
import shutil

environment = self.get_project_environment(tmp_path, "example-binary")
environment_path = environment.create()
with environment.open_connection(environment_path) as connection:
executable_path = Path(connection.run(partial(shutil.which, executable)))
assert executable_path.exists()
assert executable_path.relative_to(environment_path)


class TestVirtualenv(GenericEnvironmentTests):

Expand All @@ -151,6 +162,9 @@ class TestVirtualenv(GenericEnvironmentTests):
"invalid-project": {
"requirements": ["pyjokes==999.999.999"],
},
"example-binary": {
"requirements": [],
},
}
creation_entry_point = ("virtualenv.cli_run", PermissionError)

Expand Down Expand Up @@ -246,6 +260,9 @@ class TestConda(GenericEnvironmentTests):
"r": {
"packages": ["r-base=4.2.2"],
},
"example-binary": {
"packages": ["python"],
},
}
creation_entry_point = ("subprocess.check_call", subprocess.SubprocessError)

Expand Down Expand Up @@ -283,6 +300,14 @@ def test_local_python_environment():
local_env.destroy(connection_key)


def test_path_on_local():
import shutil

local_env = LocalPythonEnvironment()
with local_env.connect() as connection:
assert connection.run(partial(shutil.which, "python")) == shutil.which("python")


def test_isolate_server_environment(isolate_server):
environment = IsolateServer(
host=isolate_server,
Expand Down