From 15333f1c56fc0a2f7cfb715eee47a7844d6430c3 Mon Sep 17 00:00:00 2001 From: Simon Li Date: Mon, 3 Jan 2022 12:59:08 +0000 Subject: [PATCH 1/5] Depend on jupyter-server only --- nbgitpuller/__init__.py | 2 +- nbgitpuller/handlers.py | 10 +++++----- setup.py | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/nbgitpuller/__init__.py b/nbgitpuller/__init__.py index b5941286..03900ff6 100644 --- a/nbgitpuller/__init__.py +++ b/nbgitpuller/__init__.py @@ -1,7 +1,7 @@ from .version import __version__ # noqa from .handlers import SyncHandler, UIHandler, LegacyInteractRedirectHandler, LegacyGitSyncRedirectHandler from .pull import GitPuller # noqa -from notebook.utils import url_path_join +from jupyter_server.utils import url_path_join from tornado.web import StaticFileHandler import os diff --git a/nbgitpuller/handlers.py b/nbgitpuller/handlers.py index f83ad7d5..611ce757 100644 --- a/nbgitpuller/handlers.py +++ b/nbgitpuller/handlers.py @@ -2,7 +2,7 @@ import traceback import urllib.parse -from notebook.base.handlers import IPythonHandler +from jupyter_server.base.handlers import JupyterHandler import threading import json import os @@ -13,7 +13,7 @@ from .version import __version__ -class SyncHandler(IPythonHandler): +class SyncHandler(JupyterHandler): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -129,7 +129,7 @@ def pull(): self.git_lock.release() -class UIHandler(IPythonHandler): +class UIHandler(JupyterHandler): def initialize(self): super().initialize() # FIXME: Is this really the best way to use jinja2 here? @@ -179,7 +179,7 @@ def get(self): self.flush() -class LegacyGitSyncRedirectHandler(IPythonHandler): +class LegacyGitSyncRedirectHandler(JupyterHandler): @web.authenticated @gen.coroutine def get(self): @@ -190,7 +190,7 @@ def get(self): self.redirect(new_url) -class LegacyInteractRedirectHandler(IPythonHandler): +class LegacyInteractRedirectHandler(JupyterHandler): @web.authenticated @gen.coroutine def get(self): diff --git a/setup.py b/setup.py index 3efef420..c7bad826 100644 --- a/setup.py +++ b/setup.py @@ -38,7 +38,7 @@ packages=find_packages(), include_package_data=True, platforms='any', - install_requires=['notebook>=5.5.0', 'jupyter_server>=1.10.1', 'tornado'], + install_requires=['jupyter_server>=1.10.1', 'tornado'], data_files=[ ('etc/jupyter/jupyter_server_config.d', ['nbgitpuller/etc/jupyter_server_config.d/nbgitpuller.json']), ('etc/jupyter/jupyter_notebook_config.d', ['nbgitpuller/etc/jupyter_notebook_config.d/nbgitpuller.json']) From 626eb5414850a7587fcfa06ef550019966695b96 Mon Sep 17 00:00:00 2001 From: Simon Li Date: Mon, 3 Jan 2022 13:11:12 +0000 Subject: [PATCH 2/5] Change Description to Jupyter instead of Notebook --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index c7bad826..32fb0347 100644 --- a/setup.py +++ b/setup.py @@ -32,7 +32,7 @@ author='Peter Veerman, YuviPanda', author_email='peterkangveerman@gmail.com', cmdclass=cmdclass, - description='Notebook Extension to do one-way synchronization of git repositories', + description='Jupyter Extension to do one-way synchronization of git repositories', long_description=open('README.md').read(), long_description_content_type='text/markdown', packages=find_packages(), From 385c300f91739465526fb68ea849055603604b2e Mon Sep 17 00:00:00 2001 From: Min RK Date: Tue, 23 May 2023 10:57:03 +0200 Subject: [PATCH 3/5] tests still require legacy notebook --- dev-requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/dev-requirements.txt b/dev-requirements.txt index bdf678f1..0a8b925e 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,5 +1,6 @@ jupyter-packaging>=0.10 nbclassic +notebook>=5.5,<7 packaging pytest pytest-cov From f230919bbfe7951647de1d5823553ddfc5575e4f Mon Sep 17 00:00:00 2001 From: Min RK Date: Tue, 23 May 2023 11:02:19 +0200 Subject: [PATCH 4/5] verify rejected auth as well --- tests/test_api.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/test_api.py b/tests/test_api.py index 55172ca9..d5cb1c31 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -118,6 +118,23 @@ def test_clone_default(jupyterdir, jupyter_server): assert os.path.isdir(os.path.join(target_path, '.git')) +def test_clone_auth(jupyterdir, jupyter_server): + """ + Tests use of 'repo' and 'branch' parameters. + """ + with Remote() as remote, Pusher(remote) as pusher: + pusher.push_file('README.md', 'Testing some content') + print(f'path: {remote.path}') + params = { + 'repo': remote.path, + 'branch': 'master', + 'token': 'wrong', + } + r = request_api(params) + # no token, redirect to login + assert r.code == 302 + + def test_clone_targetpath(jupyterdir, jupyter_server): """ Tests use of 'targetpath' parameter. From bb2e7bd5ea518dba53422dff505660823a6ba253 Mon Sep 17 00:00:00 2001 From: Min RK Date: Tue, 23 May 2023 14:10:39 +0200 Subject: [PATCH 5/5] select base JupyterHandler according to application implementation --- nbgitpuller/__init__.py | 14 +++++++++++++- nbgitpuller/_compat.py | 40 ++++++++++++++++++++++++++++++++++++++++ nbgitpuller/handlers.py | 4 +++- 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 nbgitpuller/_compat.py diff --git a/nbgitpuller/__init__.py b/nbgitpuller/__init__.py index 16888814..47973f96 100644 --- a/nbgitpuller/__init__.py +++ b/nbgitpuller/__init__.py @@ -1,5 +1,4 @@ from .version import __version__ # noqa -from .handlers import SyncHandler, UIHandler, LegacyInteractRedirectHandler, LegacyGitSyncRedirectHandler from .pull import GitPuller # noqa from jupyter_server.utils import url_path_join from tornado.web import StaticFileHandler @@ -33,6 +32,19 @@ def _load_jupyter_server_extension(app): - notebook: https://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/Distributing%20Jupyter%20Extensions%20as%20Python%20Packages.html#Example---Server-extension - jupyter_server: https://jupyter-server.readthedocs.io/en/latest/developers/extensions.html """ + # identify base handler by app class + # must do this before importing from .handlers + from ._compat import get_base_handler + + get_base_handler(app) + + from .handlers import ( + SyncHandler, + UIHandler, + LegacyInteractRedirectHandler, + LegacyGitSyncRedirectHandler, + ) + web_app = app.web_app base_url = url_path_join(web_app.settings['base_url'], 'git-pull') handlers = [ diff --git a/nbgitpuller/_compat.py b/nbgitpuller/_compat.py new file mode 100644 index 00000000..036967c9 --- /dev/null +++ b/nbgitpuller/_compat.py @@ -0,0 +1,40 @@ +"""Import base Handler classes from Jupyter Server or Notebook + +Must be called before importing .handlers to ensure the correct base classes +""" +import warnings + +_JupyterHandler = None + + +def get_base_handler(app=None): + """Get the base JupyterHandler class to use + + Inferred from app class (either jupyter_server or notebook app) + """ + global _JupyterHandler + if _JupyterHandler is not None: + return _JupyterHandler + if app is None: + warnings.warn( + "Guessing base JupyterHandler class. Specify an app to ensure the right JupyterHandler is used.", + stacklevel=2, + ) + from jupyter_server.base.handlers import JupyterHandler + return JupyterHandler + + top_modules = {cls.__module__.split(".", 1)[0] for cls in app.__class__.mro()} + if "jupyter_server" in top_modules: + from jupyter_server.base.handlers import JupyterHandler + + _JupyterHandler = JupyterHandler + return _JupyterHandler + if "notebook" in top_modules: + from notebook.base.handlers import IPythonHandler + + _JupyterHandler = IPythonHandler + return _JupyterHandler + + warnings.warn(f"Failed to detect base JupyterHandler class for {app}.", stacklevel=2) + from jupyter_server.base.handlers import JupyterHandler + return JupyterHandler diff --git a/nbgitpuller/handlers.py b/nbgitpuller/handlers.py index 1fa36ba6..2e572768 100644 --- a/nbgitpuller/handlers.py +++ b/nbgitpuller/handlers.py @@ -2,7 +2,6 @@ import traceback import urllib.parse -from jupyter_server.base.handlers import JupyterHandler import threading import json import os @@ -11,6 +10,9 @@ from .pull import GitPuller from .version import __version__ +from ._compat import get_base_handler + +JupyterHandler = get_base_handler() jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader(