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: added --missing flag to list and remove subcommands #1056

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
51 changes: 49 additions & 2 deletions jupyter_client/kernelspecapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os.path
import sys
import typing as t
from pathlib import Path

from jupyter_core.application import JupyterApp, base_aliases, base_flags
from traitlets import Bool, Dict, Instance, List, Unicode
Expand All @@ -29,12 +30,20 @@ class ListKernelSpecs(JupyterApp):
help="output spec name and location as machine-readable json.",
config=True,
)

missing_kernels = Bool(
False,
help="List only specs with missing interpreters.",
config=True,
)
flags = {
"json": (
{"ListKernelSpecs": {"json_output": True}},
"output spec name and location as machine-readable json.",
),
"missing": (
{"ListKernelSpecs": {"missing_kernels": True}},
"output only missing kernels",
),
"debug": base_flags["debug"],
}

Expand All @@ -45,6 +54,10 @@ def start(self) -> dict[str, t.Any] | None: # type:ignore[override]
"""Start the application."""
paths = self.kernel_spec_manager.find_kernel_specs()
specs = self.kernel_spec_manager.get_all_specs()

if self.missing_kernels:
paths, specs = _limit_to_missing(paths, specs)

if not self.json_output:
if not specs:
print("No kernels available")
Expand Down Expand Up @@ -177,6 +190,11 @@ class RemoveKernelSpec(JupyterApp):

force = Bool(False, config=True, help="""Force removal, don't prompt for confirmation.""")
spec_names = List(Unicode())
missing_kernels = Bool(
False,
help="Remove missing specs.",
config=True,
)

kernel_spec_manager = Instance(KernelSpecManager)

Expand All @@ -185,6 +203,10 @@ def _kernel_spec_manager_default(self) -> KernelSpecManager:

flags = {
"f": ({"RemoveKernelSpec": {"force": True}}, force.help),
"missing": (
{"RemoveKernelSpec": {"missing_kernels": True}},
"remove missing kernels",
),
}
flags.update(JupyterApp.flags)

Expand All @@ -195,12 +217,22 @@ def parse_command_line(self, argv: list[str] | None) -> None: # type:ignore[ove
if self.extra_args:
self.spec_names = sorted(set(self.extra_args)) # remove duplicates
else:
self.exit("No kernelspec specified.")
self.spec_names = []

def start(self) -> None:
"""Start the application."""
self.kernel_spec_manager.ensure_native_kernel = False
spec_paths = self.kernel_spec_manager.find_kernel_specs()

if self.missing_kernels:
_, spec = _limit_to_missing(
spec_paths,
self.kernel_spec_manager.get_all_specs(),
)

# append missing kernels
self.spec_names = sorted(set(self.spec_names + list(spec)))

missing = set(self.spec_names).difference(set(spec_paths))
if missing:
self.exit("Couldn't find kernel spec(s): %s" % ", ".join(missing))
Expand Down Expand Up @@ -337,5 +369,20 @@ def start(self) -> None:
return self.subapp.start()


def _limit_to_missing(
paths: dict[str, str], specs: dict[str, t.Any]
) -> tuple[dict[str, str], dict[str, t.Any]]:
specs_: dict[str, t.Any] = {
k: v
for k, v in specs.items()
# If have kernel installed from current environment
# Probably not the best way to do this, but it works in edge cases I've run into.
if (prog := v["spec"]["argv"][0]) != "python" and not Path(prog).exists()
}

paths_: dict[str, str] = {k: v for k, v in paths.items() if k in specs_}
return paths_, specs_


if __name__ == "__main__":
KernelSpecApp.launch_instance()
5 changes: 5 additions & 0 deletions tests/test_kernelspecapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ def test_kernelspec_sub_apps(jp_kernel_dir):
specs = app3.start()
assert specs and "echo" not in specs

app4 = ListKernelSpecs(missing_kernels=True)
app4.kernel_spec_manager.kernel_dirs.append(kernel_dir)
specs = app4.start()
assert specs is None


def test_kernelspec_app():
app = KernelSpecApp()
Expand Down