check.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # -*- coding: utf-8 -*-
  2. from concurrent.futures import ThreadPoolExecutor
  3. import importlib.util
  4. from os import path as osp
  5. import os
  6. import shutil
  7. import sys
  8. import subprocess
  9. from tornado.ioloop import IOLoop
  10. from notebook.notebookapp import flags, aliases, NotebookApp
  11. from traitlets import Bool, Unicode
  12. from jupyterlab.labapp import get_app_dir
  13. here = osp.abspath(osp.dirname(__file__))
  14. # Import the base class from our sibling file
  15. mod_path = osp.abspath(osp.join(here, 'main.py'))
  16. spec = importlib.util.spec_from_file_location("example", mod_path)
  17. mod = importlib.util.module_from_spec(spec)
  18. spec.loader.exec_module(mod)
  19. class ExampleCheckApp(mod.ExampleApp):
  20. open_browser = Bool(False)
  21. def start(self):
  22. pool = ThreadPoolExecutor()
  23. future = pool.submit(run_browser, self.display_url)
  24. IOLoop.current().add_future(future, self._browser_finished)
  25. super().start()
  26. def _browser_finished(self, future):
  27. try:
  28. sys.exit(future.result())
  29. except Exception as e:
  30. self.log.error(str(e))
  31. sys.exit(1)
  32. def run_browser(url):
  33. """Run the browser test and return an exit code.
  34. """
  35. target = osp.join(get_app_dir(), 'example_test')
  36. if not osp.exists(osp.join(target, 'node_modules')):
  37. os.makedirs(target)
  38. subprocess.call(["jlpm"], cwd=target)
  39. subprocess.call(["jlpm", "add", "puppeteer"], cwd=target)
  40. src = osp.abspath(osp.join(here, '..', 'chrome-example-test.js'))
  41. shutil.copy(src, osp.join(target, 'chrome-example-test.js'))
  42. return subprocess.check_call(["node", "chrome-example-test.js", url], cwd=target)
  43. if __name__ == '__main__':
  44. ExampleCheckApp.launch_instance()