main.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright (c) Jupyter Development Team.
  2. # Distributed under the terms of the Modified BSD License.
  3. from __future__ import print_function, absolute_import
  4. import atexit
  5. import shutil
  6. import subprocess
  7. import sys
  8. import tempfile
  9. from multiprocessing.pool import ThreadPool
  10. from tornado import ioloop
  11. from notebook.notebookapp import NotebookApp
  12. from traitlets import Bool, Unicode
  13. root_dir = tempfile.mkdtemp(prefix='mock_contents')
  14. atexit.register(lambda: shutil.rmtree(root_dir, True))
  15. def run_task(func, args=(), kwds={}):
  16. """Run a task in a thread and exit with the return code."""
  17. loop = ioloop.IOLoop.instance()
  18. worker = ThreadPool(1)
  19. def callback(result):
  20. loop.add_callback(lambda: sys.exit(result))
  21. def start():
  22. worker.apply_async(func, args, kwds, callback)
  23. loop.call_later(1, start)
  24. class TestApp(NotebookApp):
  25. open_browser = Bool(False)
  26. notebook_dir = Unicode(root_dir)
  27. def start(self):
  28. run_task(run_node, args=(self.connection_url, self.token))
  29. super(TestApp, self).start()
  30. def run_node(base_url, token):
  31. # Run the node script with command arguments.
  32. node_command = ['node', 'index.js', '--baseUrl', base_url]
  33. if token:
  34. node_command.append('--token=%s' % token)
  35. print('*' * 60)
  36. print(' '.join(node_command))
  37. return subprocess.check_call(node_command)
  38. if __name__ == '__main__':
  39. TestApp.launch_instance()