Skip to content

Commit

Permalink
#5971 Remove duplicated functionality between setup.py and beakerx-in…
Browse files Browse the repository at this point in the history
…stall (#6016)

* #5781 move kernel directory outside static

* #5971 duplicate functionality removed

* #5971 duplicate functionality removed

* #5971 fix build

* #5971 changes after PR
  • Loading branch information
rbidas authored and scottdraves committed Oct 30, 2017
1 parent 8853ef1 commit f6aa435
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 173 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ beaker.egg-info/
beakerx/static/
beakerx.egg-info/
beakerx/beakerx/static/
beakerx/beakerx/kernel/
beakerx/dist

kernel/build/
Expand Down
1 change: 1 addition & 0 deletions beakerx/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
recursive-include beakerx/static *
recursive-include beakerx/kernel *

include LICENSE
include NOTICE
Expand Down
19 changes: 10 additions & 9 deletions beakerx/beakerx/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@

def _all_kernels():
kernels = pkg_resources.resource_listdir(
'beakerx', os.path.join('static', 'kernel'))
'beakerx', 'kernel')
return [kernel for kernel in kernels if kernel != 'base']

def _base_classpath_for(kernel):
return pkg_resources.resource_filename(
'beakerx', os.path.join('static', 'kernel', kernel))
'beakerx', os.path.join('kernel', kernel))

def _classpath_for(kernel):
return pkg_resources.resource_filename(
'beakerx', os.path.join('static', 'kernel', kernel, 'lib', '*'))
'beakerx', os.path.join('kernel', kernel, 'lib', '*'))

def _uninstall_nbextension():
subprocess.check_call(["jupyter", "nbextension", "disable", "beakerx", "--py", "--sys-prefix"])
Expand Down Expand Up @@ -85,7 +85,7 @@ def _install_kernels():
kernel_classpath = _classpath_for(kernel)
classpath = json.dumps(os.pathsep.join([base_classpath, kernel_classpath]))
template = pkg_resources.resource_string(
'beakerx', os.path.join('static', 'kernel', kernel, 'kernel.json'))
'beakerx', os.path.join('kernel', kernel, 'kernel.json'))
contents = Template(template.decode()).substitute(PATH=classpath)

with tempfile.TemporaryDirectory() as tmpdir:
Expand All @@ -97,14 +97,14 @@ def _install_kernels():
'--name', kernel, tmpdir
]
subprocess.check_call(install_cmd)

def _uninstall_kernels():
for kernel in _all_kernels():
uninstall_cmd = [
'jupyter', 'kernelspec', 'remove', kernel, '-y', '-f'
]
subprocess.check_call(uninstall_cmd)

def _pretty(it):
return json.dumps(it, indent=2)

Expand Down Expand Up @@ -138,7 +138,8 @@ def _install_kernelspec_manager(prefix, disable=False):
assert cfg["KernelSpecManager"][KSMC] == CKSM

log.info("{}abled BeakerX server config".format(action_prefix))



def make_parser():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--prefix",
Expand All @@ -152,14 +153,14 @@ def make_parser():
def _disable_beakerx():
_uninstall_nbextension()
_uninstall_kernels()

def _install_beakerx(args):
_install_nbextension()
_install_kernels()
_install_css()
_copy_icons()
_install_kernelspec_manager(args.prefix)

def install():
try:
parser = make_parser()
Expand Down
19 changes: 2 additions & 17 deletions beakerx/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
from setupbase import (
create_cmdclass,
install_node_modules,
update_kernelspec_class,
install_kernels,
copy_files,
run_gradle,
get_version,
get_data_files,
Expand All @@ -33,28 +30,16 @@
cmdclass = create_cmdclass(develop_wrappers=[
'js',
'java',
'kernels_develop',
'kernelspec_class',
'custom_css'
], distribute_wrappers=[
'js',
'java'
], install_wrappers=[
'kernelspec_class',
'custom_css'
])
cmdclass['js'] = install_node_modules(
path='js',
build_dir=os.path.join(here, 'js', 'dist'),
source_dir=os.path.join(here, 'js', 'src')
)
cmdclass['java'] = run_gradle(cmd='build')
cmdclass['kernels_develop'] = install_kernels(source_dir=os.path.join(here, 'beakerx', 'static', 'kernel'), target_dir=os.path.join(here, 'beakerx', 'static', 'kernel'))
cmdclass['kernelspec_class'] = update_kernelspec_class(prefix=os.environ['CONDA_PREFIX'])
cmdclass['custom_css'] = copy_files(
src=os.path.join(here, 'beakerx', 'static', 'custom'),
dest=os.path.join(os.environ['CONDA_PREFIX'], 'lib', 'python3.5', 'site-packages', 'notebook', 'static', 'custom')
)

setup_args = dict(
name = 'beakerx',
Expand Down Expand Up @@ -95,12 +80,12 @@
},
package_data={
'beakerx': [
'static/kernel/*/kernel.json'
'kernel/*/kernel.json'
]
},
data_files = [(
'share/jupyter/nbextensions/beakerx',
get_data_files(os.path.join('beaker', 'static'))
get_data_files(os.path.join('beaker'))
)],
install_requires = [
'notebook >=4.4.0',
Expand Down
148 changes: 4 additions & 144 deletions beakerx/setupbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,17 @@
"""

import os
import shutil
from glob import glob
import functools
import pipes
import sys
import site
import json
from subprocess import check_call
from string import Template
from beakerx.install import _classpath_for
from setuptools import Command
from setuptools.command.develop import develop
from setuptools.command.build_py import build_py
from setuptools.command.sdist import sdist
from setuptools.command.bdist_egg import bdist_egg
from distutils.command.install_data import install_data
from distutils import log

from traitlets.config.manager import BaseJSONConfigManager
from jupyter_core.paths import jupyter_config_dir

try:
from wheel.bdist_wheel import bdist_wheel
except ImportError:
Expand Down Expand Up @@ -81,7 +71,9 @@ def list2cmdline(cmd_list):
# ---------------------------------------------------------------------------
# Public Functions
# ---------------------------------------------------------------------------

def _classpath_for(kernel):
return pkg_resources.resource_filename(
'beakerx', os.path.join('kernel', kernel, 'lib', '*'))

def get_version(path):
version = {}
Expand Down Expand Up @@ -124,29 +116,24 @@ def update_package_data(distribution):
build_py.finalize_options()


def create_cmdclass(develop_wrappers=None, distribute_wrappers=None, install_wrappers=None, data_dirs=None):
def create_cmdclass(develop_wrappers=None, distribute_wrappers=None, data_dirs=None):
"""Create a command class with the given optional wrappers.
Parameters
----------
develop_wrapper: list(str), optional
The cmdclass names to run before running other commands
distribute_wrappers: list(str), optional
The cmdclass names to run before running other commands
install_wrappers: list(str), optional
The cmdclass names to run before running other commands
data_dirs: list(str), optional.
The directories containing static data.
"""
develop_wrappers = develop_wrappers or []
distribute_wrappers = distribute_wrappers or []
install_wrappers = install_wrappers or []
data_dirs = data_dirs or []
develop_wrapper = functools.partial(wrap_command, develop_wrappers, data_dirs)
distribute_wrapper = functools.partial(wrap_command, distribute_wrappers, data_dirs)
install_wrapper = functools.partial(wrap_command, install_wrappers, data_dirs)
cmdclass = dict(
develop=develop_wrapper(develop, strict=True),
install_data=install_wrapper(install_data, strict=is_repo),
sdist=distribute_wrapper(sdist, strict=True),
bdist_egg=bdist_egg if 'bdist_egg' in sys.argv else bdist_egg_disabled
)
Expand Down Expand Up @@ -304,133 +291,6 @@ def run(self):

return Yarn


def install_kernels(source_dir=os.path.join(here, 'beakerx', 'static', 'kernel'), target_dir=os.path.join(site_packages, 'beakerx', 'static', 'kernel')):
"""Install all kernels in a directory.
Parameters
----------
target_dir: str
The path of a directory containing kernels.
"""

class InstallKernels(BaseCommand):
description = 'Install all kernels in a directory'

def run(self):
try:
def install_kernel(source_kernelspec='', kernelspec_name=None):
name = kernelspec_name if kernelspec_name else os.path.basename(source_kernelspec)
base_classpath = _classpath_for('base')
kernel_classpath = _classpath_for(name)
classpath = json.dumps(os.pathsep.join([base_classpath, kernel_classpath]))
src_spec_file = os.path.join(source_kernelspec, 'kernel.json')
target_spec_file = src_spec_file + '.tmp'
with open(src_spec_file) as infile:
lines = Template(infile.read()).substitute(PATH=classpath)
with open(target_spec_file, 'w') as outfile:
outfile.write(lines)
os.remove(src_spec_file)
os.rename(target_spec_file, src_spec_file)
run(['jupyter', 'kernelspec', 'install', '--sys-prefix', '--replace', '--name', name, source_kernelspec])

for dir, subdirs, files in os.walk(source_dir):
if 'kernel.json' in files:
install_kernel(dir)
else:
continue
except Exception as e:
log.error(str(e))

return InstallKernels


def update_kernelspec_class(prefix=None):
"""Return a Command for updating kernelspec_class in jupyter_notebook_config.json.
Parameters
----------
prefix: string
Base path of Python environment
"""

class UpdateKernelspec(BaseCommand):
description = 'Update kernelspec_class in jupyter_notebook_config.json'

user_options = [
('disable', 'd', 'disable'),
]

def initialize_options(self):
self.disable = False

def run(self):
CKSM = "beakerx.kernel_spec.BeakerXKernelSpec"
KSMC = "kernel_spec_class"

def pretty(it):
return json.dumps(it, indent=2)

log.info("{}abling BeakerX server config...".format("Dis" if self.disable else "En"))

path = jupyter_config_dir()

if prefix is not None:
path = os.path.join(prefix, "etc", "jupyter")
if not os.path.exists(path):
log.debug("Making directory {}...".format(path))
os.makedirs(path)

cm = BaseJSONConfigManager(config_dir=path)
cfg = cm.get("jupyter_notebook_config")

log.debug("Existing config in {}...\n{}".format(path, pretty(cfg)))

nb_app = cfg.setdefault("KernelSpecManager", {})

if self.disable and nb_app.get(KSMC, None) == CKSM:
nb_app.pop(KSMC)
else:
nb_app.update({KSMC: CKSM})

log.debug("Writing config in {}...".format(path))

cm.set("jupyter_notebook_config", cfg)

cfg = cm.get("jupyter_notebook_config")

log.debug("Verifying config in {}...\n{}".format(path, pretty(cfg)))

if self.disable:
assert KSMC not in cfg["KernelSpecManager"]
else:
assert cfg["KernelSpecManager"][KSMC] == CKSM

log.info("{}abled BeakerX server config".format("Dis" if self.disable else "En"))

return UpdateKernelspec


def copy_files(src, dest):
"""Copy files from one directory to another.
Parameters
----------
src: Source directory
dest: Destination directory
"""

class CopyFiles(BaseCommand):
description = 'Copy files from one directory to another.'

def run(self):
if os.path.exists(dest):
shutil.rmtree(dest)
shutil.copytree(src, dest)

return CopyFiles


def run_gradle(path=kernel_path, cmd='build'):
"""Return a Command for running gradle scripts.
Expand Down
5 changes: 2 additions & 3 deletions kernel/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import groovy.time.TimeCategory
/*
* Copyright 2017 TWO SIGMA OPEN SOURCE, LLC
*
Expand All @@ -14,6 +13,7 @@ import groovy.time.TimeCategory
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import groovy.time.TimeCategory

task wrapper(type: Wrapper) {
gradleVersion = '3.5'
Expand Down Expand Up @@ -52,6 +52,7 @@ subprojects {
}
}
}
ext.pathToStaticContent = '../../beakerx/beakerx/kernel/'

test {
testLogging {
Expand All @@ -63,8 +64,6 @@ subprojects {
}
}

ext.pathToStaticContent = '../../beakerx/beakerx/static/kernel/'

task staticContent {
doLast {
copy {
Expand Down

0 comments on commit f6aa435

Please sign in to comment.