Skip to content

Commit

Permalink
Merge pull request #6 from ibm-watson-data-lab/issue4
Browse files Browse the repository at this point in the history
Issue4
  • Loading branch information
glynnbird authored Sep 26, 2017
2 parents 5ceb96f + f92917c commit b63edbc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
11 changes: 7 additions & 4 deletions pixiedust_node/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import subprocess
import os
from .nodestdreader import NodeStdReader
from pixiedust.utils.environment import Environment

# runs a Node sub-process and starts a NodeStdReader thread
# to listen to its stdout.
Expand All @@ -13,18 +14,20 @@ class Node:
# run a JavaScript script (path) with "node"
def __init__(self, path):
# get the home directory
node = 'node';
home = get_ipython().home_dir
home = Environment.pixiedustHome
node_home = os.path.join(home,'node')
if not os.path.exists(node_home):
os.makedirs(node_home)

# check that node exists
node_path = self.which(node)
node_path = self.which('node')

if node_path == None:
print 'ERROR: Cannot find Node.js executable'
raise FileNotFoundError('node executable not found in path')
else:
# create sub-process
self.ps = subprocess.Popen( (node_path, path), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd = home)
self.ps = subprocess.Popen( (node_path, path), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd = node_home)
# print ("Node process id", self.ps.pid)

# create thread to read this process's output
Expand Down
37 changes: 34 additions & 3 deletions pixiedust_node/npm.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@

import subprocess
import os
from .nodestdreader import NodeStdReader
from pixiedust.utils.environment import Environment

# npm helper
# allows npm modules to be installed, removed and listed
class Npm:

# run an npm command
def cmd(self, command, module):
# create node_modules
home = Environment.pixiedustHome
node_home = os.path.join(home,'node')
node_modules = os.path.join(node_home,'node_modules')
if not os.path.exists(node_modules):
os.makedirs(node_modules)

# create sub-process
args = ['npm', command, '-s']
npm_path = self.which('npm')
args = [npm_path, command, '-s']
if (module):
if (isinstance(module, str)):
args.append(module)
else:
args.extend(module)
print ' '.join(args)
output = subprocess.check_output(args)
print output
ps = subprocess.Popen( args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd = node_home)

# create thread to read this process's output
t = NodeStdReader(ps)
ps.wait()

def install(self, module):
self.cmd('install', module)
Expand All @@ -26,3 +40,20 @@ def remove(self, module):

def list(self):
self.cmd('list', None)

def is_exe(self, fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

def which(self, program):
fpath, fname = os.path.split(program)
if fpath:
if self.is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if self.is_exe(exe_file):
return exe_file

return None
2 changes: 1 addition & 1 deletion pixiedust_node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pixiedust_node",
"version": "0.1.2",
"version": "0.1.3",
"description": "Run Node.js cells in Jupyter notebooks with Pixiedust",
"main": "pixiedustNodeRepl.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup, find_packages

setup(name='pixiedust_node',
version='0.1.2',
version='0.1.3',
description='Pixiedust extension for Node.js',
url='https://github.com/ibm-watson-data-lab/pixiedust_node',
install_requires=['pixiedust'],
Expand Down

0 comments on commit b63edbc

Please sign in to comment.