Skip to content

Commit

Permalink
Path resolution by kernel manager and providers
Browse files Browse the repository at this point in the history
  • Loading branch information
krassowski committed Jan 31, 2024
1 parent 396e665 commit 72844bf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions jupyter_client/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,12 @@ def client(self, **kwargs: t.Any) -> BlockingKernelClient:
# Kernel management
# --------------------------------------------------------------------------

def resolve_path(self, path: str) -> t.Optional[str]:
"""Resolve path to given file."""
assert self.provisioner is not None
print('kernel manager resolving path!', path, 'to', self.provisioner.resolve_path(path))
return self.provisioner.resolve_path(path)

def update_env(self, *, env: t.Dict[str, str]) -> None:
"""
Allow to update the environment of a kernel manager.
Expand Down
12 changes: 12 additions & 0 deletions jupyter_client/provisioning/local_provisioner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Distributed under the terms of the Modified BSD License.
import asyncio
import os
import pathlib
import signal
import sys
from typing import TYPE_CHECKING, Any, Dict, List, Optional
Expand Down Expand Up @@ -31,6 +32,7 @@ class LocalProvisioner(KernelProvisionerBase): # type:ignore[misc]
pgid = None
ip = None
ports_cached = False
cwd = None

@property
def has_process(self) -> bool:
Expand Down Expand Up @@ -217,8 +219,18 @@ async def launch_kernel(self, cmd: List[str], **kwargs: Any) -> KernelConnection

self.pid = self.process.pid
self.pgid = pgid
self.cwd = kwargs.get('cwd', pathlib.Path.cwd())
return self.connection_info

async def resolve_path(self, path_str: str) -> Optional[str]:
"""Resolve path to given file."""
path = pathlib.Path(path_str).expanduser()
if path.is_absolute():
return path.as_posix()
if self.cwd:
return (pathlib.Path(self.cwd) / path).resolve().as_posix()
return None

@staticmethod
def _scrub_kwargs(kwargs: Dict[str, Any]) -> Dict[str, Any]:
"""Remove any keyword arguments that Popen does not tolerate."""
Expand Down
15 changes: 15 additions & 0 deletions jupyter_client/provisioning/provisioner_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,21 @@ def get_stable_start_time(self, recommended: float = 10.0) -> float:
"""
return recommended

def resolve_path(self, path: str) -> Optional[str]:
"""
Returns the path resolved relative to kernel working directory.
For example, path `my_code.py` for a kernel started in `/tmp/`
should result in `/tmp/my_code.py`, while path `~/test.py` for
a kernel started in `/home/my_user/` should resolve to the
(fully specified) `/home/my_user/test.py` path.
The provisioner may choose not to resolve any paths, or restrict
the resolution to paths local to the kernel working directory
to prevent path traversal and exposure of file system layout.
"""
return None

def _finalize_env(self, env: Dict[str, str]) -> None:
"""
Ensures env is appropriate prior to launch.
Expand Down

0 comments on commit 72844bf

Please sign in to comment.