From 29739c792ec3862fcf2af921e7e1e89c622f1858 Mon Sep 17 00:00:00 2001 From: Benoit Seignovert Date: Tue, 6 Feb 2024 16:21:28 +0100 Subject: [PATCH] Add fixed singleuser command script --- pyproject.toml | 3 +++ src/glicid_spawner/singleuser.py | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/glicid_spawner/singleuser.py diff --git a/pyproject.toml b/pyproject.toml index 558f0ed..2e89bb7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,6 +8,9 @@ readme = "README.md" repository = "https://gitlab.univ-nantes.fr/glicid/jupyter/spawner" packages = [{ include = "glicid_spawner", from = "src"}] +[tool.poetry.scripts] +glicid-spawner-singleuser = 'glicid_spawner.singleuser:main' + [tool.poetry.plugins."jupyterhub.spawners"] glicid-spawner = 'glicid_spawner:GlicidSpawner' diff --git a/src/glicid_spawner/singleuser.py b/src/glicid_spawner/singleuser.py new file mode 100644 index 0000000..8cb7750 --- /dev/null +++ b/src/glicid_spawner/singleuser.py @@ -0,0 +1,38 @@ +"""Patched JupyterHub singleuser command script. + +Fix based on batchspawner#250: + https://github.com/jupyterhub/batchspawner/pull/250 + +The Hub API should use an async call. + +""" + +import asyncio +import json +import sys +from runpy import run_path +from shutil import which + +from jupyterhub.services.auth import HubAuth +from jupyterhub.utils import random_port, url_path_join + + +def main(argv=None): + port = random_port() + hub_auth = HubAuth() + + asyncio.run( + hub_auth._api_request( + method='POST', + url=url_path_join(hub_auth.api_url, 'batchspawner'), + body=json.dumps({'port': port}), + ) + ) + + cmd_path = which(sys.argv[1]) + sys.argv = sys.argv[1:] + [f'--port={port}'] + run_path(cmd_path, run_name='__main__') + + +if __name__ == '__main__': + main()