Skip to content

Commit

Permalink
Merge pull request jupyterhub#41 from consideRatio/jupyterlab-pr
Browse files Browse the repository at this point in the history
Ability to launch into JupyterLab
  • Loading branch information
yuvipanda authored May 1, 2018
2 parents 28fe9b1 + b9f4398 commit bdd9e65
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
sudo: false
language: python
python:
- "3.5"
- "3.6"
install: pip install tox-travis
script: tox
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,21 @@ You should *not* use this when:
You can construct a working nbgitpuller URL like this:

```
myjupyterhub.org/hub/user-redirect/git-pull?repo=<your-repo-url>&branch=<your-branch-name>&subPath=<subPath>
myjupyterhub.org/hub/user-redirect/git-pull?repo=<your-repo-url>&branch=<your-branch-name>&subPath=<subPath>&app=<notebook | lab>
```

- **repo** is the URL of the git repository you want to clone. This parameter is required.
- **branch** is the branch name to use when cloning from the repository.
This parameter is optional and defaults to `master`.
- **subPath** is the path of the directory / notebook inside the repo to launch after cloning.
This parameter is optional, and defaults to opening the base directory of the linked Git repository.
- **app** is the branch name to use when cloning from the repository.
This parameter is optional and defaults to either the environment variable
`NBGITPULLER_APP`'s value or `notebook` if it is undefined. The allowed values
are `lab` and `notebook`, the value will determine in what application view
you end up in.
- **urlPath** will if specified override `app` and `subPath` and redirect
blindly to the specified path.

# Local development

Expand Down
4 changes: 2 additions & 2 deletions nbgitpuller/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from .version import __version__
from .handlers import SyncHandler, UIHandler, LegacyInteractRedirectHandler, LegacyGitSyncRedirectHandler
from .pull import GitPuller
from notebook.utils import url_path_join
from tornado.web import StaticFileHandler
import os
from .pull import GitPuller


def _jupyter_server_extension_paths():
return [{
Expand Down
26 changes: 21 additions & 5 deletions nbgitpuller/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import jinja2

from .pull import GitPuller
from .version import __version__

class SyncHandler(IPythonHandler):
def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -129,17 +130,32 @@ def initialize(self):
@web.authenticated
@gen.coroutine
def get(self):
app_env = os.getenv('NBGITPULLER_APP', default='notebook')

repo = self.get_argument('repo')
subPath = self.get_argument('subPath', '.')
branch = self.get_argument('branch', 'master')

repo_dir = repo.split('/')[-1]
path = os.path.join(repo_dir, subPath)
urlPath = self.get_argument('urlpath', None) or \
self.get_argument('urlPath', None)
subPath = self.get_argument('subpath', None) or \
self.get_argument('subPath', '.')
app = self.get_argument('app', app_env)

if urlPath:
path = urlPath
else:
repo_dir = repo.split('/')[-1]
path = os.path.join(repo_dir, subPath)
if app.lower() == 'lab':
path = 'lab/tree/' + path
elif path.lower().endswith('.ipynb'):
path = 'notebooks/' + path
else:
path = 'tree/' + path

self.write(
self.render_template(
'status.html',
repo=repo, path=path, branch=branch
repo=repo, branch=branch, path=path, version=__version__
))
self.flush()

Expand Down
12 changes: 3 additions & 9 deletions nbgitpuller/static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ require([
this.baseUrl = baseUrl;
this.repo = repo;
this.branch = branch;
this.path = path;

if (path.endsWith('.ipynb')) {
this.redirectUrl = baseUrl + 'notebooks/' + path;
} else {
this.redirectUrl = baseUrl + 'tree/' + path;
}
this.redirectUrl = baseUrl + path;

this.callbacks = {};
}
Expand All @@ -42,10 +36,10 @@ require([


GitSync.prototype.start = function() {
// Start git pulling handled by SyncHandler, declared in handlers.py
var syncUrl = this.baseUrl + 'git-pull/api?' + $.param({
repo: this.repo,
branch: this.branch,
path: this.path
branch: this.branch
});

this.eventSource = new EventSource(syncUrl);
Expand Down
8 changes: 4 additions & 4 deletions nbgitpuller/templates/status.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

{% block params %}
{{super()}}
data-base-url="{{base_url | urlencode }}"
data-repo="{{repo | urlencode }}"
data-path="{{path | urlencode }}"
data-base-url="{{ base_url | urlencode }}"
data-repo="{{ repo | urlencode }}"
data-path="{{ path | urlencode }}"
data-branch="{{ branch | urlencode }}"
{% endblock %}

Expand Down Expand Up @@ -32,7 +32,7 @@

{% block script %}
{{super()}}
<script src="{{ base_url }}git-pull/static/index.js">
<script src="{{ base_url }}git-pull/static/index.js?v={{ version }}">
</script>
{% endblock %}

Expand Down
2 changes: 2 additions & 0 deletions nbgitpuller/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
""""The nbgitpuller PyPI package SemVer version."""
__version__ = '0.4.0'
12 changes: 10 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
from setuptools import find_packages, setup
from distutils.util import convert_path

# Imports __version__, reference: https://stackoverflow.com/a/24517154/2220152
ns = {}
ver_path = convert_path('nbgitpuller/version.py')
with open(ver_path) as ver_file:
exec(ver_file.read(), ns)
__version__ = ns['__version__']

setup(
name='nbgitpuller',
version='0.3',
version=__version__,
url='https://github.com/data-8/nbgitpuller',
license='BSD',
author='Peter Veerman',
Expand All @@ -11,7 +19,7 @@
packages=find_packages(),
include_package_data=True,
platforms='any',
install_requires=['notebook'],
install_requires=['notebook', 'tornado'],
entry_points={
'console_scripts': [
'gitpuller = nbgitpuller.pull:main',
Expand Down
16 changes: 10 additions & 6 deletions tests/test_gitpuller.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import subprocess as sp
import glob
import time
import random
import string
import pytest
from nbgitpuller import GitPuller


class Remote:
def __init__(self, path='remote'):
self.path = path
Expand All @@ -27,6 +25,7 @@ def git(self, *args):
stderr=sp.STDOUT
).decode().strip()


class Pusher:
def __init__(self, remote, path='pusher'):
self.path = path
Expand Down Expand Up @@ -62,6 +61,7 @@ def push_file(self, path, content):
self.git('commit', '-am', 'Ignore the message')
self.git('push', 'origin', 'master')


class Puller:
def __init__(self, remote, path='puller'):
self.path = path
Expand Down Expand Up @@ -98,6 +98,7 @@ def read_file(self, path):
# 5. Make change in puller to file, make change in pusher to same part of file, run puller
# 6. Make untracked file in puller, add file with same name to pusher, run puller


def test_initialize():
with Remote() as remote, Pusher(remote) as pusher:
pusher.push_file('README.md', '1')
Expand All @@ -108,6 +109,7 @@ def test_initialize():
assert puller.git('name-rev', '--name-only', 'HEAD') == 'master'
assert puller.git('rev-parse', 'HEAD') == pusher.git('rev-parse', 'HEAD')


def test_simple_push_pull():
"""
Test the 'happy path' push/pull interaction
Expand Down Expand Up @@ -143,7 +145,7 @@ def test_simple_push_pull():
assert puller.read_file('another-file') == pusher.read_file('another-file') == '3'

pusher.git('rm', 'another-file')
pusher.git('commit', '-m','Removing File')
pusher.git('commit', '-m', 'Removing File')
pusher.git('push', 'origin', 'master')

for l in puller.gp.pull():
Expand All @@ -152,6 +154,7 @@ def test_simple_push_pull():
assert puller.git('rev-parse', 'HEAD') == pusher.git('rev-parse', 'HEAD')
assert not os.path.exists(os.path.join(puller.path, 'another-file'))


def test_git_lock():
"""
Test the 'happy path', but with stale/unstale git locks
Expand All @@ -168,11 +171,10 @@ def test_git_lock():
for l in puller.gp.pull():
print(puller.path + l)
assert False
except:
except Exception:
# This should raise an exception, since our .git/lock is new
assert True


new_time = time.time() - 700
os.utime(os.path.join(puller.path, '.git', 'index.lock'), (new_time, new_time))

Expand Down Expand Up @@ -217,6 +219,7 @@ def test_merging_simple():

assert puller.read_file('README.md') == '2'


def test_untracked_puller():
"""
Test that untracked files in puller are preserved when pulling
Expand All @@ -236,6 +239,7 @@ def test_untracked_puller():
renamed_file = glob.glob(os.path.join(puller.path, 'another-file_*'))[0]
assert puller.read_file(os.path.basename(renamed_file)) == '3'


def test_reset_file():
"""
Test that deleting files locally & pulling restores pristine copy
Expand Down
8 changes: 6 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist=py35, flake8
envlist=py36, flake8

[testenv]
commands=
Expand All @@ -8,11 +8,15 @@ deps=
six
pytest
pytest-cov
tornado
notebook

[testenv:flake8]
basepython = python3.5
basepython = python3.6
deps =
flake8
six
tornado
notebook
commands =
flake8 gitautosync tests --max-line-length=150

0 comments on commit bdd9e65

Please sign in to comment.