Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 2ff8501
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Thu Jul 27 16:13:14 2017 -0700

    preparing spawnteract for move to monorepo

commit 13be977
Author: Lukas Geiger <lukas.geiger94@gmail.com>
Date:   Mon Jun 5 03:00:24 2017 +0200

    3.1.0

commit 6fc728c
Merge: 61c109d 4f4cb88
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Sun Jun 4 17:57:19 2017 -0700

    Merge pull request nteract#27 from lgeiger/allow-restart

    Add launchSpecFromConfig to allow easy kernel restarting

commit 61c109d
Merge: 4809e74 61a63f7
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Sun Jun 4 17:56:16 2017 -0700

    Merge pull request nteract#28 from lgeiger/bumps

    Bumps and run prettier

commit 4f4cb88
Author: Lukas Geiger <lukas.geiger94@gmail.com>
Date:   Mon Jun 5 02:52:12 2017 +0200

    Remove leading underscore

commit 4902c7f
Author: Lukas Geiger <lukas.geiger94@gmail.com>
Date:   Mon Jun 5 00:26:34 2017 +0200

    Rename connection config -> connection info

commit ea6755d
Author: Lukas Geiger <lukas.geiger94@gmail.com>
Date:   Mon Jun 5 00:21:16 2017 +0200

    Stay compatible with older versions of Node

commit 61a63f7
Author: Lukas Geiger <lukas.geiger94@gmail.com>
Date:   Mon Jun 5 00:16:43 2017 +0200

    :arrow_up: all deps

commit 5bdd964
Author: Lukas Geiger <lukas.geiger94@gmail.com>
Date:   Mon Jun 5 00:15:11 2017 +0200

    :arrow_up: jsonfile@^3.0.0

commit 6237170
Author: Lukas Geiger <lukas.geiger94@gmail.com>
Date:   Mon Jun 5 00:12:01 2017 +0200

    :arrow_up: chai@^4.0.1

    And fix Node 8 deprecation warning when calling async functions without
    a callback

commit ebb1249
Author: Lukas Geiger <lukas.geiger94@gmail.com>
Date:   Mon Jun 5 00:01:45 2017 +0200

    Add launchSpecFromConfig to allow easy kernel restarting

commit 4809e74
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Tue May 30 16:31:30 2017 -0700

    3.0.0

commit e0961d3
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Mon May 8 09:22:24 2017 -0700

    test: bring ipykernel install up to date

commit df1b8f0
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Mon May 8 09:17:05 2017 -0700

    Default spawn options to stdio: ignore

    Spawn options are now folded into a set of default options, using `Object.assign`
    (not a full merge).

    Note: this is a breaking change, particularly for Hydrogen which was providing
    "loose" stdout and stderr as notification messages in Atom (I love having this
    when I'm working with Spark in Hydrogen).

    Fixes nteract#24

commit c66a66f
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Mon May 8 09:12:42 2017 -0700

    lint: run prettier through codebase

commit b7f3dba
Merge: d8324da c0c0b1b
Author: Lukas Geiger <lgeiger@users.noreply.github.com>
Date:   Sat Jan 21 14:51:16 2017 +0100

    Merge pull request nteract#21 from nteract/greenkeeper/initial

    Add Greenkeeper badge 🌴

commit c0c0b1b
Author: greenkeeper[bot] <greenkeeper[bot]@users.noreply.github.com>
Date:   Fri Jan 20 21:51:47 2017 +0000

    docs(readme): add Greenkeeper badge

    https://greenkeeper.io/

commit d8324da
Author: Lukas Geiger <lukas.geiger94@gmail.com>
Date:   Thu Dec 22 17:25:02 2016 +0100

    2.2.1

commit 37b8b62
Merge: 07c666c 74959e6
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Thu Dec 22 09:23:13 2016 -0600

    Merge pull request nteract#20 from lgeiger/bump-uuid

    Bump uuid@3.0.1

commit 74959e6
Author: Lukas Geiger <lukas.geiger94@gmail.com>
Date:   Thu Dec 22 11:14:17 2016 +0100

    :arrow_up: uuid@3.0.1

commit 07c666c
Merge: d51f036 e0cde82
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Thu Sep 1 01:17:18 2016 -0500

    Merge pull request nteract#19 from lgeiger/fix-docs-for-travis

    Fix docs publishing with travis

commit e0cde82
Author: Lukas Geiger <lukas.geiger94@gmail.com>
Date:   Thu Sep 1 08:07:00 2016 +0200

    Fix docs publishing with travis

commit d51f036
Merge: b9cfb5d c042e95
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Thu Sep 1 00:45:34 2016 -0500

    Merge pull request nteract#17 from nteract/docs

    Add docs

commit c042e95
Author: Lukas Geiger <lukas.geiger94@gmail.com>
Date:   Wed Aug 31 22:52:03 2016 +0100

    Publish docs with travis

commit d504488
Author: Lukas Geiger <lukas.geiger94@gmail.com>
Date:   Wed Aug 31 20:57:41 2016 +0100

    Add jsdoc script

commit b9cfb5d
Merge: 71a0ad0 93ada1f
Author: Safia Abdalla <safia@safia.rocks>
Date:   Wed Aug 31 21:42:05 2016 -0600

    Merge pull request nteract#18 from nteract/bumps

    Bump dependencies

commit 93ada1f
Author: Lukas Geiger <lukas.geiger94@gmail.com>
Date:   Wed Aug 31 20:51:25 2016 +0100

    :arrow_up: Bump dependencies

commit 71a0ad0
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Sat Aug 27 15:21:21 2016 -0700

    2.2.0

commit 9793269
Merge: 7f2a436 dfd4da3
Author: John Detlefs <jdetle@gmail.com>
Date:   Sat Aug 27 15:19:30 2016 -0700

    Merge pull request nteract#16 from nteract/return-kernel-spec

    return the kernelspec, to pass through launch

commit dfd4da3
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Sat Aug 27 15:13:25 2016 -0700

    return the kernelspec, to pass through launch

commit 7f2a436
Author: Lukas Geiger <lukas.geiger94@gmail.com>
Date:   Tue Aug 16 23:09:00 2016 +0200

    2.1.1

commit 37e75ee
Merge: 7732e5c 2300ba3
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Tue Aug 16 14:56:13 2016 -0500

    Merge pull request nteract#15 from captainsafia/respect-env

    Respect ENV in kernelspec

commit 2300ba3
Author: Safia Abdalla <safia@safia.rocks>
Date:   Tue Aug 16 11:51:37 2016 -0600

    Respect ENV in kernelspec

commit 7732e5c
Author: Lukas Geiger <lukas.geiger94@gmail.com>
Date:   Wed Aug 3 00:51:25 2016 +0200

    2.1.0

commit ac0e1ec
Merge: fdbf33e 93cba42
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Tue Aug 2 17:33:42 2016 -0500

    Merge pull request nteract#13 from lgeiger/master

    Export `launchSpec`

commit 93cba42
Author: Lukas Geiger <lukas.geiger94@gmail.com>
Date:   Thu Jul 28 14:06:34 2016 +0200

    Export `launchSpec`

commit fdbf33e
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Sat Mar 26 17:19:45 2016 -0500

    2.0.0

commit 13d387b
Merge: 334684a 9602160
Author: Jonathan Frederic <jdfreder@calpoly.edu>
Date:   Sat Mar 26 15:18:10 2016 -0700

    Merge pull request nteract#12 from rgbkrk/trim-down-api

    Accept spawnOptions first, specs after

commit 9602160
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Sat Mar 26 17:11:17 2016 -0500

    Accept spawnOptions first, specs after

commit 334684a
Merge: 536bd04 476cdaa
Author: Jonathan Frederic <jdfreder@calpoly.edu>
Date:   Sat Mar 26 15:05:20 2016 -0700

    Merge pull request nteract#11 from rgbkrk/passthrough-opts

    Passthrough opts

commit 476cdaa
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Sat Mar 26 16:42:42 2016 -0500

    Change name all the way through.

commit 4dde578
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Sat Mar 26 16:41:25 2016 -0500

    Be more explicit, set spawn options as needed

commit fd457f8
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Sat Mar 26 16:39:17 2016 -0500

    Don't mutate passed in objects.

commit 017b591
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Sat Mar 26 16:35:55 2016 -0500

    Pass child_process.spawn options through

commit c29bc0c
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Sat Mar 26 16:22:28 2016 -0500

    Linting.

commit 536bd04
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Sun Mar 20 15:08:09 2016 -0400

    1.0.3

commit 01c4d09
Merge: 52fbe0e 548dcbd
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Sun Mar 20 15:05:20 2016 -0400

    Merge pull request nteract#10 from mathisonian/patch-1

    add repository to package.json

commit 548dcbd
Author: Matthew Conlen <mc@mathisonian.com>
Date:   Sun Mar 20 13:37:46 2016 -0400

    add repository to package.json

commit 52fbe0e
Merge: 3516d51 697f0ed
Author: Safia Abdalla <safia@safia.rocks>
Date:   Tue Mar 8 22:42:33 2016 -0600

    Merge pull request nteract#9 from nteract/cleanup-example

    Provide example cleanup code

commit 697f0ed
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Tue Mar 8 18:20:17 2016 -0600

    Provide example cleanup code

commit 3516d51
Author: Jonathan Frederic <jon.freder@gmail.com>
Date:   Wed Feb 10 10:19:52 2016 -0800

    1.0.2

commit 8367ad5
Author: Jonathan Frederic <jon.freder@gmail.com>
Date:   Wed Feb 10 10:19:44 2016 -0800

    1.0.1

commit 80983c1
Merge: 55ab7ae 39cd03d
Author: Paul Ivanov <pivanov314@gmail.com>
Date:   Wed Feb 10 09:05:01 2016 -0800

    Merge pull request nteract#7 from jdfreder/mkdirp

    Problem: Can't launch kernels because runtime dir doesn't exist!

commit 39cd03d
Author: Jonathan Frederic <jon.freder@gmail.com>
Date:   Wed Feb 10 07:53:03 2016 -0800

    Fix things for me

commit 55ab7ae
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Fri Feb 5 12:11:15 2016 -0600

    1.0.0

commit f5c3544
Merge: 95c2666 ba2e7c5
Author: Jonathan Frederic <jdfreder@calpoly.edu>
Date:   Fri Feb 5 09:03:01 2016 -0800

    Merge pull request nteract#6 from nteract/connection-file-migration

    Problem: Not all code was using the connectionFile

commit ba2e7c5
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Fri Feb 5 10:00:36 2016 -0600

    Problem: Not all code was using the connectionFile

    Solution: Adapt the two remaining bits, which fixes the tests

commit 95c2666
Merge: 3344d08 38827b8
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Fri Feb 5 09:55:20 2016 -0600

    Merge pull request nteract#5 from jdfreder/r

    Problem: Readme out of date

commit 38827b8
Author: Jonathan Frederic <jon.freder@gmail.com>
Date:   Fri Feb 5 07:46:05 2016 -0800

    Update now out of date readme

commit 3344d08
Merge: 05716d1 c25ae4a
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Fri Feb 5 09:39:21 2016 -0600

    Merge pull request nteract#4 from jdfreder/docs

    Problem: How do I use this?

commit c25ae4a
Author: Jonathan Frederic <jon.freder@gmail.com>
Date:   Fri Feb 5 07:08:55 2016 -0800

    Use jsdoc

commit 05716d1
Merge: 36e50bb 0ae8e23
Author: Jonathan Frederic <jdfreder@calpoly.edu>
Date:   Fri Feb 5 06:26:00 2016 -0800

    Merge pull request #3 from nteract/bump-kernelspecs

    Problem: We're on an old version of kernelspecs

commit 0ae8e23
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Fri Feb 5 00:02:50 2016 -0600

    Problem: We weren't cleaning up after ourselves

    Solution: Delete the connection file and kill the spawned kernel.

commit c311814
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Thu Feb 4 23:58:42 2016 -0600

    Problem: We didn't have tests

    Solution: Begin work on tests

commit 559c6ae
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Thu Feb 4 23:43:34 2016 -0600

    Problem: we don't have a Travis test

    Solution: Add a quick test for Travis' sake

commit a8fef9e
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Thu Feb 4 23:37:42 2016 -0600

    Problem: We're on an old version of kernelspecs

    Solution: upgrade, delight at speed improvements and less nested deps

commit 36e50bb
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Mon Feb 1 21:56:02 2016 -0600

    0.1.0

commit b6700c2
Merge: 928dc06 1943b0a
Author: Ken Wheeler <dubmediagroup@gmail.com>
Date:   Mon Feb 1 22:52:38 2016 -0500

    Merge pull request #2 from nteract/kernelspecs-bump

    Problem: kernelspecs 0.1.0 didn't see sys.prefix

commit 1943b0a
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Mon Feb 1 21:43:43 2016 -0600

    Problem: kernelspecs 0.1.0 didn't see sys.prefix

    Solution: bump kernelspecs to 0.2.0

commit 928dc06
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Thu Jan 21 23:45:44 2016 -0600

    0.0.4

commit 766c5c2
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Thu Jan 21 23:45:14 2016 -0600

    Fix naming bug.

commit 70391be
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Thu Jan 21 23:44:21 2016 -0600

    0.0.3

commit 308ff02
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Thu Jan 21 23:11:30 2016 -0600

    0.0.2

commit 001e501
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Thu Jan 21 23:10:25 2016 -0600

    Doc that spawning.

commit cc473fc
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Thu Jan 21 23:04:32 2016 -0600

    0.0.1

commit 73ec321
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Thu Jan 21 23:03:30 2016 -0600

    Take out me prototyping.

commit 5fe6176
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Thu Jan 21 23:03:07 2016 -0600

    Allow for spawning by kernel name

commit 2dba889
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Thu Jan 21 22:31:07 2016 -0600

    Gitignore the usual ipynb suspects

commit fad5c13
Author: Will Whitney <wwhitney@mit.edu>
Date:   Thu Jan 21 22:30:41 2016 -0600

    Initial config creator.

commit aa15684
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Wed Jan 20 11:11:16 2016 -0600

    Lay out sample package.

commit 1d6a16e
Author: Kyle Kelley <rgbkrk@gmail.com>
Date:   Wed Jan 20 10:52:17 2016 -0600

    Boilerplate package.
  • Loading branch information
rgbkrk committed Jul 28, 2017
1 parent 4a777db commit 471fc08
Show file tree
Hide file tree
Showing 7 changed files with 368 additions and 0 deletions.
34 changes: 34 additions & 0 deletions packages/spawnteract/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Docs
docs

# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
node_modules

.ipynb_checkpoints
*.ipynb
17 changes: 17 additions & 0 deletions packages/spawnteract/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: node_js
node_js:
- "4.2"
- "5.4"
sudo: false
env:
global:
- GH_REF: github.com/nteract/spawnteract.git
- secure: JeyqTluw8Dtr6MYVEfuVw/gvmJ/1Q55qts7+ncGIsZRJ1ptwNPAF4xaKnEYtXIAb+ZDx1HvEEtuFbRcTsVMrWKep7GNV+w7CQKVrzfWaffGF0ttw3y98/NBbBG0EI7UmeF3TjUdGH5c8Tzt4wASvT3EFEas2k3eZhl7eprAdGlyXuDHgNUUcHUhoMLtRlW+t7QyZMaA2G3GjAVHL9giy7tvXnvb34r/D5BrGVC5oFYyDblKi6vvk2bE/Bgb+WNznlt+oj2tvxR76zBBj/xQoT31SrQ03DvrnJ0rv+Qg7oi0fx/whaNk0vM51iWsqZqak21MTPks9u+S4J5qkKmcJvgcoCPhxxPtUuCstEvzeitgWZUJRYKeX47dBRE085DVUQ9lvLKa69Ct0BVQemaV0wWIUzBJ3tZBSOtUTm5kmC2Mi0MtsPDSSv3h68CLCKv32UqSy1NZ0rDzDkP3w+mH5ee0GYHeJBhUa939twZDZEnh6VmiqrWiy22VyXNQ1T7sOtQw59ATtIvGeN08WB4ymODvWSzSH/uB+n9bRbBKJwvkT3LJ6K7BRjavrW0xUTDCBApjisbTV4b0Nt0Y4T0amdQr0Lxyx45QLNkViL3dNrkeayhSSRBqfpRZZAC0pSegIL+OmTCQCaIzsy8EFK9csKEx6yJ11htbJLyfmQ97G/x4=

before_install:
- pip install -U pip --user
- python -m pip install ipykernel==4.6.1 IPython==5.3.0 --user
- python -m ipykernel install --user

after_success:
- bash ./travis_after_success.sh
36 changes: 36 additions & 0 deletions packages/spawnteract/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Spawnteract

[![Greenkeeper badge](https://badges.greenkeeper.io/nteract/spawnteract.svg)](https://greenkeeper.io/)

Spawn yourself a Jupyter kernel backend.

```
npm install --save spawnteract
```

## Usage

```javascript
const spawnteract = require('spawnteract')

spawnteract.launch('python3').then(kernel => {
// Returns
// kernel.spawn <-- The running process, from child_process.spawn(...)
// kernel.connectionFile <-- Connection file path
// kernel.config <-- Connection information from the file

// Print the ip address and port for the shell channel
console.log(kernel.config.ip + ':' + kernel.config.shell_port);
})
```

You'll need to close `kernel.spawn` yourself as well as delete `kernel.connectionFile` from disk when finished:

```js
function cleanup(kernel) {
kernel.spawn.kill();
fs.unlink(kernel.connectionFile);
}
```

You will probably end up wanting to use this with [enchannel-zmq-backend](https://github.com/nteract/enchannel-zmq-backend).
211 changes: 211 additions & 0 deletions packages/spawnteract/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
/**
* This module contains methods that allow you to spawn Jupyter kernels. You
* can spawn kernels either by name or by a kernelSpec object (see the
* `kernelspecs` npm package for more information).
*
* Usage example:
* ```js
* var spawnResults = require('spawnteract').launch('python3');
*
* // Print the ip address and port for the shell channel
* console.log(spawnResults.config.ip + ':' + spawnResults.config.shell_port);
* ```
*
* You'll need to close `spawnResults.spawn` yourself as well as delete
* `spawnResults.connectionFile` from disk when finished.
*
* @exports spawnteract
*/

/* eslint camelcase: 0 */
// ^--- #justjupyterthings
/**
*
*/
const path = require("path");

const kernelspecs = require("kernelspecs");
const jp = require("jupyter-paths");

const uuid = require("uuid");
const getPorts = require("portfinder").getPorts;
const jsonfile = require("jsonfile");

const child_process = require("child_process");
const mkdirp = require("mkdirp");

/**
* Creates a connectionInfo object given an array of ports
* @private
* @param {number[]} ports array of ports to use for the connection, [hb_port,
* control_port, shell_port, stdin_port, iopub_port]
* @return {object} connectionInfo object
*/
function createConnectionInfo(ports) {
return {
version: 5,
key: uuid.v4(),
signature_scheme: "hmac-sha256",
transport: "tcp",
ip: "127.0.0.1",
hb_port: ports[0],
control_port: ports[1],
shell_port: ports[2],
stdin_port: ports[3],
iopub_port: ports[4]
};
}

/**
* Write a connection file
* @public
* @param {object} [portFinderOptions] connection options
* see {@link https://github.com/indexzero/node-portfinder/blob/master/lib/portfinder.js }
* @param {number} [portFinderOptions.port]
* @param {string} [portFinderOptions.host]
* @return {object} configResults
* @return {object} configResults.config connection info
* @return {string} configResults.connectionFile path to the connection file
*/
function writeConnectionFile(portFinderOptions) {
const options = Object.assign({}, portFinderOptions);
options.port = options.port || 9000;
options.host = options.host || "127.0.0.1";

return new Promise((resolve, reject) => {
getPorts(5, options, (err, ports) => {
if (err) {
reject(err);
} else {
// Make sure the kernel runtime dir exists before trying to write the
// kernel file.
const runtimeDir = jp.runtimeDir();
mkdirp(runtimeDir);

// Write the kernel connection file.
const config = createConnectionInfo(ports);
const connectionFile = path.join(
jp.runtimeDir(),
`kernel-${uuid.v4()}.json`
);
jsonfile.writeFile(connectionFile, config, jsonErr => {
if (jsonErr) {
reject(jsonErr);
} else {
resolve({
config,
connectionFile
});
}
});
}
});
});
}

/**
* Launch a kernel for a given kernelSpec
* @public
* @param {object} kernelSpec describes a specific
* kernel, see the npm
* package `kernelspecs`
* @param {object} [spawnOptions] options for [child_process.spawn]{@link https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options}
* @return {object} spawnResults
* @return {ChildProcess} spawnResults.spawn spawned process
* @return {string} spawnResults.connectionFile connection file path
* @return {object} spawnResults.config connection info
*
*/
function launchSpec(kernelSpec, spawnOptions) {
return writeConnectionFile().then(c => {
return launchSpecFromConnectionInfo(
kernelSpec,
c.config,
c.connectionFile,
spawnOptions
);
});
}

/**
* Launch a kernel for a given kernelSpec and connection info
* @public
* @param {object} kernelSpec describes a specific
* kernel, see the npm
* package `kernelspecs`
* @param {object} config connection config
* @param {string} connectionFile path to the config file
* @param {object} [spawnOptions] options for [child_process.spawn]{@link https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options}
* @return {object} spawnResults
* @return {ChildProcess} spawnResults.spawn spawned process
* @return {string} spawnResults.connectionFile connection file path
* @return {object} spawnResults.config connection info
*
*/
function launchSpecFromConnectionInfo(
kernelSpec,
config,
connectionFile,
spawnOptions
) {
const argv = kernelSpec.argv.map(
x => (x === "{connection_file}" ? connectionFile : x)
);

const defaultSpawnOptions = {
stdio: "ignore"
};
const env = Object.assign({}, process.env, kernelSpec.env);
const fullSpawnOptions = Object.assign(
{},
defaultSpawnOptions,
{ env: env },
spawnOptions
);

const runningKernel = child_process.spawn(
argv[0],
argv.slice(1),
fullSpawnOptions
);
return {
spawn: runningKernel,
connectionFile,
config,
kernelSpec
};
}

/**
* Launch a kernel by name
* @public
* @param {string} kernelName
* @param {object[]} [specs] array of kernelSpec
* objects to look through.
* See the npm package
* `kernelspecs`
* @param {object} [spawnOptions] options for [child_process.spawn]{@link https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options}
* @return {object} spawnResults
* @return {ChildProcess} spawnResults.spawn spawned process
* @return {string} spawnResults.connectionFile connection file path
* @return {object} spawnResults.config connection info
*/
function launch(kernelName, spawnOptions, specs) {
// Let them pass in a cached specs file
if (!specs) {
return kernelspecs
.findAll()
.then(sp => launch(kernelName, spawnOptions, sp));
}
if (!specs[kernelName]) {
return Promise.reject(new Error(`No spec available for ${kernelName}`));
}
const spec = specs[kernelName].spec;
return launchSpec(spec, spawnOptions);
}

module.exports = {
launch,
launchSpec,
launchSpecFromConnectionInfo
};
29 changes: 29 additions & 0 deletions packages/spawnteract/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "spawnteract",
"version": "3.1.0",
"description": "Proving out an API for spawning Jupyter kernels",
"main": "index.js",
"scripts": {
"test": "mocha --recursive",
"build:docs": "jsdoc -R README.md -d docs index.js"
},
"author": "Kyle Kelley <rgbkrk@gmail.com>",
"license": "BSD-3-Clause",
"repository": {
"type": "git",
"url": "https://github.com/nteract/spawnteract.git"
},
"dependencies": {
"jsonfile": "^3.0.0",
"jupyter-paths": "^1.0.0",
"kernelspecs": "^1.0.0",
"mkdirp": "^0.5.1",
"portfinder": "^1.0.13",
"uuid": "^3.0.1"
},
"devDependencies": {
"chai": "^4.0.1",
"jsdoc": "^3.4.0",
"mocha": "^3.4.2"
}
}
23 changes: 23 additions & 0 deletions packages/spawnteract/test/spawn_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const expect = require("chai").expect;
const fs = require("fs");

const launch = require("../").launch;
const kernelspecs = require("kernelspecs");

describe("launch", () => {
it("spawns a kernel", () => {
return kernelspecs
.findAll()
.then(kernels => {
const kernel = kernels.python2 || kernels.python3;
return launch(kernel.name);
})
.then(c => {
expect(c).to.not.be.null;
expect(c.spawn).to.not.be.null;

c.spawn.kill();
fs.unlinkSync(c.connectionFile);
});
});
});
18 changes: 18 additions & 0 deletions packages/spawnteract/travis_after_success.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
if [[ $TRAVIS_PULL_REQUEST == false && $TRAVIS_BRANCH == "master" ]]
then
npm run build:docs

( cd docs
git init
git config user.email "travis@travis-ci.com"
git config user.name "Travis Bot"

git add .
git commit -m "Publish docs from $TRAVIS_BUILD_NUMBER"
git push --force --quiet "https://${GH_TOKEN}@${GH_REF}" master:gh-pages > /dev/null 2>&1
echo "Documentation has been published!"
)
else
echo "Documentation has not been published because not on master!"
fi

0 comments on commit 471fc08

Please sign in to comment.