-
Notifications
You must be signed in to change notification settings - Fork 86
Added non-git source puller functionality #194
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
ea87f2b
10385bb
ab80daf
71ca2f4
ae66e53
8934f5f
ac2072c
a84096d
86fd7bf
c686651
f8e04f1
958b0b1
2048e8d
398a03f
9a8fcab
78e31c3
a131b93
55da5e1
0ca6cf9
9e808e5
8d63ee4
deecc7b
a9e08c4
09c9249
0085fab
88ec806
8592d1f
21d8f0f
ab5dd10
e8ae5ca
56ad1ee
af567ca
782a35b
d034d37
9729464
602ef01
3ebdc7e
e22d076
3b14405
613f863
5f39c68
f618560
367f3c7
8893970
7590c38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,16 @@ | |
| from notebook.utils import url_path_join | ||
| from tornado.web import StaticFileHandler | ||
| import os | ||
| import nest_asyncio | ||
|
|
||
| REPO_PARENT_DIR = None | ||
| TEMP_DOWNLOAD_REPO_DIR = "/tmp/temp_download_repo" | ||
| CACHED_ORIGIN_NON_GIT_REPO = ".nbgitpuller/targets/" | ||
|
|
||
| # this allows us to nest usage of the event_loop from asyncio | ||
| # being used by tornado in jupyter distro | ||
| # Ref: https://medium.com/@vyshali.enukonda/how-to-get-around-runtimeerror-this-event-loop-is-already-running-3f26f67e762e | ||
| nest_asyncio.apply() | ||
|
||
|
|
||
|
|
||
| def _jupyter_server_extension_paths(): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| from tornado import gen, web, locks | ||
| import traceback | ||
| import urllib.parse | ||
|
|
||
| from notebook.base.handlers import IPythonHandler | ||
| import threading | ||
| import json | ||
|
|
@@ -11,6 +10,9 @@ | |
|
|
||
| from .pull import GitPuller | ||
| from .version import __version__ | ||
| from . import hookspecs | ||
| import pluggy | ||
| import nbgitpuller | ||
|
|
||
|
|
||
| class SyncHandler(IPythonHandler): | ||
|
|
@@ -38,6 +40,38 @@ def emit(self, data): | |
| self.write('data: {}\n\n'.format(serialized_data)) | ||
| yield self.flush() | ||
|
|
||
| def setup_plugins(self, provider): | ||
sean-morris marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pm = pluggy.PluginManager("nbgitpuller") | ||
| pm.add_hookspecs(hookspecs) | ||
| pm.load_setuptools_entrypoints("nbgitpuller", name=provider) | ||
| return pm | ||
|
|
||
| @gen.coroutine | ||
| def progress_loop(self, queue): | ||
sean-morris marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
sean-morris marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| while True: | ||
| try: | ||
| progress = queue.get_nowait() | ||
| except Empty: | ||
| yield gen.sleep(0.1) | ||
sean-morris marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| continue | ||
sean-morris marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if progress is None: | ||
| yield gen.sleep(5) | ||
sean-morris marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
consideRatio marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return | ||
| if isinstance(progress, Exception): | ||
| self.emit({ | ||
| 'phase': 'error', | ||
| 'message': str(progress), | ||
| 'output': '\n'.join([ | ||
| line.strip() | ||
| for line in traceback.format_exception( | ||
| type(progress), progress, progress.__traceback__ | ||
| ) | ||
| ]) | ||
| }) | ||
| return | ||
|
|
||
| self.emit({'output': progress, 'phase': 'syncing'}) | ||
|
|
||
| @web.authenticated | ||
| @gen.coroutine | ||
| def get(self): | ||
|
|
@@ -53,6 +87,7 @@ def get(self): | |
| try: | ||
| repo = self.get_argument('repo') | ||
| branch = self.get_argument('branch', None) | ||
| provider = self.get_argument('provider', None) | ||
| depth = self.get_argument('depth', None) | ||
| if depth: | ||
| depth = int(depth) | ||
|
|
@@ -65,16 +100,31 @@ def get(self): | |
| # so that all repos are always in scope after cloning. Sometimes | ||
| # server_root_dir will include things like `~` and so the path | ||
| # must be expanded. | ||
| repo_parent_dir = os.path.join(os.path.expanduser(self.settings['server_root_dir']), | ||
| os.getenv('NBGITPULLER_PARENTPATH', '')) | ||
| repo_dir = os.path.join(repo_parent_dir, self.get_argument('targetpath', repo.split('/')[-1])) | ||
| repo_parent_dir = os.path.join(os.path.expanduser(self.settings['server_root_dir']), os.getenv('NBGITPULLER_PARENTPATH', '')) | ||
| nbgitpuller.REPO_PARENT_DIR = repo_parent_dir | ||
|
|
||
| repo_dir = os.path.join( | ||
| repo_parent_dir, | ||
| self.get_argument('targetpath', repo.split('/')[-1])) | ||
sean-morris marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # We gonna send out event streams! | ||
| self.set_header('content-type', 'text/event-stream') | ||
| self.set_header('cache-control', 'no-cache') | ||
|
|
||
| gp = GitPuller(repo, repo_dir, branch=branch, depth=depth, parent=self.settings['nbapp']) | ||
| # if provider is specified then we are dealing with compressed | ||
| # archive and not a git repo | ||
|
||
| if provider is not None: | ||
| pm = self.setup_plugins(provider) | ||
| req_args = {k: v[0].decode() for k, v in self.request.arguments.items()} | ||
| download_q = Queue() | ||
| req_args["progress_func"] = lambda: self.progress_loop(download_q) | ||
| req_args["download_q"] = download_q | ||
| hf_args = {"query_line_args": req_args} | ||
| results = pm.hook.handle_files(**hf_args) | ||
sean-morris marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| repo_dir = repo_parent_dir + results["unzip_dir"] | ||
| repo = "file://" + results["origin_repo_path"] | ||
|
|
||
| gp = GitPuller(repo, repo_dir, branch=branch, depth=depth, parent=self.settings['nbapp']) | ||
| q = Queue() | ||
|
|
||
| def pull(): | ||
|
|
@@ -87,33 +137,11 @@ def pull(): | |
| q.put_nowait(e) | ||
| raise e | ||
| self.gp_thread = threading.Thread(target=pull) | ||
|
|
||
| self.gp_thread.start() | ||
|
|
||
| while True: | ||
| try: | ||
| progress = q.get_nowait() | ||
| except Empty: | ||
| yield gen.sleep(0.5) | ||
| continue | ||
| if progress is None: | ||
| break | ||
| if isinstance(progress, Exception): | ||
| self.emit({ | ||
| 'phase': 'error', | ||
| 'message': str(progress), | ||
| 'output': '\n'.join([ | ||
| line.strip() | ||
| for line in traceback.format_exception( | ||
| type(progress), progress, progress.__traceback__ | ||
| ) | ||
| ]) | ||
| }) | ||
| return | ||
|
|
||
| self.emit({'output': progress, 'phase': 'syncing'}) | ||
|
|
||
| self.progress_loop(q) | ||
sean-morris marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| yield gen.sleep(3) | ||
sean-morris marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| self.emit({'phase': 'finished'}) | ||
|
|
||
| except Exception as e: | ||
| self.emit({ | ||
| 'phase': 'error', | ||
|
|
@@ -147,18 +175,18 @@ def initialize(self): | |
| @gen.coroutine | ||
| def get(self): | ||
| app_env = os.getenv('NBGITPULLER_APP', default='notebook') | ||
|
|
||
| repo = self.get_argument('repo') | ||
consideRatio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| branch = self.get_argument('branch', None) | ||
| depth = self.get_argument('depth', None) | ||
| provider = self.get_argument('provider', None) | ||
| urlPath = self.get_argument('urlpath', None) or \ | ||
| self.get_argument('urlPath', None) | ||
| self.get_argument('urlPath', None) | ||
consideRatio marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| subPath = self.get_argument('subpath', None) or \ | ||
| self.get_argument('subPath', '.') | ||
| self.get_argument('subPath', '.') | ||
consideRatio marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| app = self.get_argument('app', app_env) | ||
| parent_reldir = os.getenv('NBGITPULLER_PARENTPATH', '') | ||
| targetpath = self.get_argument('targetpath', None) or \ | ||
| self.get_argument('targetPath', repo.split('/')[-1]) | ||
| self.get_argument('targetPath', repo.split('/')[-1]) | ||
consideRatio marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if urlPath: | ||
| path = urlPath | ||
|
|
@@ -171,10 +199,19 @@ def get(self): | |
| else: | ||
| path = 'tree/' + path | ||
|
|
||
| if provider is not None: | ||
| path = "tree/" | ||
|
|
||
| self.write( | ||
| self.render_template( | ||
| 'status.html', | ||
| repo=repo, branch=branch, path=path, depth=depth, targetpath=targetpath, version=__version__ | ||
| repo=repo, | ||
| branch=branch, | ||
| path=path, | ||
| depth=depth, | ||
| provider=provider, | ||
| targetpath=targetpath, | ||
| version=__version__ | ||
| )) | ||
| self.flush() | ||
|
|
||
|
|
@@ -209,3 +246,10 @@ def get(self): | |
| ) | ||
|
|
||
| self.redirect(new_url) | ||
|
|
||
|
|
||
| class ThreadWithResult(threading.Thread): | ||
| def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None): | ||
| def function(): | ||
| self.result = target(*args, **kwargs) | ||
| super().__init__(group=group, target=function, name=name, daemon=daemon) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import pluggy | ||
sean-morris marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| hookspec = pluggy.HookspecMarker("nbgitpuller") | ||
| hookimpl = pluggy.HookimplMarker("nbgitpuller") | ||
sean-morris marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| @hookspec(firstresult=True) | ||
| def handle_files(query_line_args): | ||
| """ | ||
| :param json query_line_args: this includes any argument you put on the url | ||
| :return two parameter json unzip_dir and origin_repo_path | ||
| :rtype json object | ||
| The developer uses this function to download, un-compress and save the | ||
| source files to the TEMP_DOWNLOAD_REPO_DIR folder. | ||
| The parameter, query_line_args, is any argument you put on the URL | ||
| Once the files are saved to the directly, git puller can handle all the | ||
sean-morris marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| standard functions needed to make sure source files are updated or created | ||
| as needed. | ||
| """ | ||
Uh oh!
There was an error while loading. Please reload this page.