From 1de6cee2a59288fa7bd2d8dddc35f2ea0b69296d Mon Sep 17 00:00:00 2001 From: Jan-Benedikt Jagusch Date: Tue, 15 Aug 2023 16:34:26 +0200 Subject: [PATCH 1/7] Move _get_vscode_cmd outside of setup_vscode function. --- jupyter_vscode_proxy/__init__.py | 44 ++++++++++++++++---------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/jupyter_vscode_proxy/__init__.py b/jupyter_vscode_proxy/__init__.py index 06d0698..f9c9e4f 100644 --- a/jupyter_vscode_proxy/__init__.py +++ b/jupyter_vscode_proxy/__init__.py @@ -1,33 +1,33 @@ import os import shutil +def _get_vscode_cmd(port): + executable = "code-server" + if not shutil.which(executable): + raise FileNotFoundError("Can not find code-server in PATH") + + # Start vscode in CODE_WORKINGDIR env variable if set + # If not, start in 'current directory', which is $REPO_DIR in mybinder + # but /home/jovyan (or equivalent) in JupyterHubs + working_dir = os.getenv("CODE_WORKINGDIR", ".") -def setup_vscode(): - def _get_vscode_cmd(port): - executable = "code-server" - if not shutil.which(executable): - raise FileNotFoundError("Can not find code-server in PATH") - - # Start vscode in CODE_WORKINGDIR env variable if set - # If not, start in 'current directory', which is $REPO_DIR in mybinder - # but /home/jovyan (or equivalent) in JupyterHubs - working_dir = os.getenv("CODE_WORKINGDIR", ".") + extensions_dir = os.getenv("CODE_EXTENSIONSDIR", None) + cmd = [ + executable, + "--auth", + "none", + "--disable-telemetry", + "--port=" + str(port), + ] - extensions_dir = os.getenv("CODE_EXTENSIONSDIR", None) - cmd = [ - executable, - "--auth", - "none", - "--disable-telemetry", - "--port=" + str(port), - ] + if extensions_dir: + cmd += ["--extensions-dir", extensions_dir] - if extensions_dir: - cmd += ["--extensions-dir", extensions_dir] + cmd.append(working_dir) + return cmd - cmd.append(working_dir) - return cmd +def setup_vscode(): return { "command": _get_vscode_cmd, "timeout": 300, From e805c8119e9df71b7c8c54c01a6af90651675f5c Mon Sep 17 00:00:00 2001 From: Jan-Benedikt Jagusch Date: Tue, 15 Aug 2023 16:38:54 +0200 Subject: [PATCH 2/7] Allow to specify code executable. --- jupyter_vscode_proxy/__init__.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/jupyter_vscode_proxy/__init__.py b/jupyter_vscode_proxy/__init__.py index f9c9e4f..39d9ef2 100644 --- a/jupyter_vscode_proxy/__init__.py +++ b/jupyter_vscode_proxy/__init__.py @@ -27,9 +27,17 @@ def _get_vscode_cmd(port): return cmd +_CODE_EXECUTABLE_CMD_MAP = { + "code-server": _get_vscode_cmd, +} + + def setup_vscode(): + executable = os.environ.get("CODE_EXECUTABLE", "code-server") + if executable not in _CODE_EXECUTABLE_CMD_MAP: + raise KeyError(f"'{executable}' is not one of {_CODE_EXECUTABLE_CMD_MAP.keys()}.") return { - "command": _get_vscode_cmd, + "command": _CODE_EXECUTABLE_CMD_MAP[executable], "timeout": 300, "new_browser_tab": True, "launcher_entry": { From 796a39b000617898b2fb2e2bc559ac89cbd8fccc Mon Sep 17 00:00:00 2001 From: Jan-Benedikt Jagusch Date: Tue, 15 Aug 2023 16:40:30 +0200 Subject: [PATCH 3/7] Add openvscode-server as executable option. --- jupyter_vscode_proxy/__init__.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/jupyter_vscode_proxy/__init__.py b/jupyter_vscode_proxy/__init__.py index 39d9ef2..b67ea1c 100644 --- a/jupyter_vscode_proxy/__init__.py +++ b/jupyter_vscode_proxy/__init__.py @@ -27,8 +27,31 @@ def _get_vscode_cmd(port): return cmd +def _get_openvscode_cmd(port): + executable = "openvscode-server" + if not shutil.which(executable): + raise FileNotFoundError("Can not find openvscode-server in PATH") + + cmd = [ + executable, + "--without-connection-token", + "--telemetry-level off", + "--port=" + str(port), + ] + + if (extensions_dir := os.getenv("CODE_EXTENSIONSDIR", None)): + cmd += ["--extensions-dir", extensions_dir] + + # Start openvscode in CODE_WORKINGDIR env variable if set + # If not, start in 'current directory'. + working_dir = os.getenv("CODE_WORKINGDIR", ".") + cmd.append(working_dir) + return cmd + + _CODE_EXECUTABLE_CMD_MAP = { "code-server": _get_vscode_cmd, + "openvscode-server": _get_openvscode_cmd, } From ce4383633424ccebaaa6ed114c5e81ab4a6acb6c Mon Sep 17 00:00:00 2001 From: Jan-Benedikt Jagusch Date: Tue, 15 Aug 2023 16:49:03 +0200 Subject: [PATCH 4/7] Refactor to remove redundancy. --- jupyter_vscode_proxy/__init__.py | 78 ++++++++++++++++---------------- 1 file changed, 38 insertions(+), 40 deletions(-) diff --git a/jupyter_vscode_proxy/__init__.py b/jupyter_vscode_proxy/__init__.py index b67ea1c..024bda2 100644 --- a/jupyter_vscode_proxy/__init__.py +++ b/jupyter_vscode_proxy/__init__.py @@ -1,53 +1,23 @@ import os import shutil -def _get_vscode_cmd(port): - executable = "code-server" - if not shutil.which(executable): - raise FileNotFoundError("Can not find code-server in PATH") - - # Start vscode in CODE_WORKINGDIR env variable if set - # If not, start in 'current directory', which is $REPO_DIR in mybinder - # but /home/jovyan (or equivalent) in JupyterHubs - working_dir = os.getenv("CODE_WORKINGDIR", ".") - - extensions_dir = os.getenv("CODE_EXTENSIONSDIR", None) - cmd = [ - executable, + +def _get_vscode_cmd(): + return [ + "code-server", "--auth", "none", "--disable-telemetry", - "--port=" + str(port), ] - if extensions_dir: - cmd += ["--extensions-dir", extensions_dir] - - cmd.append(working_dir) - return cmd - -def _get_openvscode_cmd(port): - executable = "openvscode-server" - if not shutil.which(executable): - raise FileNotFoundError("Can not find openvscode-server in PATH") - - cmd = [ - executable, +def _get_openvscode_cmd(): + return [ + "openvscode-server", "--without-connection-token", "--telemetry-level off", - "--port=" + str(port), ] - if (extensions_dir := os.getenv("CODE_EXTENSIONSDIR", None)): - cmd += ["--extensions-dir", extensions_dir] - - # Start openvscode in CODE_WORKINGDIR env variable if set - # If not, start in 'current directory'. - working_dir = os.getenv("CODE_WORKINGDIR", ".") - cmd.append(working_dir) - return cmd - _CODE_EXECUTABLE_CMD_MAP = { "code-server": _get_vscode_cmd, @@ -55,12 +25,40 @@ def _get_openvscode_cmd(port): } -def setup_vscode(): - executable = os.environ.get("CODE_EXECUTABLE", "code-server") +def _get_cmd_factory(executable): if executable not in _CODE_EXECUTABLE_CMD_MAP: raise KeyError(f"'{executable}' is not one of {_CODE_EXECUTABLE_CMD_MAP.keys()}.") + + get_cmd = _CODE_EXECUTABLE_CMD_MAP[executable] + + def _get_cmd(port): + if not shutil.which(executable): + raise FileNotFoundError(f"Can not find {executable} in PATH") + + # Start vscode in CODE_WORKINGDIR env variable if set + # If not, start in 'current directory', which is $REPO_DIR in mybinder + # but /home/jovyan (or equivalent) in JupyterHubs + working_dir = os.getenv("CODE_WORKINGDIR", ".") + + extensions_dir = os.getenv("CODE_EXTENSIONSDIR", None) + + cmd = get_cmd() + + cmd.append("--port=" + str(port)) + + if extensions_dir: + cmd += ["--extensions-dir", extensions_dir] + + cmd.append(working_dir) + return cmd + + return _get_cmd + + +def setup_vscode(): + executable = os.environ.get("CODE_EXECUTABLE", "code-server") return { - "command": _CODE_EXECUTABLE_CMD_MAP[executable], + "command": _get_cmd_factory(executable), "timeout": 300, "new_browser_tab": True, "launcher_entry": { From 296f9d811929f5541601d0591e88cc3552ff640c Mon Sep 17 00:00:00 2001 From: Jan-Benedikt Jagusch Date: Tue, 15 Aug 2023 16:51:36 +0200 Subject: [PATCH 5/7] Add type hints. --- jupyter_vscode_proxy/__init__.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/jupyter_vscode_proxy/__init__.py b/jupyter_vscode_proxy/__init__.py index 024bda2..b2073a2 100644 --- a/jupyter_vscode_proxy/__init__.py +++ b/jupyter_vscode_proxy/__init__.py @@ -1,8 +1,10 @@ +from typing import Callable, List, Dict, Any + import os import shutil -def _get_vscode_cmd(): +def _get_vscode_cmd() -> List[str]: return [ "code-server", "--auth", @@ -11,7 +13,7 @@ def _get_vscode_cmd(): ] -def _get_openvscode_cmd(): +def _get_openvscode_cmd() -> List[str]: return [ "openvscode-server", "--without-connection-token", @@ -19,19 +21,19 @@ def _get_openvscode_cmd(): ] -_CODE_EXECUTABLE_CMD_MAP = { +_CODE_EXECUTABLE_CMD_MAP: Dict[str, Callable] = { "code-server": _get_vscode_cmd, "openvscode-server": _get_openvscode_cmd, } -def _get_cmd_factory(executable): +def _get_cmd_factory(executable: str) -> Callable: if executable not in _CODE_EXECUTABLE_CMD_MAP: raise KeyError(f"'{executable}' is not one of {_CODE_EXECUTABLE_CMD_MAP.keys()}.") get_cmd = _CODE_EXECUTABLE_CMD_MAP[executable] - def _get_cmd(port): + def _get_cmd(port: int) -> List[str]: if not shutil.which(executable): raise FileNotFoundError(f"Can not find {executable} in PATH") @@ -55,7 +57,7 @@ def _get_cmd(port): return _get_cmd -def setup_vscode(): +def setup_vscode() -> Dict[str, Any]: executable = os.environ.get("CODE_EXECUTABLE", "code-server") return { "command": _get_cmd_factory(executable), From bee1fe524345b66ec23257001641458ca6c67e20 Mon Sep 17 00:00:00 2001 From: Jan-Benedikt Jagusch Date: Tue, 15 Aug 2023 16:57:13 +0200 Subject: [PATCH 6/7] Rename to inner_cmd. --- jupyter_vscode_proxy/__init__.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/jupyter_vscode_proxy/__init__.py b/jupyter_vscode_proxy/__init__.py index b2073a2..52d06f3 100644 --- a/jupyter_vscode_proxy/__init__.py +++ b/jupyter_vscode_proxy/__init__.py @@ -4,7 +4,7 @@ import shutil -def _get_vscode_cmd() -> List[str]: +def _get_inner_vscode_cmd() -> List[str]: return [ "code-server", "--auth", @@ -13,7 +13,7 @@ def _get_vscode_cmd() -> List[str]: ] -def _get_openvscode_cmd() -> List[str]: +def _get_inner_openvscode_cmd() -> List[str]: return [ "openvscode-server", "--without-connection-token", @@ -21,17 +21,17 @@ def _get_openvscode_cmd() -> List[str]: ] -_CODE_EXECUTABLE_CMD_MAP: Dict[str, Callable] = { - "code-server": _get_vscode_cmd, - "openvscode-server": _get_openvscode_cmd, +_CODE_EXECUTABLE_INNER_CMD_MAP: Dict[str, Callable] = { + "code-server": _get_inner_vscode_cmd, + "openvscode-server": _get_inner_openvscode_cmd, } def _get_cmd_factory(executable: str) -> Callable: - if executable not in _CODE_EXECUTABLE_CMD_MAP: - raise KeyError(f"'{executable}' is not one of {_CODE_EXECUTABLE_CMD_MAP.keys()}.") + if executable not in _CODE_EXECUTABLE_INNER_CMD_MAP: + raise KeyError(f"'{executable}' is not one of {_CODE_EXECUTABLE_INNER_CMD_MAP.keys()}.") - get_cmd = _CODE_EXECUTABLE_CMD_MAP[executable] + get_inner_cmd = _CODE_EXECUTABLE_INNER_CMD_MAP[executable] def _get_cmd(port: int) -> List[str]: if not shutil.which(executable): @@ -44,7 +44,7 @@ def _get_cmd(port: int) -> List[str]: extensions_dir = os.getenv("CODE_EXTENSIONSDIR", None) - cmd = get_cmd() + cmd = get_inner_cmd() cmd.append("--port=" + str(port)) From 593cb10ce40bf035e2b6696c15cef8401ef2ede6 Mon Sep 17 00:00:00 2001 From: Jan-Benedikt Jagusch Date: Fri, 18 Aug 2023 10:29:44 +0200 Subject: [PATCH 7/7] Update jupyter_vscode_proxy/__init__.py Co-authored-by: Uwe L. Korn --- jupyter_vscode_proxy/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/jupyter_vscode_proxy/__init__.py b/jupyter_vscode_proxy/__init__.py index 52d06f3..8c2ec97 100644 --- a/jupyter_vscode_proxy/__init__.py +++ b/jupyter_vscode_proxy/__init__.py @@ -17,7 +17,8 @@ def _get_inner_openvscode_cmd() -> List[str]: return [ "openvscode-server", "--without-connection-token", - "--telemetry-level off", + "--telemetry-level", + "off", ]