Skip to content

Commit

Permalink
Merge pull request #39 from janjagusch/add-support-for-openvscode-server
Browse files Browse the repository at this point in the history
Add support for openvscode server
  • Loading branch information
xhochy authored Aug 18, 2023
2 parents bac01ce + 593cb10 commit ae67d42
Showing 1 changed file with 44 additions and 12 deletions.
56 changes: 44 additions & 12 deletions jupyter_vscode_proxy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,67 @@
from typing import Callable, List, Dict, Any

import os
import shutil


def setup_vscode():
def _get_vscode_cmd(port):
executable = "code-server"
def _get_inner_vscode_cmd() -> List[str]:
return [
"code-server",
"--auth",
"none",
"--disable-telemetry",
]


def _get_inner_openvscode_cmd() -> List[str]:
return [
"openvscode-server",
"--without-connection-token",
"--telemetry-level",
"off",
]


_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_INNER_CMD_MAP:
raise KeyError(f"'{executable}' is not one of {_CODE_EXECUTABLE_INNER_CMD_MAP.keys()}.")

get_inner_cmd = _CODE_EXECUTABLE_INNER_CMD_MAP[executable]

def _get_cmd(port: int) -> List[str]:
if not shutil.which(executable):
raise FileNotFoundError("Can not find code-server in PATH")
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 = [
executable,
"--auth",
"none",
"--disable-telemetry",
"--port=" + str(port),
]

cmd = get_inner_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() -> Dict[str, Any]:
executable = os.environ.get("CODE_EXECUTABLE", "code-server")
return {
"command": _get_vscode_cmd,
"command": _get_cmd_factory(executable),
"timeout": 300,
"new_browser_tab": True,
"launcher_entry": {
Expand Down

0 comments on commit ae67d42

Please sign in to comment.